OrderGetTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\Plugin;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class OrderGetTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\GiftMessage\Model\Plugin\OrderGet
  14. */
  15. private $plugin;
  16. /**
  17. * @var \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $giftMessageOrderRepositoryMock;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $giftMessageOrderItemRepositoryMock;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $orderExtensionFactoryMock;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $orderItemExtensionFactoryMock;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $orderMock;
  36. /**
  37. * @var \PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $orderExtensionMock;
  40. /**
  41. * @var \PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $giftMessageMock;
  44. /**
  45. * @var \PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $orderItemMock;
  48. /**
  49. * @var \PHPUnit_Framework_MockObject_MockObject
  50. */
  51. private $orderItemExtensionMock;
  52. /**
  53. * @var \PHPUnit_Framework_MockObject_MockObject
  54. */
  55. private $orderRepositoryMock;
  56. /**
  57. * @var \PHPUnit_Framework_MockObject_MockObject
  58. */
  59. private $collectionMock;
  60. public function setUp()
  61. {
  62. $this->giftMessageOrderRepositoryMock = $this->createMock(
  63. \Magento\GiftMessage\Api\OrderRepositoryInterface::class
  64. );
  65. $this->giftMessageOrderItemRepositoryMock = $this->createMock(
  66. \Magento\GiftMessage\Api\OrderItemRepositoryInterface::class
  67. );
  68. $this->orderExtensionFactoryMock = $this->createPartialMock(
  69. \Magento\Sales\Api\Data\OrderExtensionFactory::class,
  70. ['create']
  71. );
  72. $this->orderItemExtensionFactoryMock = $this->createPartialMock(
  73. \Magento\Sales\Api\Data\OrderItemExtensionFactory::class,
  74. ['create']
  75. );
  76. $this->orderMock = $this->createMock(
  77. \Magento\Sales\Api\Data\OrderInterface::class
  78. );
  79. $this->orderExtensionMock = $this->createPartialMock(
  80. \Magento\Sales\Api\Data\OrderExtension::class,
  81. ['getGiftMessage', 'setGiftMessage']
  82. );
  83. $this->giftMessageMock = $this->createMock(
  84. \Magento\GiftMessage\Api\Data\MessageInterface::class
  85. );
  86. $this->orderItemMock = $this->createMock(
  87. \Magento\Sales\Api\Data\OrderItemInterface::class
  88. );
  89. $this->orderItemExtensionMock = $this->createPartialMock(
  90. \Magento\Sales\Api\Data\OrderItemExtension::class,
  91. ['setGiftMessage', 'getGiftMessage']
  92. );
  93. $this->orderRepositoryMock = $this->createMock(
  94. \Magento\Sales\Api\OrderRepositoryInterface::class
  95. );
  96. $this->collectionMock = $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class);
  97. $this->plugin = new \Magento\GiftMessage\Model\Plugin\OrderGet(
  98. $this->giftMessageOrderRepositoryMock,
  99. $this->giftMessageOrderItemRepositoryMock,
  100. $this->orderExtensionFactoryMock,
  101. $this->orderItemExtensionFactoryMock
  102. );
  103. }
  104. public function testAfterGetGiftMessageOnOrderLevel()
  105. {
  106. //set Gift Message for Order
  107. $orderId = 1;
  108. $this->orderMock->expects($this->once())->method('getEntityId')->willReturn($orderId);
  109. $this->orderMock
  110. ->expects($this->once())
  111. ->method('getExtensionAttributes')
  112. ->willReturn($this->orderExtensionMock);
  113. $this->orderExtensionMock->expects($this->once())->method('getGiftMessage')->willReturn([]);
  114. $this->giftMessageOrderRepositoryMock
  115. ->expects($this->once())
  116. ->method('get')
  117. ->with($orderId)
  118. ->willReturn($this->giftMessageMock);
  119. $this->orderExtensionMock
  120. ->expects($this->once())
  121. ->method('setGiftMessage')
  122. ->with($this->giftMessageMock)
  123. ->willReturnSelf();
  124. $this->orderMock
  125. ->expects($this->once())
  126. ->method('setExtensionAttributes')
  127. ->with($this->orderExtensionMock)
  128. ->willReturnSelf();
  129. // set Gift Message on Item Level
  130. $this->orderMock->expects($this->once())->method('getItems')->willReturn([]);
  131. $this->plugin->afterGet($this->orderRepositoryMock, $this->orderMock);
  132. }
  133. public function testAfterGetGiftMessageOnItemLevel()
  134. {
  135. //set Gift Message for Order
  136. $orderId = 1;
  137. $orderItemId = 2;
  138. $this->orderItemMock->expects($this->once())->method('getItemId')->willReturn($orderItemId);
  139. $this->orderMock->expects($this->once())->method('getEntityId')->willReturn($orderId);
  140. $this->orderMock
  141. ->expects($this->once())
  142. ->method('getExtensionAttributes')
  143. ->willReturn($this->orderExtensionMock);
  144. $this->orderExtensionMock->expects($this->once())->method('getGiftMessage')->willReturn($this->giftMessageMock);
  145. // set Gift Message on Item Level
  146. $this->orderMock->expects($this->once())->method('getItems')->willReturn([$this->orderItemMock]);
  147. $this->orderItemMock
  148. ->expects($this->once())
  149. ->method('getExtensionAttributes')
  150. ->willReturn($this->orderItemExtensionMock);
  151. $this->orderItemExtensionMock->expects($this->once())->method('getGiftMessage')->willReturn([]);
  152. $this->giftMessageOrderItemRepositoryMock
  153. ->expects($this->once())
  154. ->method('get')
  155. ->with($orderId, $orderItemId)
  156. ->willReturn($this->giftMessageMock);
  157. $this->orderItemExtensionMock
  158. ->expects($this->once())
  159. ->method('setGiftMessage')
  160. ->with($this->giftMessageMock)
  161. ->willReturnSelf();
  162. $this->orderItemMock
  163. ->expects($this->once())
  164. ->method('setExtensionAttributes')
  165. ->with($this->orderItemExtensionMock)
  166. ->willReturnSelf();
  167. $this->plugin->afterGet($this->orderRepositoryMock, $this->orderMock);
  168. }
  169. public function testGetAfterWhenMessagesAreNotSet()
  170. {
  171. $orderId = 1;
  172. $orderItemId = 2;
  173. //set Gift Message for Order
  174. $this->orderMock->expects($this->exactly(2))->method('getEntityId')->willReturn($orderId);
  175. $this->orderItemMock->expects($this->once())->method('getItemId')->willReturn($orderItemId);
  176. $this->orderMock
  177. ->expects($this->once())
  178. ->method('getExtensionAttributes')
  179. ->willReturn($this->orderExtensionMock);
  180. $this->orderExtensionMock->expects($this->once())->method('getGiftMessage')->willReturn([]);
  181. $this->giftMessageOrderRepositoryMock
  182. ->expects($this->once())
  183. ->method('get')
  184. ->with($orderId)
  185. ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException());
  186. $this->orderExtensionMock
  187. ->expects($this->never())
  188. ->method('setGiftMessage');
  189. // set Gift Message on Item Level
  190. $this->orderMock->expects($this->once())->method('getItems')->willReturn([$this->orderItemMock]);
  191. $this->orderItemMock
  192. ->expects($this->once())
  193. ->method('getExtensionAttributes')
  194. ->willReturn($this->orderItemExtensionMock);
  195. $this->orderItemExtensionMock->expects($this->once())->method('getGiftMessage')->willReturn([]);
  196. $this->giftMessageOrderItemRepositoryMock
  197. ->expects($this->once())
  198. ->method('get')
  199. ->with($orderId, $orderItemId)
  200. ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException());
  201. $this->orderItemExtensionMock
  202. ->expects($this->never())
  203. ->method('setGiftMessage');
  204. $this->plugin->afterGet($this->orderRepositoryMock, $this->orderMock);
  205. }
  206. public function testAfterGetList()
  207. {
  208. //set Gift Message List for Order
  209. $orderId = 1;
  210. $this->orderMock->expects($this->once())->method('getEntityId')->willReturn($orderId);
  211. $this->orderMock
  212. ->expects($this->once())
  213. ->method('getExtensionAttributes')
  214. ->willReturn($this->orderExtensionMock);
  215. $this->orderExtensionMock->expects($this->once())->method('getGiftMessage')->willReturn([]);
  216. $this->giftMessageOrderRepositoryMock
  217. ->expects($this->once())
  218. ->method('get')
  219. ->with($orderId)
  220. ->willReturn($this->giftMessageMock);
  221. $this->orderExtensionMock
  222. ->expects($this->once())
  223. ->method('setGiftMessage')
  224. ->with($this->giftMessageMock)
  225. ->willReturnSelf();
  226. $this->orderMock
  227. ->expects($this->once())
  228. ->method('setExtensionAttributes')
  229. ->with($this->orderExtensionMock)
  230. ->willReturnSelf();
  231. // set Gift Message on Item Level
  232. $this->orderMock->expects($this->once())->method('getItems')->willReturn([]);
  233. $this->collectionMock->expects($this->once())->method('getItems')->willReturn([$this->orderMock]);
  234. $this->plugin->afterGetList($this->orderRepositoryMock, $this->collectionMock);
  235. }
  236. }