RefundTransactionStrategyCommandTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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\Command;
  8. use Magento\AuthorizenetAcceptjs\Gateway\Command\RefundTransactionStrategyCommand;
  9. use Magento\AuthorizenetAcceptjs\Gateway\SubjectReader;
  10. use Magento\Payment\Gateway\Command\CommandPoolInterface;
  11. use Magento\Payment\Gateway\Command\ResultInterface;
  12. use Magento\Payment\Gateway\CommandInterface;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. use PHPUnit\Framework\TestCase;
  15. class RefundTransactionStrategyCommandTest extends TestCase
  16. {
  17. /**
  18. * @var CommandInterface|MockObject
  19. */
  20. private $commandMock;
  21. /**
  22. * @var CommandInterface|MockObject
  23. */
  24. private $transactionDetailsCommandMock;
  25. /**
  26. * @var CommandPoolInterface|MockObject
  27. */
  28. private $commandPoolMock;
  29. /**
  30. * @var RefundTransactionStrategyCommand
  31. */
  32. private $command;
  33. /**
  34. * @var ResultInterface|MockObject
  35. */
  36. private $transactionResultMock;
  37. protected function setUp()
  38. {
  39. $this->transactionDetailsCommandMock = $this->createMock(CommandInterface::class);
  40. $this->commandMock = $this->createMock(CommandInterface::class);
  41. $this->transactionResultMock = $this->createMock(ResultInterface::class);
  42. $this->commandPoolMock = $this->createMock(CommandPoolInterface::class);
  43. $this->command = new RefundTransactionStrategyCommand(
  44. $this->commandPoolMock,
  45. new SubjectReader()
  46. );
  47. }
  48. public function testCommandWillVoidWhenTransactionIsPendingSettlement()
  49. {
  50. // Assert command is executed
  51. $this->commandMock->expects($this->once())
  52. ->method('execute');
  53. $this->commandPoolMock->method('get')
  54. ->willReturnMap([
  55. ['get_transaction_details', $this->transactionDetailsCommandMock],
  56. ['void', $this->commandMock]
  57. ]);
  58. $this->transactionResultMock->method('get')
  59. ->willReturn([
  60. 'transaction' => [
  61. 'transactionStatus' => 'capturedPendingSettlement'
  62. ]
  63. ]);
  64. $buildSubject = [
  65. 'foo' => '123'
  66. ];
  67. $this->transactionDetailsCommandMock->expects($this->once())
  68. ->method('execute')
  69. ->with($buildSubject)
  70. ->willReturn($this->transactionResultMock);
  71. $this->command->execute($buildSubject);
  72. }
  73. public function testCommandWillRefundWhenTransactionIsSettled()
  74. {
  75. // Assert command is executed
  76. $this->commandMock->expects($this->once())
  77. ->method('execute');
  78. $this->commandPoolMock->method('get')
  79. ->willReturnMap([
  80. ['get_transaction_details', $this->transactionDetailsCommandMock],
  81. ['refund_settled', $this->commandMock]
  82. ]);
  83. $this->transactionResultMock->method('get')
  84. ->willReturn([
  85. 'transaction' => [
  86. 'transactionStatus' => 'settledSuccessfully'
  87. ]
  88. ]);
  89. $buildSubject = [
  90. 'foo' => '123'
  91. ];
  92. $this->transactionDetailsCommandMock->expects($this->once())
  93. ->method('execute')
  94. ->with($buildSubject)
  95. ->willReturn($this->transactionResultMock);
  96. $this->command->execute($buildSubject);
  97. }
  98. /**
  99. * @expectedException \Magento\Payment\Gateway\Command\CommandException
  100. * @expectedExceptionMessage This transaction cannot be refunded with its current status.
  101. */
  102. public function testCommandWillThrowExceptionWhenTransactionIsInInvalidState()
  103. {
  104. // Assert command is never executed
  105. $this->commandMock->expects($this->never())
  106. ->method('execute');
  107. $this->commandPoolMock->method('get')
  108. ->willReturnMap([
  109. ['get_transaction_details', $this->transactionDetailsCommandMock],
  110. ]);
  111. $this->transactionResultMock->method('get')
  112. ->willReturn([
  113. 'transaction' => [
  114. 'transactionStatus' => 'somethingIsWrong'
  115. ]
  116. ]);
  117. $buildSubject = [
  118. 'foo' => '123'
  119. ];
  120. $this->transactionDetailsCommandMock->expects($this->once())
  121. ->method('execute')
  122. ->with($buildSubject)
  123. ->willReturn($this->transactionResultMock);
  124. $this->command->execute($buildSubject);
  125. }
  126. }