ShipOrderTest.php 16 KB

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