InvoiceTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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;
  7. use Magento\Sales\Api\Data\InvoiceInterface;
  8. use Magento\Sales\Model\Order;
  9. use Magento\Sales\Model\Order\Invoice;
  10. use Magento\Sales\Model\ResourceModel\Order\Invoice\Collection as InvoiceCollection;
  11. use Magento\Sales\Model\ResourceModel\OrderFactory;
  12. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  13. /**
  14. * Class InvoiceTest
  15. *
  16. * @package Magento\Sales\Model\Order
  17. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  18. */
  19. class InvoiceTest extends \PHPUnit\Framework\TestCase
  20. {
  21. /**
  22. * @var \Magento\Sales\Model\Order\Invoice
  23. */
  24. protected $model;
  25. /**
  26. * Same as $model but Order was not set
  27. * @var \Magento\Sales\Model\Order\Invoice
  28. */
  29. protected $modelWithoutOrder;
  30. /**
  31. * @var string
  32. */
  33. protected $entityType = 'invoice';
  34. /**
  35. * @var OrderFactory |\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $orderFactory;
  38. /**
  39. * @var Order|MockObject
  40. */
  41. private $order;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Sales\Model\Order\Payment
  44. */
  45. protected $paymentMock;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Event\ManagerInterface
  48. */
  49. protected $eventManagerMock;
  50. /**
  51. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  52. */
  53. protected $helperManager;
  54. protected function setUp()
  55. {
  56. $this->helperManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  57. $this->order = $this->getMockBuilder(Order::class)
  58. ->disableOriginalConstructor()
  59. ->setMethods(
  60. [
  61. 'getPayment', '__wakeup', 'load', 'setHistoryEntityName', 'getStore', 'getBillingAddress',
  62. 'getShippingAddress', 'getConfig',
  63. ]
  64. )
  65. ->getMock();
  66. $this->order->method('setHistoryEntityName')
  67. ->with($this->entityType)
  68. ->willReturnSelf();
  69. $this->paymentMock = $this->getMockBuilder(
  70. \Magento\Sales\Model\Order\Payment::class
  71. )->disableOriginalConstructor()->setMethods(
  72. ['canVoid', '__wakeup', 'canCapture', 'capture', 'pay', 'cancelInvoice']
  73. )->getMock();
  74. $this->orderFactory = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, ['create']);
  75. $this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  76. $contextMock = $this->createMock(\Magento\Framework\Model\Context::class);
  77. $contextMock->expects($this->any())
  78. ->method('getEventDispatcher')
  79. ->willReturn($this->eventManagerMock);
  80. $arguments = [
  81. 'context' => $contextMock,
  82. 'orderFactory' => $this->orderFactory,
  83. 'calculatorFactory' => $this->createMock(\Magento\Framework\Math\CalculatorFactory::class),
  84. 'invoiceItemCollectionFactory' =>
  85. $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Invoice\Item\CollectionFactory::class),
  86. 'invoiceCommentFactory' => $this->createMock(\Magento\Sales\Model\Order\Invoice\CommentFactory::class),
  87. 'commentCollectionFactory' =>
  88. $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Invoice\Comment\CollectionFactory::class),
  89. ];
  90. $this->model = $this->helperManager->getObject(\Magento\Sales\Model\Order\Invoice::class, $arguments);
  91. $this->model->setOrder($this->order);
  92. $this->modelWithoutOrder = $this->helperManager->getObject(
  93. \Magento\Sales\Model\Order\Invoice::class,
  94. $arguments
  95. );
  96. }
  97. /**
  98. * @dataProvider canVoidDataProvider
  99. * @param bool $canVoid
  100. */
  101. public function testCanVoid($canVoid)
  102. {
  103. $this->order->expects($this->once())->method('getPayment')->willReturn($this->paymentMock);
  104. $this->paymentMock->expects($this->once())
  105. ->method('canVoid', '__wakeup')
  106. ->willReturn($canVoid);
  107. $this->model->setState(\Magento\Sales\Model\Order\Invoice::STATE_PAID);
  108. $this->assertEquals($canVoid, $this->model->canVoid());
  109. }
  110. /**
  111. * @dataProvider canVoidDataProvider
  112. * @param bool $canVoid
  113. */
  114. public function testDefaultCanVoid($canVoid)
  115. {
  116. $this->model->setState(\Magento\Sales\Model\Order\Invoice::STATE_PAID);
  117. $this->model->setCanVoidFlag($canVoid);
  118. $this->assertEquals($canVoid, $this->model->canVoid());
  119. }
  120. /**
  121. * @return array
  122. */
  123. public function canVoidDataProvider()
  124. {
  125. return [[true], [false]];
  126. }
  127. public function testGetOrder()
  128. {
  129. $this->order->expects($this->once())
  130. ->method('setHistoryEntityName')
  131. ->with($this->entityType)
  132. ->will($this->returnSelf());
  133. $this->assertEquals($this->order, $this->model->getOrder());
  134. }
  135. public function testGetOrderLoadedById()
  136. {
  137. $orderId = 100000041;
  138. $this->modelWithoutOrder->setOrderId($orderId);
  139. $this->order->expects($this->once())
  140. ->method('load')
  141. ->with($orderId)
  142. ->willReturnSelf();
  143. $this->order->expects($this->once())
  144. ->method('setHistoryEntityName')
  145. ->with($this->entityType)
  146. ->willReturnSelf();
  147. $this->orderFactory->expects($this->once())
  148. ->method('create')
  149. ->willReturn($this->order);
  150. $this->assertEquals($this->order, $this->modelWithoutOrder->getOrder());
  151. }
  152. public function testGetEntityType()
  153. {
  154. $this->assertEquals($this->entityType, $this->model->getEntityType());
  155. }
  156. public function testGetIncrementId()
  157. {
  158. $this->model->setIncrementId('test_increment_id');
  159. $this->assertEquals('test_increment_id', $this->model->getIncrementId());
  160. }
  161. public function testSetOrder()
  162. {
  163. $orderId = 1111;
  164. $storeId = 2221;
  165. $this->order->setId($orderId);
  166. $this->order->setStoreId($storeId);
  167. $this->assertNull($this->model->getOrderId());
  168. $this->assertNull($this->model->getStoreId());
  169. $this->assertEquals($this->model, $this->model->setOrder($this->order));
  170. $this->assertEquals($this->order, $this->model->getOrder());
  171. $this->assertEquals($orderId, $this->model->getOrderId());
  172. $this->assertEquals($storeId, $this->model->getStoreId());
  173. }
  174. public function testGetStore()
  175. {
  176. $store = $this->helperManager->getObject(\Magento\Store\Model\Store::class, []);
  177. $this->order->expects($this->once())->method('getStore')->willReturn($store);
  178. $this->assertEquals($store, $this->model->getStore());
  179. }
  180. public function testGetShippingAddress()
  181. {
  182. $address = $this->helperManager->getObject(\Magento\Sales\Model\Order\Address::class, []);
  183. $this->order->expects($this->once())->method('getShippingAddress')->willReturn($address);
  184. $this->assertEquals($address, $this->model->getShippingAddress());
  185. }
  186. /**
  187. * @dataProvider canCaptureDataProvider
  188. * @param string $state
  189. * @param bool|null $canPaymentCapture
  190. * @param bool $expectedResult
  191. */
  192. public function testCanCapture($state, $canPaymentCapture, $expectedResult)
  193. {
  194. $this->model->setState($state);
  195. if (null !== $canPaymentCapture) {
  196. $this->order->expects($this->once())->method('getPayment')->willReturn($this->paymentMock);
  197. $this->paymentMock->expects($this->once())->method('canCapture')->willReturn($canPaymentCapture);
  198. } else {
  199. $this->order->expects($this->never())->method('getPayment');
  200. $this->paymentMock->expects($this->never())->method('canCapture');
  201. }
  202. $this->assertEquals($expectedResult, $this->model->canCapture());
  203. }
  204. /**
  205. * Data provider for testCanCapture
  206. *
  207. * @return array
  208. */
  209. public function canCaptureDataProvider()
  210. {
  211. return [
  212. [Invoice::STATE_OPEN, true, true],
  213. [Invoice::STATE_OPEN, false, false],
  214. [Invoice::STATE_CANCELED, null, false],
  215. [Invoice::STATE_CANCELED, null, false],
  216. [Invoice::STATE_PAID, null, false],
  217. [Invoice::STATE_PAID, null, false]
  218. ];
  219. }
  220. /**
  221. * @dataProvider canCancelDataProvider
  222. * @param string $state
  223. * @param bool $expectedResult
  224. */
  225. public function testCanCancel($state, $expectedResult)
  226. {
  227. $this->model->setState($state);
  228. $this->assertEquals($expectedResult, $this->model->canCancel());
  229. }
  230. /**
  231. * Data provider for testCanCancel
  232. *
  233. * @return array
  234. */
  235. public function canCancelDataProvider()
  236. {
  237. return [
  238. [Invoice::STATE_OPEN, true],
  239. [Invoice::STATE_CANCELED, false],
  240. [Invoice::STATE_PAID, false]
  241. ];
  242. }
  243. /**
  244. * @dataProvider canRefundDataProvider
  245. * @param string $state
  246. * @param float $baseGrandTotal
  247. * @param float $baseTotalRefunded
  248. * @param bool $expectedResult
  249. */
  250. public function testCanRefund($state, $baseGrandTotal, $baseTotalRefunded, $expectedResult)
  251. {
  252. $this->model->setState($state);
  253. $this->model->setBaseGrandTotal($baseGrandTotal);
  254. $this->model->setBaseTotalRefunded($baseTotalRefunded);
  255. $this->assertEquals($expectedResult, $this->model->canRefund());
  256. }
  257. /**
  258. * Data provider for testCanRefund
  259. *
  260. * @return array
  261. */
  262. public function canRefundDataProvider()
  263. {
  264. return [
  265. [Invoice::STATE_OPEN, 0.00, 0.00, false],
  266. [Invoice::STATE_CANCELED, 1.00, 0.01, false],
  267. [Invoice::STATE_PAID, 1.00, 0.00, true],
  268. //[Invoice::STATE_PAID, 1.00, 1.00, false]
  269. [Invoice::STATE_PAID, 1.000101, 1.0000, true],
  270. [Invoice::STATE_PAID, 1.0001, 1.00, false],
  271. [Invoice::STATE_PAID, 1.00, 1.0001, false],
  272. ];
  273. }
  274. public function testCaptureNotPaid()
  275. {
  276. $this->model->setIsPaid(false);
  277. $this->order->expects($this->once())->method('getPayment')->willReturn($this->paymentMock);
  278. $this->paymentMock->expects($this->once())->method('capture')->with($this->model)->willReturnSelf();
  279. $this->paymentMock->expects($this->never())->method('pay');
  280. $this->assertEquals($this->model, $this->model->capture());
  281. }
  282. public function testCapturePaid()
  283. {
  284. $collection = $this->getOrderInvoiceCollection();
  285. $collection->method('getItems')
  286. ->willReturn([$this->model]);
  287. $this->model->setIsPaid(true);
  288. $this->order->method('getPayment')
  289. ->willReturn($this->paymentMock);
  290. $this->paymentMock->method('capture')
  291. ->with($this->model)
  292. ->willReturnSelf();
  293. $this->mockPay();
  294. self::assertEquals($this->model, $this->model->capture());
  295. }
  296. public function mockPay()
  297. {
  298. $this->order->expects($this->any())->method('getPayment')->willReturn($this->paymentMock);
  299. $this->paymentMock->expects($this->once())->method('pay')->with($this->model)->willReturnSelf();
  300. $this->eventManagerMock
  301. ->expects($this->once())
  302. ->method('dispatch')
  303. ->with('sales_order_invoice_pay');
  304. }
  305. /**
  306. * @dataProvider payDataProvider
  307. * @param float $totalPaid
  308. * @param float $baseTotalPaid
  309. * @param float $expectedTotal
  310. * @param float $expectedBaseTotal
  311. * @param float $expectedState
  312. * @param array $items
  313. */
  314. public function testPay(
  315. $totalPaid,
  316. $baseTotalPaid,
  317. $expectedTotal,
  318. $expectedBaseTotal,
  319. $expectedState,
  320. array $items
  321. ) {
  322. $this->mockPay();
  323. $this->model->setGrandTotal($totalPaid);
  324. $this->model->setBaseGrandTotal($baseTotalPaid);
  325. $this->order->setTotalPaid($totalPaid);
  326. $this->order->setBaseTotalPaid($baseTotalPaid);
  327. $collection = $this->getOrderInvoiceCollection();
  328. $collection->method('getItems')
  329. ->willReturn($items);
  330. self::assertFalse($this->model->wasPayCalled());
  331. self::assertEquals($this->model, $this->model->pay());
  332. self::assertTrue($this->model->wasPayCalled());
  333. self::assertEquals($expectedState, $this->model->getState());
  334. #second call of pay() method must do nothing
  335. $this->model->pay();
  336. self::assertEquals($expectedBaseTotal, $this->order->getBaseTotalPaid());
  337. self::assertEquals($expectedTotal, $this->order->getTotalPaid());
  338. }
  339. /**
  340. * @return array
  341. */
  342. public function payDataProvider()
  343. {
  344. return [
  345. [10.99, 1.00, 10.99, 1.00, Invoice::STATE_PAID, ['item1']],
  346. [11.00, 1.00, 22.00, 2.00, Invoice::STATE_PAID, ['item1', 'item2']],
  347. ];
  348. }
  349. /**
  350. * Creates collection of invoices for order.
  351. *
  352. * @return InvoiceCollection|MockObject
  353. */
  354. private function getOrderInvoiceCollection()
  355. {
  356. $collection = $this->getMockBuilder(InvoiceCollection::class)
  357. ->disableOriginalConstructor()
  358. ->getMock();
  359. $refObject = new \ReflectionClass($this->order);
  360. $refProperty = $refObject->getProperty('_invoices');
  361. $refProperty->setAccessible(true);
  362. $refProperty->setValue($this->order, $collection);
  363. return $collection;
  364. }
  365. /**
  366. * Assert open invoice can be canceled, and its status changes
  367. */
  368. public function testCancelOpenInvoice()
  369. {
  370. $orderConfigMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Config::class)
  371. ->disableOriginalConstructor()->setMethods(
  372. ['getStateDefaultStatus']
  373. )->getMock();
  374. $orderConfigMock->expects($this->once())->method('getStateDefaultStatus')
  375. ->with(Order::STATE_PROCESSING)
  376. ->willReturn(Order::STATE_PROCESSING);
  377. $this->order->expects($this->once())->method('getPayment')->willReturn($this->paymentMock);
  378. $this->order->expects($this->once())->method('getConfig')->willReturn($orderConfigMock);
  379. $this->paymentMock->expects($this->once())
  380. ->method('cancelInvoice')
  381. ->willReturn($this->paymentMock);
  382. $this->eventManagerMock->expects($this->once())
  383. ->method('dispatch')
  384. ->with('sales_order_invoice_cancel');
  385. $this->model->setData(InvoiceInterface::ITEMS, []);
  386. $this->model->setState(Invoice::STATE_OPEN);
  387. $this->model->cancel();
  388. self::assertEquals(Invoice::STATE_CANCELED, $this->model->getState());
  389. }
  390. /**
  391. * Assert open invoice can be canceled, and its status changes
  392. *
  393. * @param $initialInvoiceStatus
  394. * @param $finalInvoiceStatus
  395. * @dataProvider getNotOpenedInvoiceStatuses
  396. */
  397. public function testCannotCancelNotOpenedInvoice($initialInvoiceStatus, $finalInvoiceStatus)
  398. {
  399. $this->order->expects($this->never())->method('getPayment');
  400. $this->paymentMock->expects($this->never())->method('cancelInvoice');
  401. $this->eventManagerMock->expects($this->never())
  402. ->method('dispatch')
  403. ->with('sales_order_invoice_cancel');
  404. $this->model->setState($initialInvoiceStatus);
  405. $this->model->cancel();
  406. self::assertEquals($finalInvoiceStatus, $this->model->getState());
  407. }
  408. /**
  409. * @return array
  410. */
  411. public function getNotOpenedInvoiceStatuses()
  412. {
  413. return [
  414. [Invoice::STATE_PAID, Invoice::STATE_PAID],
  415. [Invoice::STATE_CANCELED, Invoice::STATE_CANCELED],
  416. ];
  417. }
  418. }