CaptureStrategyCommandTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Test\Unit\Gateway\Command;
  7. use Braintree\IsNode;
  8. use Magento\Braintree\Gateway\Command\CaptureStrategyCommand;
  9. use Magento\Braintree\Gateway\SubjectReader;
  10. use Magento\Braintree\Model\Adapter\BraintreeAdapter;
  11. use Magento\Braintree\Model\Adapter\BraintreeAdapterFactory;
  12. use Magento\Braintree\Model\Adapter\BraintreeSearchAdapter;
  13. use Magento\Framework\Api\FilterBuilder;
  14. use Magento\Framework\Api\Search\SearchCriteria;
  15. use Magento\Framework\Api\SearchCriteriaBuilder;
  16. use Magento\Payment\Gateway\Command\CommandPoolInterface;
  17. use Magento\Payment\Gateway\Command\GatewayCommand;
  18. use Magento\Payment\Gateway\Data\OrderAdapterInterface;
  19. use Magento\Payment\Gateway\Data\PaymentDataObject;
  20. use Magento\Sales\Api\TransactionRepositoryInterface;
  21. use Magento\Sales\Model\Order\Payment;
  22. use Magento\Sales\Model\ResourceModel\Order\Payment\Transaction\CollectionFactory;
  23. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  24. /**
  25. * Tests \Magento\Braintree\Gateway\Command\CaptureStrategyCommand.
  26. *
  27. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  28. */
  29. class CaptureStrategyCommandTest extends \PHPUnit\Framework\TestCase
  30. {
  31. /**
  32. * @var CaptureStrategyCommand
  33. */
  34. private $strategyCommand;
  35. /**
  36. * @var CommandPoolInterface|MockObject
  37. */
  38. private $commandPoolMock;
  39. /**
  40. * @var TransactionRepositoryInterface|MockObject
  41. */
  42. private $transactionRepositoryMock;
  43. /**
  44. * @var FilterBuilder|MockObject
  45. */
  46. private $filterBuilderMock;
  47. /**
  48. * @var SearchCriteriaBuilder|MockObject
  49. */
  50. private $searchCriteriaBuilderMock;
  51. /**
  52. * @var Payment|MockObject
  53. */
  54. private $paymentMock;
  55. /**
  56. * @var GatewayCommand|MockObject
  57. */
  58. private $commandMock;
  59. /**
  60. * @var SubjectReader|MockObject
  61. */
  62. private $subjectReaderMock;
  63. /**
  64. * @var BraintreeAdapter|MockObject
  65. */
  66. private $braintreeAdapterMock;
  67. /**
  68. * @var BraintreeSearchAdapter
  69. */
  70. private $braintreeSearchAdapter;
  71. protected function setUp()
  72. {
  73. $this->commandPoolMock = $this->getMockBuilder(CommandPoolInterface::class)
  74. ->disableOriginalConstructor()
  75. ->setMethods(['get', '__wakeup'])
  76. ->getMock();
  77. $this->subjectReaderMock = $this->getMockBuilder(SubjectReader::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $this->initCommandMock();
  81. $this->initTransactionRepositoryMock();
  82. $this->initFilterBuilderMock();
  83. $this->initSearchCriteriaBuilderMock();
  84. $this->braintreeAdapterMock = $this->getMockBuilder(BraintreeAdapter::class)
  85. ->disableOriginalConstructor()
  86. ->getMock();
  87. /** @var BraintreeAdapterFactory|MockObject $adapterFactoryMock */
  88. $adapterFactoryMock = $this->getMockBuilder(BraintreeAdapterFactory::class)
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $adapterFactoryMock->expects(self::any())
  92. ->method('create')
  93. ->willReturn($this->braintreeAdapterMock);
  94. $this->braintreeSearchAdapter = new BraintreeSearchAdapter();
  95. $this->strategyCommand = new CaptureStrategyCommand(
  96. $this->commandPoolMock,
  97. $this->transactionRepositoryMock,
  98. $this->filterBuilderMock,
  99. $this->searchCriteriaBuilderMock,
  100. $this->subjectReaderMock,
  101. $adapterFactoryMock,
  102. $this->braintreeSearchAdapter
  103. );
  104. }
  105. /**
  106. * @covers \Magento\Braintree\Gateway\Command\CaptureStrategyCommand::execute
  107. */
  108. public function testSaleExecute()
  109. {
  110. $paymentData = $this->getPaymentDataObjectMock();
  111. $subject['payment'] = $paymentData;
  112. $this->subjectReaderMock->expects(self::once())
  113. ->method('readPayment')
  114. ->with($subject)
  115. ->willReturn($paymentData);
  116. $this->paymentMock->expects(static::once())
  117. ->method('getAuthorizationTransaction')
  118. ->willReturn(false);
  119. $this->paymentMock->expects(static::once())
  120. ->method('getId')
  121. ->willReturn(1);
  122. $this->buildSearchCriteria();
  123. $this->transactionRepositoryMock->expects(static::once())
  124. ->method('getTotalCount')
  125. ->willReturn(0);
  126. $this->commandPoolMock->expects(static::once())
  127. ->method('get')
  128. ->with(CaptureStrategyCommand::SALE)
  129. ->willReturn($this->commandMock);
  130. $this->strategyCommand->execute($subject);
  131. }
  132. /**
  133. * @covers \Magento\Braintree\Gateway\Command\CaptureStrategyCommand::execute
  134. */
  135. public function testCaptureExecute()
  136. {
  137. $paymentData = $this->getPaymentDataObjectMock();
  138. $subject['payment'] = $paymentData;
  139. $lastTransId = 'txnds';
  140. $this->subjectReaderMock->expects(self::once())
  141. ->method('readPayment')
  142. ->with($subject)
  143. ->willReturn($paymentData);
  144. $this->paymentMock->expects(static::once())
  145. ->method('getAuthorizationTransaction')
  146. ->willReturn(true);
  147. $this->paymentMock->expects(static::once())
  148. ->method('getLastTransId')
  149. ->willReturn($lastTransId);
  150. $this->paymentMock->expects(static::once())
  151. ->method('getId')
  152. ->willReturn(1);
  153. $this->buildSearchCriteria();
  154. $this->transactionRepositoryMock->expects(static::once())
  155. ->method('getTotalCount')
  156. ->willReturn(0);
  157. // authorization transaction was not expired
  158. $collection = $this->getNotExpiredExpectedCollection($lastTransId);
  159. $collection->expects(static::once())
  160. ->method('maximumCount')
  161. ->willReturn(0);
  162. $this->commandPoolMock->expects(static::once())
  163. ->method('get')
  164. ->with(CaptureStrategyCommand::CAPTURE)
  165. ->willReturn($this->commandMock);
  166. $this->strategyCommand->execute($subject);
  167. }
  168. /**
  169. * @param string $lastTransactionId
  170. * @return \Braintree\ResourceCollection|MockObject
  171. */
  172. private function getNotExpiredExpectedCollection($lastTransactionId)
  173. {
  174. $isExpectations = [
  175. 'id' => ['is' => $lastTransactionId],
  176. 'status' => [\Braintree\Transaction::AUTHORIZATION_EXPIRED]
  177. ];
  178. $collection = $this->getMockBuilder(\Braintree\ResourceCollection::class)
  179. ->disableOriginalConstructor()
  180. ->getMock();
  181. $this->braintreeAdapterMock->expects(static::once())
  182. ->method('search')
  183. ->with(
  184. static::callback(
  185. function (array $filters) use ($isExpectations) {
  186. foreach ($filters as $filter) {
  187. /** @var IsNode $filter */
  188. if (!isset($isExpectations[$filter->name])) {
  189. return false;
  190. }
  191. if ($isExpectations[$filter->name] !== $filter->toParam()) {
  192. return false;
  193. }
  194. }
  195. return true;
  196. }
  197. )
  198. )
  199. ->willReturn($collection);
  200. return $collection;
  201. }
  202. /**
  203. * @covers \Magento\Braintree\Gateway\Command\CaptureStrategyCommand::execute
  204. */
  205. public function testExpiredAuthorizationPerformVaultCaptureExecute()
  206. {
  207. $paymentData = $this->getPaymentDataObjectMock();
  208. $subject['payment'] = $paymentData;
  209. $lastTransId = 'txnds';
  210. $this->subjectReaderMock->expects(self::once())
  211. ->method('readPayment')
  212. ->with($subject)
  213. ->willReturn($paymentData);
  214. $this->paymentMock->expects(static::once())
  215. ->method('getAuthorizationTransaction')
  216. ->willReturn(true);
  217. $this->paymentMock->expects(static::once())
  218. ->method('getLastTransId')
  219. ->willReturn($lastTransId);
  220. $this->paymentMock->expects(static::once())
  221. ->method('getId')
  222. ->willReturn(1);
  223. $this->buildSearchCriteria();
  224. $this->transactionRepositoryMock->expects(static::once())
  225. ->method('getTotalCount')
  226. ->willReturn(0);
  227. // authorization transaction was expired
  228. $collection = $this->getNotExpiredExpectedCollection($lastTransId);
  229. $collection->expects(static::once())
  230. ->method('maximumCount')
  231. ->willReturn(1);
  232. $this->commandPoolMock->expects(static::once())
  233. ->method('get')
  234. ->with(CaptureStrategyCommand::VAULT_CAPTURE)
  235. ->willReturn($this->commandMock);
  236. $this->strategyCommand->execute($subject);
  237. }
  238. /**
  239. * @covers \Magento\Braintree\Gateway\Command\CaptureStrategyCommand::execute
  240. */
  241. public function testVaultCaptureExecute()
  242. {
  243. $paymentData = $this->getPaymentDataObjectMock();
  244. $subject['payment'] = $paymentData;
  245. $this->subjectReaderMock->expects(self::once())
  246. ->method('readPayment')
  247. ->with($subject)
  248. ->willReturn($paymentData);
  249. $this->paymentMock->expects(static::once())
  250. ->method('getAuthorizationTransaction')
  251. ->willReturn(true);
  252. $this->paymentMock->expects(static::once())
  253. ->method('getId')
  254. ->willReturn(1);
  255. $this->buildSearchCriteria();
  256. $this->transactionRepositoryMock->expects(static::once())
  257. ->method('getTotalCount')
  258. ->willReturn(1);
  259. $this->commandPoolMock->expects(static::once())
  260. ->method('get')
  261. ->with(CaptureStrategyCommand::VAULT_CAPTURE)
  262. ->willReturn($this->commandMock);
  263. $this->strategyCommand->execute($subject);
  264. }
  265. /**
  266. * Creates mock for payment data object and order payment
  267. * @return MockObject
  268. */
  269. private function getPaymentDataObjectMock()
  270. {
  271. $this->paymentMock = $this->getMockBuilder(Payment::class)
  272. ->disableOriginalConstructor()
  273. ->getMock();
  274. $mock = $this->getMockBuilder(PaymentDataObject::class)
  275. ->setMethods(['getPayment', 'getOrder'])
  276. ->disableOriginalConstructor()
  277. ->getMock();
  278. $mock->expects(static::once())
  279. ->method('getPayment')
  280. ->willReturn($this->paymentMock);
  281. $orderMock = $this->getMockBuilder(OrderAdapterInterface::class)
  282. ->disableOriginalConstructor()
  283. ->getMock();
  284. $mock->method('getOrder')
  285. ->willReturn($orderMock);
  286. return $mock;
  287. }
  288. /**
  289. * Creates mock for gateway command object
  290. */
  291. private function initCommandMock()
  292. {
  293. $this->commandMock = $this->getMockBuilder(GatewayCommand::class)
  294. ->disableOriginalConstructor()
  295. ->setMethods(['execute'])
  296. ->getMock();
  297. $this->commandMock->expects(static::once())
  298. ->method('execute')
  299. ->willReturn([]);
  300. }
  301. /**
  302. * Creates mock for filter object
  303. */
  304. private function initFilterBuilderMock()
  305. {
  306. $this->filterBuilderMock = $this->getMockBuilder(FilterBuilder::class)
  307. ->disableOriginalConstructor()
  308. ->setMethods(['setField', 'setValue', 'create', '__wakeup'])
  309. ->getMock();
  310. }
  311. /**
  312. * Builds search criteria
  313. */
  314. private function buildSearchCriteria()
  315. {
  316. $this->filterBuilderMock->expects(static::exactly(2))
  317. ->method('setField')
  318. ->willReturnSelf();
  319. $this->filterBuilderMock->expects(static::exactly(2))
  320. ->method('setValue')
  321. ->willReturnSelf();
  322. $searchCriteria = new SearchCriteria();
  323. $this->searchCriteriaBuilderMock->expects(static::exactly(2))
  324. ->method('addFilters')
  325. ->willReturnSelf();
  326. $this->searchCriteriaBuilderMock->expects(static::once())
  327. ->method('create')
  328. ->willReturn($searchCriteria);
  329. $this->transactionRepositoryMock->expects(static::once())
  330. ->method('getList')
  331. ->with($searchCriteria)
  332. ->willReturnSelf();
  333. }
  334. /**
  335. * Create mock for search criteria object
  336. */
  337. private function initSearchCriteriaBuilderMock()
  338. {
  339. $this->searchCriteriaBuilderMock = $this->getMockBuilder(SearchCriteriaBuilder::class)
  340. ->disableOriginalConstructor()
  341. ->setMethods(['addFilters', 'create', '__wakeup'])
  342. ->getMock();
  343. }
  344. /**
  345. * Create mock for transaction repository
  346. */
  347. private function initTransactionRepositoryMock()
  348. {
  349. $this->transactionRepositoryMock = $this->getMockBuilder(TransactionRepositoryInterface::class)
  350. ->disableOriginalConstructor()
  351. ->setMethods(['getList', 'getTotalCount', 'delete', 'get', 'save', 'create', '__wakeup'])
  352. ->getMock();
  353. }
  354. }