PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/Magento/Sales/Test/Unit/Model/Order/Invoice/PayOperationTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 444 lines | 366 code | 30 blank | 48 comment | 7 complexity | 13f2b123da1b7cb4a9f748d64b2cec1c MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. 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->getMock(
  211. \Magento\Framework\Model\Context::class,
  212. [],
  213. [],
  214. '',
  215. false
  216. );
  217. $this->invoiceItemMock = $this->getMockForAbstractClass(
  218. \Magento\Sales\Api\Data\InvoiceItemInterface::class,
  219. [],
  220. '',
  221. false,
  222. false,
  223. true,
  224. [
  225. 'isDeleted',
  226. 'register',
  227. ]
  228. );
  229. $this->invoiceItemMock->expects($this->any())
  230. ->method('isDeleted')
  231. ->willReturn(false);
  232. $this->invoiceItemMock->expects($this->any())
  233. ->method('getQty')
  234. ->willReturn(1);
  235. $this->orderPaymentMock = $this->getMockForAbstractClass(
  236. \Magento\Sales\Api\Data\OrderPaymentInterface::class,
  237. [],
  238. '',
  239. false,
  240. false,
  241. true,
  242. [
  243. 'canCapture',
  244. 'getMethodInstance',
  245. 'getIsTransactionPending',
  246. ]
  247. );
  248. $this->orderMock->expects($this->any())
  249. ->method('getPayment')
  250. ->willReturn($this->orderPaymentMock);
  251. $this->eventManagerMock = $this->getMockForAbstractClass(
  252. \Magento\Framework\Event\ManagerInterface::class,
  253. [],
  254. '',
  255. false,
  256. false,
  257. true,
  258. []
  259. );
  260. $this->contextMock->expects($this->any())
  261. ->method('getEventDispatcher')
  262. ->willReturn($this->eventManagerMock);
  263. $this->paymentMethodMock = $this->getMockForAbstractClass(
  264. \Magento\Payment\Model\MethodInterface::class,
  265. [],
  266. '',
  267. false,
  268. false,
  269. true,
  270. []
  271. );
  272. $this->orderPaymentMock->expects($this->any())
  273. ->method('getMethodInstance')
  274. ->willReturn($this->paymentMethodMock);
  275. $this->subject = new \Magento\Sales\Model\Order\Invoice\PayOperation(
  276. $this->contextMock
  277. );
  278. }
  279. /**
  280. * @param bool|null $canCapture
  281. * @param bool|null $isOnline
  282. * @param bool|null $isGateway
  283. * @param bool|null $isTransactionPending
  284. *
  285. * @dataProvider payDataProvider
  286. *
  287. * @return void
  288. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  289. */
  290. public function testExecute($canCapture, $isOnline, $isGateway, $isTransactionPending)
  291. {
  292. $this->invoiceMock->expects($this->any())
  293. ->method('getItems')
  294. ->willReturn([$this->invoiceItemMock]);
  295. if ($canCapture) {
  296. $this->invoiceMock->expects($this->any())
  297. ->method('getState')
  298. ->willReturn(\Magento\Sales\Model\Order\Invoice::STATE_OPEN);
  299. $this->orderPaymentMock->expects($this->any())
  300. ->method('canCapture')
  301. ->willReturn(true);
  302. if ($isOnline) {
  303. $this->invoiceMock->expects($this->once())
  304. ->method('capture');
  305. } else {
  306. $this->invoiceMock->expects($this->never())
  307. ->method('capture');
  308. $this->invoiceMock->expects($this->once())
  309. ->method('setCanVoidFlag')
  310. ->with(false);
  311. $this->invoiceMock->expects($this->once())
  312. ->method('pay');
  313. }
  314. } else {
  315. $this->paymentMethodMock->expects($this->any())
  316. ->method('isGateway')
  317. ->willReturn($isGateway);
  318. $this->orderPaymentMock->expects($this->any())
  319. ->method('getIsTransactionPending')
  320. ->willReturn($isTransactionPending);
  321. $this->invoiceMock->expects($this->never())
  322. ->method('capture');
  323. if ((!$isGateway || !$isOnline) && !$isTransactionPending) {
  324. $this->invoiceMock->expects($this->once())
  325. ->method('setCanVoidFlag')
  326. ->with(false);
  327. $this->invoiceMock->expects($this->once())
  328. ->method('pay');
  329. }
  330. }
  331. $this->orderMock->expects($this->once())
  332. ->method('setTotalInvoiced')
  333. ->with(86);
  334. $this->orderMock->expects($this->once())
  335. ->method('setBaseTotalInvoiced')
  336. ->with(86);
  337. $this->orderMock->expects($this->once())
  338. ->method('setSubtotalInvoiced')
  339. ->with(44);
  340. $this->orderMock->expects($this->once())
  341. ->method('setBaseSubtotalInvoiced')
  342. ->with(44);
  343. $this->orderMock->expects($this->once())
  344. ->method('setTaxInvoiced')
  345. ->with(30);
  346. $this->orderMock->expects($this->once())
  347. ->method('setBaseTaxInvoiced')
  348. ->with(30);
  349. $this->orderMock->expects($this->once())
  350. ->method('setDiscountTaxCompensationInvoiced')
  351. ->with(22);
  352. $this->orderMock->expects($this->once())
  353. ->method('setBaseDiscountTaxCompensationInvoiced')
  354. ->with(22);
  355. $this->orderMock->expects($this->once())
  356. ->method('setShippingTaxInvoiced')
  357. ->with(24);
  358. $this->orderMock->expects($this->once())
  359. ->method('setBaseShippingTaxInvoiced')
  360. ->with(24);
  361. $this->orderMock->expects($this->once())
  362. ->method('setShippingInvoiced')
  363. ->with(56);
  364. $this->orderMock->expects($this->once())
  365. ->method('setBaseShippingInvoiced')
  366. ->with(56);
  367. $this->orderMock->expects($this->once())
  368. ->method('setDiscountInvoiced')
  369. ->with(38);
  370. $this->orderMock->expects($this->once())
  371. ->method('setBaseDiscountInvoiced')
  372. ->with(38);
  373. $this->orderMock->expects($this->once())
  374. ->method('setBaseTotalInvoicedCost')
  375. ->with(62);
  376. $this->eventManagerMock->expects($this->once())
  377. ->method('dispatch')
  378. ->with(
  379. 'sales_order_invoice_register',
  380. [
  381. 'invoice' => $this->invoiceMock,
  382. 'order' => $this->orderMock,
  383. ]
  384. );
  385. $this->assertEquals(
  386. $this->orderMock,
  387. $this->subject->execute(
  388. $this->orderMock,
  389. $this->invoiceMock,
  390. $isOnline
  391. )
  392. );
  393. }
  394. /**
  395. * @return array
  396. */
  397. public function payDataProvider()
  398. {
  399. return [
  400. 'Invoice can capture, online' => [
  401. true, true, null, null,
  402. ],
  403. 'Invoice can capture, offline' => [
  404. true, false, null, null,
  405. ],
  406. 'Invoice can not capture, online, is not gateway, transaction is not pending' => [
  407. false, true, false, false,
  408. ],
  409. 'Invoice can not capture, offline, gateway, transaction is not pending' => [
  410. false, false, true, false,
  411. ],
  412. ];
  413. }
  414. }