CaptureStrategyCommandTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\CaptureStrategyCommand;
  9. use Magento\AuthorizenetAcceptjs\Gateway\SubjectReader;
  10. use Magento\Framework\Api\FilterBuilder;
  11. use Magento\Framework\Api\Search\SearchCriteria;
  12. use Magento\Framework\Api\SearchCriteriaBuilder;
  13. use Magento\Payment\Gateway\Command\CommandPoolInterface;
  14. use Magento\Payment\Gateway\Command\GatewayCommand;
  15. use Magento\Payment\Gateway\Data\PaymentDataObject;
  16. use Magento\Sales\Api\Data\TransactionSearchResultInterface;
  17. use Magento\Sales\Api\TransactionRepositoryInterface;
  18. use Magento\Sales\Model\Order\Payment;
  19. use PHPUnit\Framework\MockObject\MockObject;
  20. use PHPUnit\Framework\TestCase;
  21. class CaptureStrategyCommandTest extends TestCase
  22. {
  23. /**
  24. * @var CaptureStrategyCommand
  25. */
  26. private $strategyCommand;
  27. /**
  28. * @var CommandPoolInterface|MockObject
  29. */
  30. private $commandPoolMock;
  31. /**
  32. * @var TransactionRepositoryInterface|MockObject
  33. */
  34. private $transactionRepositoryMock;
  35. /**
  36. * @var FilterBuilder|MockObject
  37. */
  38. private $filterBuilderMock;
  39. /**
  40. * @var SearchCriteriaBuilder|MockObject
  41. */
  42. private $searchCriteriaBuilderMock;
  43. /**
  44. * @var Payment|MockObject
  45. */
  46. private $paymentMock;
  47. /**
  48. * @var PaymentDataObject|MockObject
  49. */
  50. private $paymentDOMock;
  51. /**
  52. * @var GatewayCommand|MockObject
  53. */
  54. private $commandMock;
  55. /**
  56. * @var TransactionSearchResultInterface|MockObject
  57. */
  58. private $transactionsResult;
  59. protected function setUp()
  60. {
  61. // Simple mocks
  62. $this->paymentDOMock = $this->createMock(PaymentDataObject::class);
  63. $this->paymentMock = $this->createMock(Payment::class);
  64. $this->paymentDOMock->method('getPayment')
  65. ->willReturn($this->paymentMock);
  66. $this->commandMock = $this->createMock(GatewayCommand::class);
  67. $this->commandPoolMock = $this->createMock(CommandPoolInterface::class);
  68. $this->searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class);
  69. $this->transactionRepositoryMock = $this->createMock(TransactionRepositoryInterface::class);
  70. // The search criteria builder should return the criteria with the specified filters
  71. $this->filterBuilderMock = $this->createMock(FilterBuilder::class);
  72. // We aren't coupling the implementation to the test. The test only cares how the result is processed
  73. $this->filterBuilderMock->method('setField')
  74. ->willReturnSelf();
  75. $this->filterBuilderMock->method('setValue')
  76. ->willReturnSelf();
  77. $searchCriteria = new SearchCriteria();
  78. $this->searchCriteriaBuilderMock->method('addFilters')
  79. ->willReturnSelf();
  80. $this->searchCriteriaBuilderMock->method('create')
  81. ->willReturn($searchCriteria);
  82. // The transaction result can be customized per test to simulate different scenarios
  83. $this->transactionsResult = $this->createMock(TransactionSearchResultInterface::class);
  84. $this->transactionRepositoryMock->method('getList')
  85. ->with($searchCriteria)
  86. ->willReturn($this->transactionsResult);
  87. $this->strategyCommand = new CaptureStrategyCommand(
  88. $this->commandPoolMock,
  89. $this->transactionRepositoryMock,
  90. $this->filterBuilderMock,
  91. $this->searchCriteriaBuilderMock,
  92. new SubjectReader()
  93. );
  94. }
  95. public function testExecuteWillAuthorizeWhenNotAuthorizedAndNotCaptured()
  96. {
  97. $subject = ['payment' => $this->paymentDOMock];
  98. // Hasn't been authorized
  99. $this->paymentMock->method('getAuthorizationTransaction')
  100. ->willReturn(false);
  101. // Hasn't been captured
  102. $this->transactionsResult->method('getTotalCount')
  103. ->willReturn(0);
  104. // Assert authorize command was used
  105. $this->commandPoolMock->expects($this->once())
  106. ->method('get')
  107. ->with('sale')
  108. ->willReturn($this->commandMock);
  109. // Assert execute was called and with correct data
  110. $this->commandMock->expects($this->once())
  111. ->method('execute')
  112. ->with($subject);
  113. $this->strategyCommand->execute($subject);
  114. // Assertions are performed via mock expects above
  115. }
  116. public function testExecuteWillAuthorizeAndCaptureWhenAlreadyCaptured()
  117. {
  118. $subject = ['payment' => $this->paymentDOMock];
  119. // Already authorized
  120. $this->paymentMock->method('getAuthorizationTransaction')
  121. ->willReturn(true);
  122. // And already captured
  123. $this->transactionsResult->method('getTotalCount')
  124. ->willReturn(1);
  125. // Assert authorize command was used
  126. $this->commandPoolMock->expects($this->once())
  127. ->method('get')
  128. ->with('settle')
  129. ->willReturn($this->commandMock);
  130. // Assert execute was called and with correct data
  131. $this->commandMock->expects($this->once())
  132. ->method('execute')
  133. ->with($subject);
  134. $this->strategyCommand->execute($subject);
  135. // Assertions are performed via mock expects above
  136. }
  137. public function testExecuteWillCaptureWhenAlreadyAuthorizedButNotCaptured()
  138. {
  139. $subject = ['payment' => $this->paymentDOMock];
  140. // Was already authorized
  141. $this->paymentMock->method('getAuthorizationTransaction')
  142. ->willReturn(true);
  143. // But, hasn't been captured
  144. $this->transactionsResult->method('getTotalCount')
  145. ->willReturn(0);
  146. // Assert authorize command was used
  147. $this->commandPoolMock->expects($this->once())
  148. ->method('get')
  149. ->with('settle')
  150. ->willReturn($this->commandMock);
  151. // Assert execute was called and with correct data
  152. $this->commandMock->expects($this->once())
  153. ->method('execute')
  154. ->with($subject);
  155. $this->strategyCommand->execute($subject);
  156. // Assertions are performed via mock expects above
  157. }
  158. }