PaymentResponseHandlerTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\AuthorizenetAcceptjs\Test\Unit\Gateway\Response;
  8. use Magento\AuthorizenetAcceptjs\Gateway\Response\PaymentResponseHandler;
  9. use Magento\AuthorizenetAcceptjs\Gateway\SubjectReader;
  10. use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
  11. use Magento\Sales\Model\Order\Payment;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. use PHPUnit\Framework\TestCase;
  14. class PaymentResponseHandlerTest extends TestCase
  15. {
  16. private const RESPONSE_CODE_APPROVED = 1;
  17. private const RESPONSE_CODE_HELD = 4;
  18. /**
  19. * @var PaymentResponseHandler
  20. */
  21. private $builder;
  22. /**
  23. * @var Payment|MockObject
  24. */
  25. private $paymentMock;
  26. /**
  27. * @var Payment|MockObject
  28. */
  29. private $paymentDOMock;
  30. protected function setUp()
  31. {
  32. $this->paymentDOMock = $this->createMock(PaymentDataObjectInterface::class);
  33. $this->paymentMock = $this->createMock(Payment::class);
  34. $this->paymentDOMock->method('getPayment')
  35. ->willReturn($this->paymentMock);
  36. $this->builder = new PaymentResponseHandler(new SubjectReader());
  37. }
  38. public function testHandleDefaultResponse()
  39. {
  40. $this->paymentMock->method('getAdditionalInformation')
  41. ->with('ccLast4')
  42. ->willReturn('1234');
  43. // Assert the avs code is saved
  44. $this->paymentMock->expects($this->once())
  45. ->method('setCcAvsStatus')
  46. ->with('avshurray');
  47. $this->paymentMock->expects($this->once())
  48. ->method('setCcLast4')
  49. ->with('1234');
  50. $this->paymentMock->expects($this->once())
  51. ->method('setIsTransactionClosed')
  52. ->with(false);
  53. $response = [
  54. 'transactionResponse' => [
  55. 'avsResultCode' => 'avshurray',
  56. 'responseCode' => self::RESPONSE_CODE_APPROVED,
  57. ]
  58. ];
  59. $subject = [
  60. 'payment' => $this->paymentDOMock
  61. ];
  62. $this->builder->handle($subject, $response);
  63. // Assertions are part of mocking above
  64. }
  65. public function testHandleHeldResponse()
  66. {
  67. // Assert the avs code is saved
  68. $this->paymentMock->expects($this->once())
  69. ->method('setCcAvsStatus')
  70. ->with('avshurray');
  71. $this->paymentMock->expects($this->once())
  72. ->method('setIsTransactionClosed')
  73. ->with(false);
  74. // opaque data wasn't provided
  75. $this->paymentMock->expects($this->never())
  76. ->method('setAdditionalInformation');
  77. // Assert the payment is flagged for review
  78. $this->paymentMock->expects($this->once())
  79. ->method('setIsTransactionPending')
  80. ->with(true)
  81. ->willReturnSelf();
  82. $this->paymentMock->expects($this->once())
  83. ->method('setIsFraudDetected')
  84. ->with(true);
  85. $response = [
  86. 'transactionResponse' => [
  87. 'avsResultCode' => 'avshurray',
  88. 'responseCode' => self::RESPONSE_CODE_HELD,
  89. ]
  90. ];
  91. $subject = [
  92. 'payment' => $this->paymentDOMock
  93. ];
  94. $this->builder->handle($subject, $response);
  95. // Assertions are part of mocking above
  96. }
  97. }