ResponseTest.php 3.99 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Authorizenet\Test\Unit\Model\Directpost;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Authorizenet\Model\Directpost;

class ResponseTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var \Magento\Authorizenet\Model\Directpost\Response
     */
    private $responseModel;

    protected function setUp()
    {
        $objectManager = new ObjectManager($this);
        $this->responseModel = $objectManager->getObject(
            \Magento\Authorizenet\Model\Directpost\Response::class
        );
    }

    /**
     * @param $merchantMd5
     * @param $merchantApiLogin
     * @param $amount
     * @param $transactionId
     * @return string
     */
    protected function generateHash($merchantMd5, $merchantApiLogin, $amount, $transactionId)
    {
        return strtoupper(md5($merchantMd5 . $merchantApiLogin . $transactionId . $amount));
    }

    /**
     * @param string $storedHash
     * @param string $hashKey
     * @param string $merchantApiLogin
     * @param float|null $amount
     * @param string $transactionId
     * @param string $hash
     * @param bool $expectedValue
     * @dataProvider isValidHashDataProvider
     */
    public function testIsValidHash(
        string $storedHash,
        string $hashKey,
        string $merchantApiLogin,
        $amount,
        string $transactionId,
        string $hash,
        bool $expectedValue
    ) {
        $this->responseModel->setXAmount($amount);
        $this->responseModel->setXTransId($transactionId);
        $this->responseModel->setData($hashKey, $hash);
        $result = $this->responseModel->isValidHash($storedHash, $merchantApiLogin);

        $this->assertEquals($expectedValue, $result);
    }

    /**
     * @return array
     */
    public function isValidHashDataProvider()
    {
        $signatureKey = '3EAFCE5697C1B4B9748385C1FCD29D86F3B9B41C7EED85A3A01DFF6570C8C' .
            '29373C2A153355C3313CDF4AF723C0036DBF244A0821713A910024EE85547CEF37F';
        $expectedSha2Hash = '368D48E0CD1274BF41C059138DA69985594021A4AD5B4C5526AE88C8F' .
            '7C5769B13C5E1E4358900F3E51076FB69D14B0A797904C22E8A11A52AA49CDE5FBB703C';
        return [
            [
                'merchantMd5' => 'FCD7F001E9274FDEFB14BFF91C799306',
                'hashKey' => 'x_MD5_Hash',
                'merchantApiLogin' => 'Magento',
                'amount' => null,
                'transactionId' => '1',
                'hash' => '1F24A4EC9A169B2B2A072A5F168E16DC',
                'expectedValue' => true
            ],
            [
                'merchantMd5' => '8AEF4E508261A287C3E2F544720FCA3A',
                'hashKey' => 'x_MD5_Hash',
                'merchantApiLogin' => 'Magento2',
                'amount' => 100.50,
                'transactionId' => '2',
                'hash' => '1F24A4EC9A169B2B2A072A5F168E16DC',
                'expectedValue' => false
            ],
            [
                'signatureKey' => $signatureKey,
                'hashKey' => 'x_SHA2_Hash',
                'merchantApiLogin' => 'Magento2',
                'amount' => 100.50,
                'transactionId' => '2',
                'hash' => $expectedSha2Hash,
                'expectedValue' => true
            ]
        ];
    }

    /**
     * @param int $xResponseCode
     * @param bool $expectedValue
     * @dataProvider isApprovedDataProvider
     */
    public function testIsApproved($xResponseCode, $expectedValue)
    {
        $this->responseModel->setXResponseCode($xResponseCode);
        $this->assertSame($expectedValue, $this->responseModel->isApproved());
    }

    /**
     * @return array
     */
    public function isApprovedDataProvider()
    {
        return [
            [Directpost::RESPONSE_CODE_APPROVED, true],
            [Directpost::RESPONSE_CODE_DECLINED, false],
            [Directpost::RESPONSE_CODE_ERROR, false],
            [Directpost::RESPONSE_CODE_HELD, false],
        ];
    }
}