CaptureStrategyCommandTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Payment\Test\Unit\Gateway\Command;
  17. use Amazon\Payment\Gateway\Command\CaptureStrategyCommand;
  18. use Amazon\Core\Helper\Data;
  19. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  20. use Magento\Framework\Api\SearchCriteriaBuilder;
  21. use Magento\Framework\Api\FilterBuilder;
  22. use Magento\Framework\Api\Search\SearchCriteria;
  23. use Magento\Payment\Gateway\Command\CommandPoolInterface;
  24. use Magento\Payment\Gateway\Command\GatewayCommand;
  25. use Magento\Payment\Gateway\Data\OrderAdapterInterface;
  26. use Magento\Payment\Gateway\Data\PaymentDataObject;
  27. use Magento\Sales\Api\TransactionRepositoryInterface;
  28. use Magento\Sales\Model\Order\Payment;
  29. use Magento\Sales\Model\ResourceModel\Order\Payment\Transaction\CollectionFactory;
  30. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  31. /**
  32. * Class CaptureStrategyCommandTest
  33. *
  34. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  35. */
  36. class CaptureStrategyCommandTest extends \PHPUnit\Framework\TestCase
  37. {
  38. /**
  39. * @var CaptureStrategyCommand
  40. */
  41. private $strategyCommand;
  42. /**
  43. * @var CommandPoolInterface|MockObject
  44. */
  45. private $commandPool;
  46. /**
  47. * @var TransactionRepositoryInterface|MockObject
  48. */
  49. private $transactionRepository;
  50. /**
  51. * @var FilterBuilder|MockObject
  52. */
  53. private $filterBuilder;
  54. /**
  55. * @var SearchCriteriaBuilder|MockObject
  56. */
  57. private $searchCriteriaBuilder;
  58. /**
  59. * @var Payment|MockObject
  60. */
  61. private $payment;
  62. /**
  63. * @var GatewayCommand|MockObject
  64. */
  65. private $command;
  66. /**
  67. * @var Data|MockObject
  68. */
  69. private $coreHelper;
  70. /**
  71. * Sets up base classes needed to mock the command strategy class
  72. */
  73. protected function setUp()
  74. {
  75. $this->commandPool = $this->getMockBuilder(CommandPoolInterface::class)
  76. ->disableOriginalConstructor()
  77. ->setMethods(['get', '__wakeup'])
  78. ->getMock();
  79. $this->initCommandMock();
  80. $this->initTransactionRepositoryMock();
  81. $this->initFilterBuilderMock();
  82. $this->initSearchCriteriaBuilderMock();
  83. $this->coreHelper = $this->getMockBuilder(\Amazon\Core\Helper\Data::class)
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $this->strategyCommand = new CaptureStrategyCommand(
  87. $this->commandPool,
  88. $this->transactionRepository,
  89. $this->searchCriteriaBuilder,
  90. $this->filterBuilder,
  91. $this->coreHelper
  92. );
  93. }
  94. /**
  95. * Tests if command strategy class returns correct command value when item is authorized but not captured
  96. * @throws \Magento\Payment\Gateway\Command\CommandException
  97. */
  98. public function testSaleExecute()
  99. {
  100. $paymentData = $this->getPaymentDataObjectMock();
  101. $subject['payment'] = $paymentData;
  102. $this->payment->method('getAuthorizationTransaction')
  103. ->willReturn(false);
  104. $this->payment->method('getId')
  105. ->willReturn(1);
  106. $this->coreHelper->method('getPaymentAction')->willReturn('authorize_capture');
  107. $this->buildSearchCriteria();
  108. $this->transactionRepository->method('getTotalCount')
  109. ->willReturn(0);
  110. $this->commandPool->method('get')
  111. ->with(CaptureStrategyCommand::SALE)
  112. ->willReturn($this->command);
  113. $this->strategyCommand->execute($subject);
  114. }
  115. /**
  116. * Tests if command strategy class returns correct command value when item is to be authorized and captured
  117. * @throws \Magento\Payment\Gateway\Command\CommandException
  118. */
  119. public function testCaptureExecute()
  120. {
  121. $paymentData = $this->getPaymentDataObjectMock();
  122. $subject['payment'] = $paymentData;
  123. $lastTransId = 'transaction_id';
  124. $this->payment->method('getAuthorizationTransaction')
  125. ->willReturn(true);
  126. $this->payment->method('getLastTransId')
  127. ->willReturn($lastTransId);
  128. $this->payment->method('getId')
  129. ->willReturn(1);
  130. $this->buildSearchCriteria();
  131. $this->transactionRepository->method('getTotalCount')
  132. ->willReturn(0);
  133. $this->commandPool->method('get')
  134. ->with(CaptureStrategyCommand::CAPTURE)
  135. ->willReturn($this->command);
  136. $this->strategyCommand->execute($subject);
  137. }
  138. /**
  139. * Creates mock for payment data object and order payment
  140. * @return MockObject
  141. */
  142. private function getPaymentDataObjectMock()
  143. {
  144. $this->payment = $this->getMockBuilder(Payment::class)
  145. ->disableOriginalConstructor()
  146. ->getMock();
  147. $mock = $this->getMockBuilder(PaymentDataObject::class)
  148. ->setMethods(['getPayment', 'getOrder'])
  149. ->disableOriginalConstructor()
  150. ->getMock();
  151. $mock->method('getPayment')
  152. ->willReturn($this->payment);
  153. $order = $this->getMockBuilder(OrderAdapterInterface::class)
  154. ->disableOriginalConstructor()
  155. ->getMock();
  156. $mock->method('getOrder')
  157. ->willReturn($order);
  158. return $mock;
  159. }
  160. /**
  161. * Creates mock for gateway command object
  162. */
  163. private function initCommandMock()
  164. {
  165. $this->command = $this->getMockBuilder(GatewayCommand::class)
  166. ->disableOriginalConstructor()
  167. ->setMethods(['execute'])
  168. ->getMock();
  169. $this->command->method('execute')
  170. ->willReturn([]);
  171. }
  172. /**
  173. * Creates mock for filter object
  174. */
  175. private function initFilterBuilderMock()
  176. {
  177. $this->filterBuilder = $this->getMockBuilder(FilterBuilder::class)
  178. ->disableOriginalConstructor()
  179. ->setMethods(['setField', 'setValue', 'create', '__wakeup'])
  180. ->getMock();
  181. }
  182. /**
  183. * Builds search criteria
  184. */
  185. private function buildSearchCriteria()
  186. {
  187. $this->filterBuilder->expects(self::exactly(2))
  188. ->method('setField')
  189. ->willReturnSelf();
  190. $this->filterBuilder->expects(self::exactly(2))
  191. ->method('setValue')
  192. ->willReturnSelf();
  193. $searchCriteria = new SearchCriteria();
  194. $this->searchCriteriaBuilder->expects(self::exactly(2))
  195. ->method('addFilters')
  196. ->willReturnSelf();
  197. $this->searchCriteriaBuilder->method('create')
  198. ->willReturn($searchCriteria);
  199. $this->transactionRepository->method('getList')
  200. ->with($searchCriteria)
  201. ->willReturnSelf();
  202. }
  203. /**
  204. * Create mock for search criteria object
  205. */
  206. private function initSearchCriteriaBuilderMock()
  207. {
  208. $this->searchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class)
  209. ->disableOriginalConstructor()
  210. ->setMethods(['addFilters', 'create', '__wakeup'])
  211. ->getMock();
  212. }
  213. /**
  214. * Create mock for transaction repository
  215. */
  216. private function initTransactionRepositoryMock()
  217. {
  218. $this->transactionRepository = $this->getMockBuilder(TransactionRepositoryInterface::class)
  219. ->disableOriginalConstructor()
  220. ->setMethods(['getList', 'getTotalCount', 'delete', 'get', 'save', 'create', '__wakeup'])
  221. ->getMock();
  222. }
  223. }