ResponseTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Authorizenet\Test\Unit\Model\Directpost;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Authorizenet\Model\Directpost;
  9. class ResponseTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Authorizenet\Model\Directpost\Response
  13. */
  14. private $responseModel;
  15. protected function setUp()
  16. {
  17. $objectManager = new ObjectManager($this);
  18. $this->responseModel = $objectManager->getObject(
  19. \Magento\Authorizenet\Model\Directpost\Response::class
  20. );
  21. }
  22. /**
  23. * @param $merchantMd5
  24. * @param $merchantApiLogin
  25. * @param $amount
  26. * @param $transactionId
  27. * @return string
  28. */
  29. protected function generateHash($merchantMd5, $merchantApiLogin, $amount, $transactionId)
  30. {
  31. return strtoupper(md5($merchantMd5 . $merchantApiLogin . $transactionId . $amount));
  32. }
  33. /**
  34. * @param string $storedHash
  35. * @param string $hashKey
  36. * @param string $merchantApiLogin
  37. * @param float|null $amount
  38. * @param string $transactionId
  39. * @param string $hash
  40. * @param bool $expectedValue
  41. * @dataProvider isValidHashDataProvider
  42. */
  43. public function testIsValidHash(
  44. string $storedHash,
  45. string $hashKey,
  46. string $merchantApiLogin,
  47. $amount,
  48. string $transactionId,
  49. string $hash,
  50. bool $expectedValue
  51. ) {
  52. $this->responseModel->setXAmount($amount);
  53. $this->responseModel->setXTransId($transactionId);
  54. $this->responseModel->setData($hashKey, $hash);
  55. $result = $this->responseModel->isValidHash($storedHash, $merchantApiLogin);
  56. $this->assertEquals($expectedValue, $result);
  57. }
  58. /**
  59. * @return array
  60. */
  61. public function isValidHashDataProvider()
  62. {
  63. $signatureKey = '3EAFCE5697C1B4B9748385C1FCD29D86F3B9B41C7EED85A3A01DFF6570C8C' .
  64. '29373C2A153355C3313CDF4AF723C0036DBF244A0821713A910024EE85547CEF37F';
  65. $expectedSha2Hash = '368D48E0CD1274BF41C059138DA69985594021A4AD5B4C5526AE88C8F' .
  66. '7C5769B13C5E1E4358900F3E51076FB69D14B0A797904C22E8A11A52AA49CDE5FBB703C';
  67. return [
  68. [
  69. 'merchantMd5' => 'FCD7F001E9274FDEFB14BFF91C799306',
  70. 'hashKey' => 'x_MD5_Hash',
  71. 'merchantApiLogin' => 'Magento',
  72. 'amount' => null,
  73. 'transactionId' => '1',
  74. 'hash' => '1F24A4EC9A169B2B2A072A5F168E16DC',
  75. 'expectedValue' => true
  76. ],
  77. [
  78. 'merchantMd5' => '8AEF4E508261A287C3E2F544720FCA3A',
  79. 'hashKey' => 'x_MD5_Hash',
  80. 'merchantApiLogin' => 'Magento2',
  81. 'amount' => 100.50,
  82. 'transactionId' => '2',
  83. 'hash' => '1F24A4EC9A169B2B2A072A5F168E16DC',
  84. 'expectedValue' => false
  85. ],
  86. [
  87. 'signatureKey' => $signatureKey,
  88. 'hashKey' => 'x_SHA2_Hash',
  89. 'merchantApiLogin' => 'Magento2',
  90. 'amount' => 100.50,
  91. 'transactionId' => '2',
  92. 'hash' => $expectedSha2Hash,
  93. 'expectedValue' => true
  94. ]
  95. ];
  96. }
  97. /**
  98. * @param int $xResponseCode
  99. * @param bool $expectedValue
  100. * @dataProvider isApprovedDataProvider
  101. */
  102. public function testIsApproved($xResponseCode, $expectedValue)
  103. {
  104. $this->responseModel->setXResponseCode($xResponseCode);
  105. $this->assertSame($expectedValue, $this->responseModel->isApproved());
  106. }
  107. /**
  108. * @return array
  109. */
  110. public function isApprovedDataProvider()
  111. {
  112. return [
  113. [Directpost::RESPONSE_CODE_APPROVED, true],
  114. [Directpost::RESPONSE_CODE_DECLINED, false],
  115. [Directpost::RESPONSE_CODE_ERROR, false],
  116. [Directpost::RESPONSE_CODE_HELD, false],
  117. ];
  118. }
  119. }