PayOperationTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Model\Order\Invoice;
  7. /**
  8. * Unit test for Invoice pay operation.
  9. */
  10. class PayOperationTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Sales\Model\Order\Invoice\PayOperation
  14. */
  15. private $subject;
  16. /**
  17. * @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $orderMock;
  20. /**
  21. * @var \Magento\Sales\Api\Data\InvoiceInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $invoiceMock;
  24. /**
  25. * @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $contextMock;
  28. /**
  29. * @var \Magento\Sales\Api\Data\InvoiceItemInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $invoiceItemMock;
  32. /**
  33. * @var \Magento\Sales\Api\Data\OrderPaymentInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $orderPaymentMock;
  36. /**
  37. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $eventManagerMock;
  40. /**
  41. * @var \Magento\Payment\Model\MethodInterface|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $paymentMethodMock;
  44. /**
  45. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  46. */
  47. protected function setUp()
  48. {
  49. $this->orderMock = $this->getMockForAbstractClass(
  50. \Magento\Sales\Api\Data\OrderInterface::class,
  51. [],
  52. '',
  53. false,
  54. false,
  55. true,
  56. [
  57. 'getPayment',
  58. 'setTotalInvoiced',
  59. 'getTotalInvoiced',
  60. 'setBaseTotalInvoiced',
  61. 'getBaseTotalInvoiced',
  62. 'setSubtotalInvoiced',
  63. 'getSubtotalInvoiced',
  64. 'setBaseSubtotalInvoiced',
  65. 'getBaseSubtotalInvoiced',
  66. 'setTaxInvoiced',
  67. 'getTaxInvoiced',
  68. 'setBaseTaxInvoiced',
  69. 'getBaseTaxInvoiced',
  70. 'setDiscountTaxCompensationInvoiced',
  71. 'getDiscountTaxCompensationInvoiced',
  72. 'setBaseDiscountTaxCompensationInvoiced',
  73. 'getBaseDiscountTaxCompensationInvoiced',
  74. 'setShippingTaxInvoiced',
  75. 'getShippingTaxInvoiced',
  76. 'setBaseShippingTaxInvoiced',
  77. 'getBaseShippingTaxInvoiced',
  78. 'setShippingInvoiced',
  79. 'getShippingInvoiced',
  80. 'setBaseShippingInvoiced',
  81. 'getBaseShippingInvoiced',
  82. 'setDiscountInvoiced',
  83. 'getDiscountInvoiced',
  84. 'setBaseDiscountInvoiced',
  85. 'getBaseDiscountInvoiced',
  86. 'setBaseTotalInvoicedCost',
  87. 'getBaseTotalInvoicedCost',
  88. ]
  89. );
  90. $this->orderMock->expects($this->any())
  91. ->method('getTotalInvoiced')
  92. ->willReturn(43);
  93. $this->orderMock->expects($this->any())
  94. ->method('getBaseTotalInvoiced')
  95. ->willReturn(43);
  96. $this->orderMock->expects($this->any())
  97. ->method('getSubtotalInvoiced')
  98. ->willReturn(22);
  99. $this->orderMock->expects($this->any())
  100. ->method('getBaseSubtotalInvoiced')
  101. ->willReturn(22);
  102. $this->orderMock->expects($this->any())
  103. ->method('getTaxInvoiced')
  104. ->willReturn(15);
  105. $this->orderMock->expects($this->any())
  106. ->method('getBaseTaxInvoiced')
  107. ->willReturn(15);
  108. $this->orderMock->expects($this->any())
  109. ->method('getDiscountTaxCompensationInvoiced')
  110. ->willReturn(11);
  111. $this->orderMock->expects($this->any())
  112. ->method('getBaseDiscountTaxCompensationInvoiced')
  113. ->willReturn(11);
  114. $this->orderMock->expects($this->any())
  115. ->method('getShippingTaxInvoiced')
  116. ->willReturn(12);
  117. $this->orderMock->expects($this->any())
  118. ->method('getBaseShippingTaxInvoiced')
  119. ->willReturn(12);
  120. $this->orderMock->expects($this->any())
  121. ->method('getShippingInvoiced')
  122. ->willReturn(28);
  123. $this->orderMock->expects($this->any())
  124. ->method('getBaseShippingInvoiced')
  125. ->willReturn(28);
  126. $this->orderMock->expects($this->any())
  127. ->method('getDiscountInvoiced')
  128. ->willReturn(19);
  129. $this->orderMock->expects($this->any())
  130. ->method('getBaseDiscountInvoiced')
  131. ->willReturn(19);
  132. $this->orderMock->expects($this->any())
  133. ->method('getBaseTotalInvoicedCost')
  134. ->willReturn(31);
  135. $this->invoiceMock = $this->getMockForAbstractClass(
  136. \Magento\Sales\Api\Data\InvoiceInterface::class,
  137. [],
  138. '',
  139. false,
  140. false,
  141. true,
  142. [
  143. 'getItems',
  144. 'getState',
  145. 'capture',
  146. 'setCanVoidFlag',
  147. 'pay',
  148. 'getGrandTotal',
  149. 'getBaseGrandTotal',
  150. 'getSubtotal',
  151. 'getBaseSubtotal',
  152. 'getTaxAmount',
  153. 'getBaseTaxAmount',
  154. 'getDiscountTaxCompensationAmount',
  155. 'getBaseDiscountTaxCompensationAmount',
  156. 'getShippingTaxAmount',
  157. 'getBaseShippingTaxAmount',
  158. 'getShippingAmount',
  159. 'getBaseShippingAmount',
  160. 'getDiscountAmount',
  161. 'getBaseDiscountAmount',
  162. 'getBaseCost',
  163. ]
  164. );
  165. $this->invoiceMock->expects($this->any())
  166. ->method('getGrandTotal')
  167. ->willReturn(43);
  168. $this->invoiceMock->expects($this->any())
  169. ->method('getBaseGrandTotal')
  170. ->willReturn(43);
  171. $this->invoiceMock->expects($this->any())
  172. ->method('getSubtotal')
  173. ->willReturn(22);
  174. $this->invoiceMock->expects($this->any())
  175. ->method('getBaseSubtotal')
  176. ->willReturn(22);
  177. $this->invoiceMock->expects($this->any())
  178. ->method('getTaxAmount')
  179. ->willReturn(15);
  180. $this->invoiceMock->expects($this->any())
  181. ->method('getBaseTaxAmount')
  182. ->willReturn(15);
  183. $this->invoiceMock->expects($this->any())
  184. ->method('getDiscountTaxCompensationAmount')
  185. ->willReturn(11);
  186. $this->invoiceMock->expects($this->any())
  187. ->method('getBaseDiscountTaxCompensationAmount')
  188. ->willReturn(11);
  189. $this->invoiceMock->expects($this->any())
  190. ->method('getShippingTaxAmount')
  191. ->willReturn(12);
  192. $this->invoiceMock->expects($this->any())
  193. ->method('getBaseShippingTaxAmount')
  194. ->willReturn(12);
  195. $this->invoiceMock->expects($this->any())
  196. ->method('getShippingAmount')
  197. ->willReturn(28);
  198. $this->invoiceMock->expects($this->any())
  199. ->method('getBaseShippingAmount')
  200. ->willReturn(28);
  201. $this->invoiceMock->expects($this->any())
  202. ->method('getDiscountAmount')
  203. ->willReturn(19);
  204. $this->invoiceMock->expects($this->any())
  205. ->method('getBaseDiscountAmount')
  206. ->willReturn(19);
  207. $this->invoiceMock->expects($this->any())
  208. ->method('getBaseCost')
  209. ->willReturn(31);
  210. $this->contextMock = $this->createMock(\Magento\Framework\Model\Context::class);
  211. $this->invoiceItemMock = $this->getMockForAbstractClass(
  212. \Magento\Sales\Api\Data\InvoiceItemInterface::class,
  213. [],
  214. '',
  215. false,
  216. false,
  217. true,
  218. [
  219. 'isDeleted',
  220. 'register',
  221. ]
  222. );
  223. $this->invoiceItemMock->expects($this->any())
  224. ->method('isDeleted')
  225. ->willReturn(false);
  226. $this->invoiceItemMock->expects($this->any())
  227. ->method('getQty')
  228. ->willReturn(1);
  229. $this->orderPaymentMock = $this->getMockForAbstractClass(
  230. \Magento\Sales\Api\Data\OrderPaymentInterface::class,
  231. [],
  232. '',
  233. false,
  234. false,
  235. true,
  236. [
  237. 'canCapture',
  238. 'getMethodInstance',
  239. 'getIsTransactionPending',
  240. ]
  241. );
  242. $this->orderMock->expects($this->any())
  243. ->method('getPayment')
  244. ->willReturn($this->orderPaymentMock);
  245. $this->eventManagerMock = $this->getMockForAbstractClass(
  246. \Magento\Framework\Event\ManagerInterface::class,
  247. [],
  248. '',
  249. false,
  250. false,
  251. true,
  252. []
  253. );
  254. $this->contextMock->expects($this->any())
  255. ->method('getEventDispatcher')
  256. ->willReturn($this->eventManagerMock);
  257. $this->paymentMethodMock = $this->getMockForAbstractClass(
  258. \Magento\Payment\Model\MethodInterface::class,
  259. [],
  260. '',
  261. false,
  262. false,
  263. true,
  264. []
  265. );
  266. $this->orderPaymentMock->expects($this->any())
  267. ->method('getMethodInstance')
  268. ->willReturn($this->paymentMethodMock);
  269. $this->subject = new \Magento\Sales\Model\Order\Invoice\PayOperation(
  270. $this->contextMock
  271. );
  272. }
  273. /**
  274. * @param bool|null $canCapture
  275. * @param bool|null $isOnline
  276. * @param bool|null $isGateway
  277. * @param bool|null $isTransactionPending
  278. *
  279. * @dataProvider payDataProvider
  280. *
  281. * @return void
  282. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  283. */
  284. public function testExecute($canCapture, $isOnline, $isGateway, $isTransactionPending)
  285. {
  286. $this->invoiceMock->expects($this->any())
  287. ->method('getItems')
  288. ->willReturn([$this->invoiceItemMock]);
  289. if ($canCapture) {
  290. $this->invoiceMock->expects($this->any())
  291. ->method('getState')
  292. ->willReturn(\Magento\Sales\Model\Order\Invoice::STATE_OPEN);
  293. $this->orderPaymentMock->expects($this->any())
  294. ->method('canCapture')
  295. ->willReturn(true);
  296. if ($isOnline) {
  297. $this->invoiceMock->expects($this->once())
  298. ->method('capture');
  299. } else {
  300. $this->invoiceMock->expects($this->never())
  301. ->method('capture');
  302. $this->invoiceMock->expects($this->once())
  303. ->method('setCanVoidFlag')
  304. ->with(false);
  305. $this->invoiceMock->expects($this->once())
  306. ->method('pay');
  307. }
  308. } else {
  309. $this->paymentMethodMock->expects($this->any())
  310. ->method('isGateway')
  311. ->willReturn($isGateway);
  312. $this->orderPaymentMock->expects($this->any())
  313. ->method('getIsTransactionPending')
  314. ->willReturn($isTransactionPending);
  315. $this->invoiceMock->expects($this->never())
  316. ->method('capture');
  317. if ((!$isGateway || !$isOnline) && !$isTransactionPending) {
  318. $this->invoiceMock->expects($this->once())
  319. ->method('setCanVoidFlag')
  320. ->with(false);
  321. $this->invoiceMock->expects($this->once())
  322. ->method('pay');
  323. }
  324. }
  325. $this->orderMock->expects($this->once())
  326. ->method('setTotalInvoiced')
  327. ->with(86);
  328. $this->orderMock->expects($this->once())
  329. ->method('setBaseTotalInvoiced')
  330. ->with(86);
  331. $this->orderMock->expects($this->once())
  332. ->method('setSubtotalInvoiced')
  333. ->with(44);
  334. $this->orderMock->expects($this->once())
  335. ->method('setBaseSubtotalInvoiced')
  336. ->with(44);
  337. $this->orderMock->expects($this->once())
  338. ->method('setTaxInvoiced')
  339. ->with(30);
  340. $this->orderMock->expects($this->once())
  341. ->method('setBaseTaxInvoiced')
  342. ->with(30);
  343. $this->orderMock->expects($this->once())
  344. ->method('setDiscountTaxCompensationInvoiced')
  345. ->with(22);
  346. $this->orderMock->expects($this->once())
  347. ->method('setBaseDiscountTaxCompensationInvoiced')
  348. ->with(22);
  349. $this->orderMock->expects($this->once())
  350. ->method('setShippingTaxInvoiced')
  351. ->with(24);
  352. $this->orderMock->expects($this->once())
  353. ->method('setBaseShippingTaxInvoiced')
  354. ->with(24);
  355. $this->orderMock->expects($this->once())
  356. ->method('setShippingInvoiced')
  357. ->with(56);
  358. $this->orderMock->expects($this->once())
  359. ->method('setBaseShippingInvoiced')
  360. ->with(56);
  361. $this->orderMock->expects($this->once())
  362. ->method('setDiscountInvoiced')
  363. ->with(38);
  364. $this->orderMock->expects($this->once())
  365. ->method('setBaseDiscountInvoiced')
  366. ->with(38);
  367. $this->orderMock->expects($this->once())
  368. ->method('setBaseTotalInvoicedCost')
  369. ->with(62);
  370. $this->eventManagerMock->expects($this->once())
  371. ->method('dispatch')
  372. ->with(
  373. 'sales_order_invoice_register',
  374. [
  375. 'invoice' => $this->invoiceMock,
  376. 'order' => $this->orderMock,
  377. ]
  378. );
  379. $this->assertEquals(
  380. $this->orderMock,
  381. $this->subject->execute(
  382. $this->orderMock,
  383. $this->invoiceMock,
  384. $isOnline
  385. )
  386. );
  387. }
  388. /**
  389. * @return array
  390. */
  391. public function payDataProvider()
  392. {
  393. return [
  394. 'Invoice can capture, online' => [
  395. true, true, null, null,
  396. ],
  397. 'Invoice can capture, offline' => [
  398. true, false, null, null,
  399. ],
  400. 'Invoice can not capture, online, is not gateway, transaction is not pending' => [
  401. false, true, false, false,
  402. ],
  403. 'Invoice can not capture, offline, gateway, transaction is not pending' => [
  404. false, false, true, false,
  405. ],
  406. ];
  407. }
  408. }