InvoiceOrderTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\DB\Adapter\AdapterInterface;
  9. use Magento\Sales\Api\Data\InvoiceCommentCreationInterface;
  10. use Magento\Sales\Api\Data\InvoiceCreationArgumentsInterface;
  11. use Magento\Sales\Api\Data\InvoiceInterface;
  12. use Magento\Sales\Api\Data\OrderInterface;
  13. use Magento\Sales\Api\OrderRepositoryInterface;
  14. use Magento\Sales\Model\Order;
  15. use Magento\Sales\Model\Order\Config as OrderConfig;
  16. use Magento\Sales\Model\Order\Invoice\NotifierInterface;
  17. use Magento\Sales\Model\Order\InvoiceDocumentFactory;
  18. use Magento\Sales\Model\Order\InvoiceRepository;
  19. use Magento\Sales\Model\Order\OrderStateResolverInterface;
  20. use Magento\Sales\Model\Order\Validation\InvoiceOrderInterface;
  21. use Magento\Sales\Model\Order\PaymentAdapterInterface;
  22. use Magento\Sales\Model\ValidatorResultInterface;
  23. use Magento\Sales\Model\InvoiceOrder;
  24. use Psr\Log\LoggerInterface;
  25. /**
  26. * Class InvoiceOrderTest
  27. *
  28. * @SuppressWarnings(PHPMD.TooManyFields)
  29. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  30. */
  31. class InvoiceOrderTest extends \PHPUnit\Framework\TestCase
  32. {
  33. /**
  34. * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $resourceConnectionMock;
  37. /**
  38. * @var OrderRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $orderRepositoryMock;
  41. /**
  42. * @var InvoiceDocumentFactory|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $invoiceDocumentFactoryMock;
  45. /**
  46. * @var InvoiceOrderInterface|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $invoiceOrderValidatorMock;
  49. /**
  50. * @var PaymentAdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $paymentAdapterMock;
  53. /**
  54. * @var OrderStateResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  55. */
  56. private $orderStateResolverMock;
  57. /**
  58. * @var OrderConfig|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. private $configMock;
  61. /**
  62. * @var InvoiceRepository|\PHPUnit_Framework_MockObject_MockObject
  63. */
  64. private $invoiceRepositoryMock;
  65. /**
  66. * @var NotifierInterface|\PHPUnit_Framework_MockObject_MockObject
  67. */
  68. private $notifierInterfaceMock;
  69. /**
  70. * @var InvoiceOrder|\PHPUnit_Framework_MockObject_MockObject
  71. */
  72. private $invoiceOrder;
  73. /**
  74. * @var InvoiceCreationArgumentsInterface|\PHPUnit_Framework_MockObject_MockObject
  75. */
  76. private $invoiceCommentCreationMock;
  77. /**
  78. * @var InvoiceCommentCreationInterface|\PHPUnit_Framework_MockObject_MockObject
  79. */
  80. private $invoiceCreationArgumentsMock;
  81. /**
  82. * @var OrderInterface|\PHPUnit_Framework_MockObject_MockObject
  83. */
  84. private $orderMock;
  85. /**
  86. * @var InvoiceInterface|\PHPUnit_Framework_MockObject_MockObject
  87. */
  88. private $invoiceMock;
  89. /**
  90. * @var AdapterInterface
  91. */
  92. private $adapterInterface;
  93. /**
  94. * @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  95. */
  96. private $loggerMock;
  97. /**
  98. * @var ValidatorResultInterface|\PHPUnit_Framework_MockObject_MockObject
  99. */
  100. private $errorMessagesMock;
  101. protected function setUp()
  102. {
  103. $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
  104. ->disableOriginalConstructor()
  105. ->getMock();
  106. $this->orderRepositoryMock = $this->getMockBuilder(OrderRepositoryInterface::class)
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $this->invoiceDocumentFactoryMock = $this->getMockBuilder(InvoiceDocumentFactory::class)
  110. ->disableOriginalConstructor()
  111. ->getMock();
  112. $this->paymentAdapterMock = $this->getMockBuilder(PaymentAdapterInterface::class)
  113. ->disableOriginalConstructor()
  114. ->getMock();
  115. $this->orderStateResolverMock = $this->getMockBuilder(OrderStateResolverInterface::class)
  116. ->disableOriginalConstructor()
  117. ->getMock();
  118. $this->configMock = $this->getMockBuilder(OrderConfig::class)
  119. ->disableOriginalConstructor()
  120. ->getMock();
  121. $this->invoiceRepositoryMock = $this->getMockBuilder(InvoiceRepository::class)
  122. ->disableOriginalConstructor()
  123. ->getMock();
  124. $this->notifierInterfaceMock = $this->getMockBuilder(NotifierInterface::class)
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
  128. ->disableOriginalConstructor()
  129. ->getMock();
  130. $this->invoiceCommentCreationMock = $this->getMockBuilder(InvoiceCommentCreationInterface::class)
  131. ->disableOriginalConstructor()
  132. ->getMock();
  133. $this->invoiceCreationArgumentsMock = $this->getMockBuilder(InvoiceCreationArgumentsInterface::class)
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. $this->orderMock = $this->getMockBuilder(OrderInterface::class)
  137. ->disableOriginalConstructor()
  138. ->getMock();
  139. $this->invoiceMock = $this->getMockBuilder(InvoiceInterface::class)
  140. ->disableOriginalConstructor()
  141. ->getMock();
  142. $this->adapterInterface = $this->getMockBuilder(AdapterInterface::class)
  143. ->disableOriginalConstructor()
  144. ->getMock();
  145. $this->invoiceOrderValidatorMock = $this->getMockBuilder(InvoiceOrderInterface::class)
  146. ->disableOriginalConstructor()
  147. ->getMock();
  148. $this->errorMessagesMock = $this->getMockBuilder(ValidatorResultInterface::class)
  149. ->disableOriginalConstructor()
  150. ->setMethods(['hasMessages', 'getMessages', 'addMessage'])
  151. ->getMock();
  152. $this->invoiceOrder = new InvoiceOrder(
  153. $this->resourceConnectionMock,
  154. $this->orderRepositoryMock,
  155. $this->invoiceDocumentFactoryMock,
  156. $this->paymentAdapterMock,
  157. $this->orderStateResolverMock,
  158. $this->configMock,
  159. $this->invoiceRepositoryMock,
  160. $this->invoiceOrderValidatorMock,
  161. $this->notifierInterfaceMock,
  162. $this->loggerMock
  163. );
  164. }
  165. /**
  166. * @param int $orderId
  167. * @param bool $capture
  168. * @param array $items
  169. * @param bool $notify
  170. * @param bool $appendComment
  171. * @throws \Magento\Sales\Exception\CouldNotInvoiceException
  172. * @throws \Magento\Sales\Exception\DocumentValidationException
  173. * @dataProvider dataProvider
  174. */
  175. public function testOrderInvoice($orderId, $capture, $items, $notify, $appendComment)
  176. {
  177. $this->resourceConnectionMock->expects($this->once())
  178. ->method('getConnection')
  179. ->with('sales')
  180. ->willReturn($this->adapterInterface);
  181. $this->orderRepositoryMock->expects($this->once())
  182. ->method('get')
  183. ->willReturn($this->orderMock);
  184. $this->invoiceDocumentFactoryMock->expects($this->once())
  185. ->method('create')
  186. ->with(
  187. $this->orderMock,
  188. $items,
  189. $this->invoiceCommentCreationMock,
  190. ($appendComment && $notify),
  191. $this->invoiceCreationArgumentsMock
  192. )->willReturn($this->invoiceMock);
  193. $this->invoiceOrderValidatorMock->expects($this->once())
  194. ->method('validate')
  195. ->with(
  196. $this->orderMock,
  197. $this->invoiceMock,
  198. $capture,
  199. $items,
  200. $notify,
  201. $appendComment,
  202. $this->invoiceCommentCreationMock,
  203. $this->invoiceCreationArgumentsMock
  204. )
  205. ->willReturn($this->errorMessagesMock);
  206. $hasMessages = false;
  207. $this->errorMessagesMock->expects($this->once())
  208. ->method('hasMessages')->willReturn($hasMessages);
  209. $this->paymentAdapterMock->expects($this->once())
  210. ->method('pay')
  211. ->with($this->orderMock, $this->invoiceMock, $capture)
  212. ->willReturn($this->orderMock);
  213. $this->orderStateResolverMock->expects($this->once())
  214. ->method('getStateForOrder')
  215. ->with($this->orderMock, [OrderStateResolverInterface::IN_PROGRESS])
  216. ->willReturn(Order::STATE_PROCESSING);
  217. $this->orderMock->expects($this->once())
  218. ->method('setState')
  219. ->with(Order::STATE_PROCESSING)
  220. ->willReturnSelf();
  221. $this->orderMock->expects($this->once())
  222. ->method('getState')
  223. ->willReturn(Order::STATE_PROCESSING);
  224. $this->configMock->expects($this->once())
  225. ->method('getStateDefaultStatus')
  226. ->with(Order::STATE_PROCESSING)
  227. ->willReturn('Processing');
  228. $this->orderMock->expects($this->once())
  229. ->method('setStatus')
  230. ->with('Processing')
  231. ->willReturnSelf();
  232. $this->invoiceMock->expects($this->once())
  233. ->method('setState')
  234. ->with(\Magento\Sales\Model\Order\Invoice::STATE_PAID)
  235. ->willReturnSelf();
  236. $this->invoiceRepositoryMock->expects($this->once())
  237. ->method('save')
  238. ->with($this->invoiceMock)
  239. ->willReturn($this->invoiceMock);
  240. $this->orderRepositoryMock->expects($this->once())
  241. ->method('save')
  242. ->with($this->orderMock)
  243. ->willReturn($this->orderMock);
  244. if ($notify) {
  245. $this->notifierInterfaceMock->expects($this->once())
  246. ->method('notify')
  247. ->with($this->orderMock, $this->invoiceMock, $this->invoiceCommentCreationMock);
  248. }
  249. $this->invoiceMock->expects($this->once())
  250. ->method('getEntityId')
  251. ->willReturn(2);
  252. $this->assertEquals(
  253. 2,
  254. $this->invoiceOrder->execute(
  255. $orderId,
  256. $capture,
  257. $items,
  258. $notify,
  259. $appendComment,
  260. $this->invoiceCommentCreationMock,
  261. $this->invoiceCreationArgumentsMock
  262. )
  263. );
  264. }
  265. /**
  266. * @expectedException \Magento\Sales\Api\Exception\DocumentValidationExceptionInterface
  267. */
  268. public function testDocumentValidationException()
  269. {
  270. $orderId = 1;
  271. $capture = true;
  272. $items = [1 => 2];
  273. $notify = true;
  274. $appendComment = true;
  275. $errorMessages = ['error1', 'error2'];
  276. $this->orderRepositoryMock->expects($this->once())
  277. ->method('get')
  278. ->willReturn($this->orderMock);
  279. $this->invoiceDocumentFactoryMock->expects($this->once())
  280. ->method('create')
  281. ->with(
  282. $this->orderMock,
  283. $items,
  284. $this->invoiceCommentCreationMock,
  285. ($appendComment && $notify),
  286. $this->invoiceCreationArgumentsMock
  287. )->willReturn($this->invoiceMock);
  288. $this->invoiceOrderValidatorMock->expects($this->once())
  289. ->method('validate')
  290. ->with(
  291. $this->orderMock,
  292. $this->invoiceMock,
  293. $capture,
  294. $items,
  295. $notify,
  296. $appendComment,
  297. $this->invoiceCommentCreationMock,
  298. $this->invoiceCreationArgumentsMock
  299. )
  300. ->willReturn($this->errorMessagesMock);
  301. $hasMessages = true;
  302. $this->errorMessagesMock->expects($this->once())
  303. ->method('hasMessages')->willReturn($hasMessages);
  304. $this->errorMessagesMock->expects($this->once())
  305. ->method('getMessages')->willReturn($errorMessages);
  306. $this->invoiceOrder->execute(
  307. $orderId,
  308. $capture,
  309. $items,
  310. $notify,
  311. $appendComment,
  312. $this->invoiceCommentCreationMock,
  313. $this->invoiceCreationArgumentsMock
  314. );
  315. }
  316. /**
  317. * @expectedException \Magento\Sales\Api\Exception\CouldNotInvoiceExceptionInterface
  318. */
  319. public function testCouldNotInvoiceException()
  320. {
  321. $orderId = 1;
  322. $items = [1 => 2];
  323. $capture = true;
  324. $notify = true;
  325. $appendComment = true;
  326. $this->resourceConnectionMock->expects($this->once())
  327. ->method('getConnection')
  328. ->with('sales')
  329. ->willReturn($this->adapterInterface);
  330. $this->orderRepositoryMock->expects($this->once())
  331. ->method('get')
  332. ->willReturn($this->orderMock);
  333. $this->invoiceDocumentFactoryMock->expects($this->once())
  334. ->method('create')
  335. ->with(
  336. $this->orderMock,
  337. $items,
  338. $this->invoiceCommentCreationMock,
  339. ($appendComment && $notify),
  340. $this->invoiceCreationArgumentsMock
  341. )->willReturn($this->invoiceMock);
  342. $this->invoiceOrderValidatorMock->expects($this->once())
  343. ->method('validate')
  344. ->with(
  345. $this->orderMock,
  346. $this->invoiceMock,
  347. $capture,
  348. $items,
  349. $notify,
  350. $appendComment,
  351. $this->invoiceCommentCreationMock,
  352. $this->invoiceCreationArgumentsMock
  353. )
  354. ->willReturn($this->errorMessagesMock);
  355. $hasMessages = false;
  356. $this->errorMessagesMock->expects($this->once())
  357. ->method('hasMessages')->willReturn($hasMessages);
  358. $e = new \Exception();
  359. $this->paymentAdapterMock->expects($this->once())
  360. ->method('pay')
  361. ->with($this->orderMock, $this->invoiceMock, $capture)
  362. ->willThrowException($e);
  363. $this->loggerMock->expects($this->once())
  364. ->method('critical')
  365. ->with($e);
  366. $this->adapterInterface->expects($this->once())
  367. ->method('rollBack');
  368. $this->invoiceOrder->execute(
  369. $orderId,
  370. $capture,
  371. $items,
  372. $notify,
  373. $appendComment,
  374. $this->invoiceCommentCreationMock,
  375. $this->invoiceCreationArgumentsMock
  376. );
  377. }
  378. /**
  379. * @return array
  380. */
  381. public function dataProvider()
  382. {
  383. return [
  384. 'TestWithNotifyTrue' => [1, true, [1 => 2], true, true],
  385. 'TestWithNotifyFalse' => [1, true, [1 => 2], false, true],
  386. ];
  387. }
  388. }