OrderItemRepositoryTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GiftMessage\Test\Unit\Model;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Framework\Exception\State\InvalidTransitionException;
  9. use Magento\Framework\Exception\CouldNotSaveException;
  10. /**
  11. * Test class for \Magento\GiftMessage\Model\OrderItemRepository
  12. * * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class OrderItemRepositoryTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\GiftMessage\Model\OrderItemRepository|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $orderItemRepository;
  20. /**
  21. * @var \Magento\Sales\Model\OrderFactory|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $orderFactoryMock;
  24. /**
  25. * @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $orderMock;
  28. /**
  29. * @var \Magento\GiftMessage\Helper\Message|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $helperMock;
  32. /**
  33. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $storeManagerMock;
  36. /**
  37. * @var \Magento\Store\Api\Data\StoreInterface|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $storeMock;
  40. /**
  41. * @var \Magento\GiftMessage\Model\MessageFactory|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $messageFactoryMock;
  44. /**
  45. * @var \Magento\GiftMessage\Model\Save|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $giftMessageSaveModelMock;
  48. protected function setUp()
  49. {
  50. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  51. $this->orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  52. ->disableOriginalConstructor()
  53. ->setMethods(['load', 'getItemById', 'getIsVirtual'])
  54. ->getMock();
  55. $this->orderFactoryMock = $this->getMockBuilder(\Magento\Sales\Model\OrderFactory::class)
  56. ->disableOriginalConstructor()
  57. ->setMethods(['create'])
  58. ->getMock();
  59. $this->helperMock = $this->getMockBuilder(\Magento\GiftMessage\Helper\Message::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  63. ->disableOriginalConstructor()
  64. ->setMethods(['getStore'])
  65. ->getMockForAbstractClass();
  66. $this->storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
  67. ->disableOriginalConstructor()
  68. ->setMethods([])
  69. ->getMock();
  70. $this->messageFactoryMock = $this->getMockBuilder(\Magento\GiftMessage\Model\MessageFactory::class)
  71. ->disableOriginalConstructor()
  72. ->setMethods(['create'])
  73. ->getMock();
  74. $this->giftMessageSaveModelMock = $this->getMockBuilder(\Magento\GiftMessage\Model\Save::class)
  75. ->disableOriginalConstructor()
  76. ->setMethods(['setGiftmessages', 'saveAllInOrder'])
  77. ->getMock();
  78. $this->storeManagerMock->expects($this->any())
  79. ->method('getStore')
  80. ->willReturn($this->storeMock);
  81. $this->orderItemRepository = $helper->getObject(
  82. \Magento\GiftMessage\Model\OrderItemRepository::class,
  83. [
  84. 'orderFactory' => $this->orderFactoryMock,
  85. 'storeManager' => $this->storeManagerMock,
  86. 'helper' => $this->helperMock,
  87. 'messageFactory' => $this->messageFactoryMock,
  88. 'giftMessageSaveModel' => $this->giftMessageSaveModelMock
  89. ]
  90. );
  91. }
  92. /**
  93. * @covers \Magento\GiftMessage\Model\OrderItemRepository::get
  94. */
  95. public function testGet()
  96. {
  97. $orderId = 1;
  98. $orderItemId = 2;
  99. $messageId = 3;
  100. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  101. ->disableOriginalConstructor()
  102. ->setMethods(['getGiftMessageId'])
  103. ->getMock();
  104. $messageMock = $this->getMockBuilder(\Magento\GiftMessage\Model\Message::class)
  105. ->disableOriginalConstructor()
  106. ->getMock();
  107. $this->orderFactoryMock->expects($this->once())
  108. ->method('create')
  109. ->willReturn($this->orderMock);
  110. $this->orderMock->expects($this->once())
  111. ->method('load')
  112. ->willReturnSelf();
  113. $this->orderMock->expects($this->once())
  114. ->method('getItemById')
  115. ->with($orderItemId)
  116. ->willReturn($orderItemMock);
  117. $this->helperMock->expects($this->once())
  118. ->method('isMessagesAllowed')
  119. ->with('order_item', $orderItemMock, $this->storeMock)
  120. ->willReturn(true);
  121. $orderItemMock->expects($this->once())
  122. ->method('getGiftMessageId')
  123. ->willReturn($messageId);
  124. $this->messageFactoryMock->expects($this->any())
  125. ->method('create')
  126. ->willReturn($messageMock);
  127. $messageMock->expects($this->once())
  128. ->method('load')
  129. ->with($messageId)
  130. ->willReturnSelf();
  131. $this->assertEquals($messageMock, $this->orderItemRepository->get($orderId, $orderItemId));
  132. }
  133. /**
  134. * @covers \Magento\GiftMessage\Model\OrderItemRepository::get
  135. */
  136. public function testGetNoSuchEntityExceptionOnGetItemById()
  137. {
  138. $orderId = 1;
  139. $orderItemId = 2;
  140. $this->orderFactoryMock->expects($this->once())
  141. ->method('create')
  142. ->willReturn($this->orderMock);
  143. $this->orderMock->expects($this->once())
  144. ->method('load')
  145. ->willReturnSelf();
  146. $this->orderMock->expects($this->once())
  147. ->method('getItemById')
  148. ->with($orderItemId)
  149. ->willReturn(null);
  150. $this->helperMock->expects($this->never())->method('isMessagesAllowed');
  151. try {
  152. $this->orderItemRepository->get($orderId, $orderItemId);
  153. $this->fail('Expected NoSuchEntityException not caught');
  154. } catch (NoSuchEntityException $exception) {
  155. $this->assertEquals(
  156. 'No item with the provided ID was found in the Order. Verify the ID and try again.',
  157. $exception->getMessage()
  158. );
  159. }
  160. }
  161. /**
  162. * @covers \Magento\GiftMessage\Model\OrderItemRepository::get
  163. */
  164. public function testGetNoSuchEntityExceptionOnIsMessageAllowed()
  165. {
  166. $orderId = 1;
  167. $orderItemId = 2;
  168. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  169. ->disableOriginalConstructor()
  170. ->setMethods(['getGiftMessageId'])
  171. ->getMock();
  172. $this->orderFactoryMock->expects($this->once())
  173. ->method('create')
  174. ->willReturn($this->orderMock);
  175. $this->orderMock->expects($this->once())
  176. ->method('load')
  177. ->willReturnSelf();
  178. $this->orderMock->expects($this->once())
  179. ->method('getItemById')
  180. ->with($orderItemId)
  181. ->willReturn($orderItemMock);
  182. $this->helperMock->expects($this->once())
  183. ->method('isMessagesAllowed')
  184. ->with('order_item', $orderItemMock, $this->storeMock)
  185. ->willReturn(false);
  186. $orderItemMock->expects($this->never())->method('getGiftMessageId');
  187. try {
  188. $this->orderItemRepository->get($orderId, $orderItemId);
  189. $this->fail('Expected NoSuchEntityException not caught');
  190. } catch (NoSuchEntityException $exception) {
  191. $this->assertEquals(
  192. "No item with the provided ID was found in the Order, or a gift message isn't allowed. "
  193. . "Verify and try again.",
  194. $exception->getMessage()
  195. );
  196. }
  197. }
  198. /**
  199. * @covers \Magento\GiftMessage\Model\OrderItemRepository::get
  200. */
  201. public function testGetNoSuchEntityExceptionOnGetGiftMessageId()
  202. {
  203. $orderId = 1;
  204. $orderItemId = 2;
  205. $messageId = null;
  206. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  207. ->disableOriginalConstructor()
  208. ->setMethods(['getGiftMessageId'])
  209. ->getMock();
  210. $this->orderFactoryMock->expects($this->once())
  211. ->method('create')
  212. ->willReturn($this->orderMock);
  213. $this->orderMock->expects($this->once())
  214. ->method('load')
  215. ->willReturnSelf();
  216. $this->orderMock->expects($this->once())
  217. ->method('getItemById')
  218. ->with($orderItemId)
  219. ->willReturn($orderItemMock);
  220. $this->helperMock->expects($this->once())
  221. ->method('isMessagesAllowed')
  222. ->with('order_item', $orderItemMock, $this->storeMock)
  223. ->willReturn(true);
  224. $orderItemMock->expects($this->once())
  225. ->method('getGiftMessageId')
  226. ->willReturn($messageId);
  227. $this->messageFactoryMock->expects($this->never())->method('create');
  228. try {
  229. $this->orderItemRepository->get($orderId, $orderItemId);
  230. $this->fail('Expected NoSuchEntityException not caught');
  231. } catch (NoSuchEntityException $exception) {
  232. $this->assertEquals(
  233. 'No item with the provided ID was found in the Order. Verify the ID and try again.',
  234. $exception->getMessage()
  235. );
  236. }
  237. }
  238. /**
  239. * @covers \Magento\GiftMessage\Model\OrderItemRepository::save
  240. */
  241. public function testSave()
  242. {
  243. $orderId = 1;
  244. $orderItemId = 2;
  245. $message[$orderItemId] = [
  246. 'type' => 'order_item',
  247. 'sender' => 'sender_value',
  248. 'recipient' => 'recipient_value',
  249. 'message' => 'message_value',
  250. ];
  251. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  252. ->disableOriginalConstructor()
  253. ->setMethods(['getGiftMessageId'])
  254. ->getMock();
  255. $messageMock = $this->getMockBuilder(\Magento\GiftMessage\Api\Data\MessageInterface::class)
  256. ->disableOriginalConstructor()
  257. ->getMock();
  258. $this->orderFactoryMock->expects($this->any())
  259. ->method('create')
  260. ->willReturn($this->orderMock);
  261. $this->orderMock->expects($this->any())
  262. ->method('load')
  263. ->willReturnSelf();
  264. $this->orderMock->expects($this->once())
  265. ->method('getItemById')
  266. ->with($orderItemId)
  267. ->willReturn($orderItemMock);
  268. $this->orderMock->expects($this->once())
  269. ->method('getIsVirtual')
  270. ->willReturn(false);
  271. $this->helperMock->expects($this->once())
  272. ->method('isMessagesAllowed')
  273. ->with('order_item', $orderItemMock, $this->storeMock)
  274. ->willReturn(true);
  275. $messageMock->expects($this->once())
  276. ->method('getSender')
  277. ->willReturn('sender_value');
  278. $messageMock->expects($this->once())
  279. ->method('getRecipient')
  280. ->willReturn('recipient_value');
  281. $messageMock->expects($this->once())
  282. ->method('getMessage')
  283. ->willReturn('message_value');
  284. $this->giftMessageSaveModelMock->expects($this->once())
  285. ->method('setGiftmessages')
  286. ->with($message);
  287. $this->giftMessageSaveModelMock->expects($this->once())
  288. ->method('saveAllInOrder');
  289. $this->assertTrue($this->orderItemRepository->save($orderId, $orderItemId, $messageMock));
  290. }
  291. /**
  292. * @covers \Magento\GiftMessage\Model\OrderItemRepository::save
  293. */
  294. public function testSaveNoSuchEntityException()
  295. {
  296. $orderId = 1;
  297. $orderItemId = 2;
  298. $messageMock = $this->getMockBuilder(\Magento\GiftMessage\Api\Data\MessageInterface::class)
  299. ->disableOriginalConstructor()
  300. ->getMock();
  301. $this->orderFactoryMock->expects($this->any())
  302. ->method('create')
  303. ->willReturn($this->orderMock);
  304. $this->orderMock->expects($this->any())
  305. ->method('load')
  306. ->willReturnSelf();
  307. $this->orderMock->expects($this->once())
  308. ->method('getItemById')
  309. ->with($orderItemId)
  310. ->willReturn(null);
  311. $this->orderMock->expects($this->never())
  312. ->method('getIsVirtual');
  313. try {
  314. $this->orderItemRepository->save($orderId, $orderItemId, $messageMock);
  315. $this->fail('Expected NoSuchEntityException not caught');
  316. } catch (NoSuchEntityException $exception) {
  317. $this->assertEquals(
  318. 'No item with the provided ID was found in the Order. Verify the ID and try again.',
  319. $exception->getMessage()
  320. );
  321. }
  322. }
  323. /**
  324. * @covers \Magento\GiftMessage\Model\OrderItemRepository::save
  325. */
  326. public function testSaveInvalidTransitionException()
  327. {
  328. $orderId = 1;
  329. $orderItemId = 2;
  330. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  331. ->disableOriginalConstructor()
  332. ->setMethods(['getGiftMessageId'])
  333. ->getMock();
  334. $messageMock = $this->getMockBuilder(\Magento\GiftMessage\Api\Data\MessageInterface::class)
  335. ->disableOriginalConstructor()
  336. ->getMock();
  337. $this->orderFactoryMock->expects($this->any())
  338. ->method('create')
  339. ->willReturn($this->orderMock);
  340. $this->orderMock->expects($this->any())
  341. ->method('load')
  342. ->willReturnSelf();
  343. $this->orderMock->expects($this->once())
  344. ->method('getItemById')
  345. ->with($orderItemId)
  346. ->willReturn($orderItemMock);
  347. $this->orderMock->expects($this->once())
  348. ->method('getIsVirtual')
  349. ->willReturn(true);
  350. $this->helperMock->expects($this->never())
  351. ->method('isMessagesAllowed');
  352. try {
  353. $this->orderItemRepository->save($orderId, $orderItemId, $messageMock);
  354. $this->fail('Expected InvalidTransitionException not caught');
  355. } catch (InvalidTransitionException $exception) {
  356. $this->assertEquals("Gift messages can't be used for virtual products.", $exception->getMessage());
  357. }
  358. }
  359. /**
  360. * @covers \Magento\GiftMessage\Model\OrderItemRepository::save
  361. */
  362. public function testSaveCouldNotSaveException()
  363. {
  364. $orderId = 1;
  365. $orderItemId = 2;
  366. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  367. ->disableOriginalConstructor()
  368. ->setMethods(['getGiftMessageId'])
  369. ->getMock();
  370. $messageMock = $this->getMockBuilder(\Magento\GiftMessage\Api\Data\MessageInterface::class)
  371. ->disableOriginalConstructor()
  372. ->getMock();
  373. $this->orderFactoryMock->expects($this->any())
  374. ->method('create')
  375. ->willReturn($this->orderMock);
  376. $this->orderMock->expects($this->any())
  377. ->method('load')
  378. ->willReturnSelf();
  379. $this->orderMock->expects($this->once())
  380. ->method('getItemById')
  381. ->with($orderItemId)
  382. ->willReturn($orderItemMock);
  383. $this->orderMock->expects($this->once())
  384. ->method('getIsVirtual')
  385. ->willReturn(false);
  386. $this->helperMock->expects($this->once())
  387. ->method('isMessagesAllowed')
  388. ->with('order_item', $orderItemMock, $this->storeMock)
  389. ->willReturn(false);
  390. $messageMock->expects($this->never())
  391. ->method('getSender');
  392. try {
  393. $this->orderItemRepository->save($orderId, $orderItemId, $messageMock);
  394. $this->fail('Expected CouldNotSaveException not caught');
  395. } catch (CouldNotSaveException $exception) {
  396. $this->assertEquals("The gift message isn't available.", $exception->getMessage());
  397. }
  398. }
  399. /**
  400. * @covers \Magento\GiftMessage\Model\OrderItemRepository::save
  401. */
  402. public function testSaveCouldNotSaveExceptionOnSaveAllInOrder()
  403. {
  404. $orderId = 1;
  405. $orderItemId = 2;
  406. $message[$orderItemId] = [
  407. 'type' => 'order_item',
  408. 'sender' => 'sender_value',
  409. 'recipient' => 'recipient_value',
  410. 'message' => 'message_value',
  411. ];
  412. $excep = new \Exception('Exception message');
  413. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  414. ->disableOriginalConstructor()
  415. ->setMethods(['getGiftMessageId'])
  416. ->getMock();
  417. $messageMock = $this->getMockBuilder(\Magento\GiftMessage\Api\Data\MessageInterface::class)
  418. ->disableOriginalConstructor()
  419. ->getMock();
  420. $this->orderFactoryMock->expects($this->any())
  421. ->method('create')
  422. ->willReturn($this->orderMock);
  423. $this->orderMock->expects($this->any())
  424. ->method('load')
  425. ->willReturnSelf();
  426. $this->orderMock->expects($this->once())
  427. ->method('getItemById')
  428. ->with($orderItemId)
  429. ->willReturn($orderItemMock);
  430. $this->orderMock->expects($this->once())
  431. ->method('getIsVirtual')
  432. ->willReturn(false);
  433. $this->helperMock->expects($this->once())
  434. ->method('isMessagesAllowed')
  435. ->with('order_item', $orderItemMock, $this->storeMock)
  436. ->willReturn(true);
  437. $messageMock->expects($this->once())
  438. ->method('getSender')
  439. ->willReturn('sender_value');
  440. $messageMock->expects($this->once())
  441. ->method('getRecipient')
  442. ->willReturn('recipient_value');
  443. $messageMock->expects($this->once())
  444. ->method('getMessage')
  445. ->willReturn('message_value');
  446. $this->giftMessageSaveModelMock->expects($this->once())
  447. ->method('setGiftmessages')
  448. ->with($message);
  449. $this->giftMessageSaveModelMock->expects($this->once())
  450. ->method('saveAllInOrder')
  451. ->will($this->throwException($excep));
  452. try {
  453. $this->orderItemRepository->save($orderId, $orderItemId, $messageMock);
  454. $this->fail('Expected CouldNotSaveException not caught');
  455. } catch (CouldNotSaveException $exception) {
  456. $this->assertEquals(
  457. 'The gift message couldn\'t be added to the "' . $excep->getMessage() . '" order.',
  458. $exception->getMessage()
  459. );
  460. }
  461. }
  462. }