<commit_before><?php

/*
 * This file is part of the AuthnetJSON package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace JohnConde\Authnet;

class AuthnetWebhookTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @covers            \JohnConde\Authnet\AuthnetWebhook::__construct()
     */
    public function testConstructor()
    {
        $signatureKey = '52CB4A002C634B84E397DC8A218E1A160BA7CAB7CBE4C05B35E9CBB05E14FE4A2385812E980CCF97D177F17863CE214D1BE6CE8E1E894487AACF3609C1A5FE17';
        $webhookJson = '{"notificationId":"182cbbff-cab2-4080-931d-80e5d818f23a","eventType":"net.authorize.payment.authcapture.created","eventDate":"2017-08-18T20:40:52.7722007Z","webhookId":"849eb87e-078a-4169-a34b-c0bded5019a8","payload":{"responseCode":0,"authCode":"Z3PV5H","avsResponse":"Y","authAmount":0.0,"entityName":"transaction","id":"40005915599"}}';
        $headers = [
            'X-Request-Id' => 'ae3b39b1-c58e-4a78-859b-1b4e6c62c5b7',
            'X-ANET-Signature' => 'sha512=A1E0CC0F8A7B6274E0951CAC5EB63146DCE02B98DEE3954579F70FC6B7B15384BA5A8E331661B51DE953E141E88994DFA7D267FFB8428BE0660511B6EFB94960'
        ];

        $webhook = new AuthnetWebhook($signatureKey, $webhookJson, $headers);

        $reflectionOfWebhook = new \ReflectionObject($webhook);
        $signature = $reflectionOfWebhook->getProperty('signature');
        $signature->setAccessible(true);

        $this->assertEquals($signatureKey, $signature->getValue($webhook));
    }

    /**
     * @covers            \JohnConde\Authnet\AuthnetWebhook::__construct()
     * @expectedException \JohnConde\Authnet\AuthnetInvalidJsonException
     */
    public function testExceptionIsRaisedForCannotSetParamsException()
    {
        new AuthnetWebhook('', 'Not JSON', []);
    }

    /**
     * @covers            \JohnConde\Authnet\AuthnetWebhook::__toString()
     */
    public function testToString()
    {
        $signatureKey = '52CB4A002C634B84E397DC8A218E1A160BA7CAB7CBE4C05B35E9CBB05E14FE4A2385812E980CCF97D177F17863CE214D1BE6CE8E1E894487AACF3609C1A5FE17';
        $webhookJson = '{"notificationId":"182cbbff-cab2-4080-931d-80e5d818f23a","eventType":"net.authorize.payment.authcapture.created","eventDate":"2017-08-18T20:40:52.7722007Z","webhookId":"849eb87e-078a-4169-a34b-c0bded5019a8","payload":{"responseCode":0,"authCode":"Z3PV5H","avsResponse":"Y","authAmount":0.0,"entityName":"transaction","id":"40005915599"}}';
        $headers = [
            'X-Request-Id' => 'ae3b39b1-c58e-4a78-859b-1b4e6c62c5b7',
            'X-ANET-Signature' => 'sha512=A1E0CC0F8A7B6274E0951CAC5EB63146DCE02B98DEE3954579F70FC6B7B15384BA5A8E331661B51DE953E141E88994DFA7D267FFB8428BE0660511B6EFB94960'
        ];
<commit_msg>Updated tests to support optional headers and validation of signature<commit_after><?php

/*
 * This file is part of the AuthnetJSON package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace JohnConde\Authnet;

class AuthnetWebhookTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @covers            \JohnConde\Authnet\AuthnetWebhook::__construct()
     */
    public function testConstructor()
    {
        $signatureKey = '52CB4A002C634B84E397DC8A218E1A160BA7CAB7CBE4C05B35E9CBB05E14FE4A2385812E980CCF97D177F17863CE214D1BE6CE8E1E894487AACF3609C1A5FE17';
        $webhookJson = '{"notificationId":"182cbbff-cab2-4080-931d-80e5d818f23a","eventType":"net.authorize.payment.authcapture.created","eventDate":"2017-08-18T20:40:52.7722007Z","webhookId":"849eb87e-078a-4169-a34b-c0bded5019a8","payload":{"responseCode":0,"authCode":"Z3PV5H","avsResponse":"Y","authAmount":0.0,"entityName":"transaction","id":"40005915599"}}';
        $headers = [
            'X-Request-Id' => 'ae3b39b1-c58e-4a78-859b-1b4e6c62c5b7',
            'X-ANET-Signature' => 'sha512=A1E0CC0F8A7B6274E0951CAC5EB63146DCE02B98DEE3954579F70FC6B7B15384BA5A8E331661B51DE953E141E88994DFA7D267FFB8428BE0660511B6EFB94960'
        ];

        $webhook = new AuthnetWebhook($signatureKey, $webhookJson, $headers);

        $reflectionOfWebhook = new \ReflectionObject($webhook);
        $signature = $reflectionOfWebhook->getProperty('signature');
        $signature->setAccessible(true);

        $this->assertEquals($signatureKey, $signature->getValue($webhook));
    }

    /**
     * @covers            \JohnConde\Authnet\AuthnetWebhook::__construct()
     * @expectedException \JohnConde\Authnet\AuthnetInvalidCredentialsException
     */
    public function testExceptionIsRaisedForNoSignature()
    {
        new AuthnetWebhook('', 'Not JSON', []);
    }

    /**
     * @covers            \JohnConde\Authnet\AuthnetWebhook::__construct()
     * @expectedException \JohnConde\Authnet\AuthnetInvalidJsonException
     */
    public function testExceptionIsRaisedForCannotSetParamsException()
    {
        new AuthnetWebhook('a', 'Not JSON', []);
    }

    /**
     * @covers            \JohnConde\Authnet\AuthnetWebhook::__toString()
     */
    public function testToString()
    {
        $signatureKey = '52CB4A002C634B84E397DC8A218E1A160BA7CAB7CBE4C05B35E9CBB05E14FE4A2385812E980CCF97D177F17863CE214D1BE6CE8E1E894487AACF3609C1A5FE17';
        $webhookJson = '{"notificationId":"182cbbff-cab2-4080-931d-80e5d818f23a","eventType":"net.authorize.payment.authcapture.created","eventDate":"2017-08-18T20:40:52.7722007Z","webhookId":"849eb87e-078a-4169-a34b-c0bded5019a8","payload":{"responseCode":0,"authCode":"Z3PV5H","avsResponse":"Y","authAmount":0.0,"entityName":"transaction","id":"40005915599"}}';
        $headers = [
            'X-Request-Id' => 'ae3b39b1-c58e-4a78-859b-1b4e6c62c5b7',
            'X-ANET-Signature' => 'sha512=A1E0CC0F8A7B6274E0951CAC5EB63146DCE02B98DEE3954579F70FC6B7B15384BA5A8E331661B51DE953E141E88994DFA7D267FFB8428BE0660511B6EFB94960'
        ];

{'fraction_non_alphanumeric': 0.13222314737718568, 'fraction_numerical': 0.16985845129059118, 'mean_word_length': 4.576601671309192, 'pattern_counts': {'":': 44, '<': 11, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1527521', 'n_tokens_mistral': 2785, 'n_tokens_neox': 2285, 'n_words': 198}