77
88namespace Magento \GiftMessage \Test \Unit \Model \Plugin ;
99
10- use Magento \Framework \Exception \NoSuchEntityException ;
1110use Magento \GiftMessage \Api \Data \MessageInterface ;
1211use Magento \GiftMessage \Api \OrderItemRepositoryInterface ;
1312use Magento \GiftMessage \Api \OrderRepositoryInterface ;
1413use Magento \GiftMessage \Model \Plugin \OrderGet ;
1514use Magento \Sales \Api \Data \OrderExtension ;
16- use Magento \Sales \Api \Data \OrderExtensionFactory ;
1715use Magento \Sales \Api \Data \OrderInterface ;
1816use Magento \Sales \Api \Data \OrderItemExtension ;
19- use Magento \Sales \Api \Data \OrderItemExtensionFactory ;
2017use Magento \Sales \Api \Data \OrderItemInterface ;
2118use Magento \Sales \Model \ResourceModel \Order \Collection ;
2219use PHPUnit \Framework \MockObject \MockObject ;
23- use PHPUnit \Framework \MockObject \RuntimeException ;
2420use PHPUnit \Framework \TestCase ;
2521
2622/**
@@ -43,16 +39,6 @@ class OrderGetTest extends TestCase
4339 */
4440 private $ giftMessageOrderItemRepositoryMock ;
4541
46- /**
47- * @var MockObject
48- */
49- private $ orderExtensionFactoryMock ;
50-
51- /**
52- * @var MockObject
53- */
54- private $ orderItemExtensionFactoryMock ;
55-
5642 /**
5743 * @var MockObject
5844 */
@@ -96,25 +82,19 @@ protected function setUp(): void
9682 $ this ->giftMessageOrderItemRepositoryMock = $ this ->createMock (
9783 OrderItemRepositoryInterface::class
9884 );
99- $ this ->orderExtensionFactoryMock = $ this ->createPartialMock (
100- OrderExtensionFactory::class,
101- ['create ' ]
102- );
103- $ this ->orderItemExtensionFactoryMock = $ this ->createPartialMock (
104- OrderItemExtensionFactory::class,
105- ['create ' ]
106- );
107- $ this ->orderMock = $ this ->createMock (
108- OrderInterface::class
109- );
110- $ this ->orderExtensionMock = $ this ->getOrderExtensionMock ();
85+ $ this ->orderMock = $ this ->getMockBuilder (OrderInterface::class)
86+ ->disableOriginalConstructor ()
87+ ->addMethods (['getGiftMessageId ' ])
88+ ->getMockForAbstractClass ();
89+ $ this ->orderExtensionMock = $ this ->createMock (OrderExtension::class);
11190 $ this ->giftMessageMock = $ this ->createMock (
11291 MessageInterface::class
11392 );
114- $ this ->orderItemMock = $ this ->createMock (
115- OrderItemInterface::class
116- );
117- $ this ->orderItemExtensionMock = $ this ->getOrderItemExtensionMock ();
93+ $ this ->orderItemMock = $ this ->getMockBuilder (OrderItemInterface::class)
94+ ->disableOriginalConstructor ()
95+ ->addMethods (['getGiftMessageId ' ])
96+ ->getMockForAbstractClass ();
97+ $ this ->orderItemExtensionMock = $ this ->createMock (OrderItemExtension::class);
11898 $ this ->orderRepositoryMock = $ this ->createMock (
11999 \Magento \Sales \Api \OrderRepositoryInterface::class
120100 );
@@ -123,16 +103,19 @@ protected function setUp(): void
123103
124104 $ this ->plugin = new OrderGet (
125105 $ this ->giftMessageOrderRepositoryMock ,
126- $ this ->giftMessageOrderItemRepositoryMock ,
127- $ this ->orderExtensionFactoryMock ,
128- $ this ->orderItemExtensionFactoryMock
106+ $ this ->giftMessageOrderItemRepositoryMock
129107 );
130108 }
131109
132- public function testAfterGetGiftMessageOnOrderLevel ()
110+ /**
111+ * @return void
112+ */
113+ public function testAfterGetGiftMessageOnOrderLevel (): void
133114 {
134115 //set Gift Message for Order
135116 $ orderId = 1 ;
117+ $ messageId = 1 ;
118+ $ this ->orderMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn ($ messageId );
136119 $ this ->orderMock ->expects ($ this ->once ())->method ('getEntityId ' )->willReturn ($ orderId );
137120 $ this ->orderMock
138121 ->expects ($ this ->once ())
@@ -160,12 +143,17 @@ public function testAfterGetGiftMessageOnOrderLevel()
160143 $ this ->plugin ->afterGet ($ this ->orderRepositoryMock , $ this ->orderMock );
161144 }
162145
163- public function testAfterGetGiftMessageOnItemLevel ()
146+ /**
147+ * @return void
148+ */
149+ public function testAfterGetGiftMessageOnItemLevel (): void
164150 {
165151 //set Gift Message for Order
166152 $ orderId = 1 ;
167153 $ orderItemId = 2 ;
154+ $ messageId = 1 ;
168155 $ this ->orderItemMock ->expects ($ this ->once ())->method ('getItemId ' )->willReturn ($ orderItemId );
156+ $ this ->orderItemMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn ($ messageId );
169157 $ this ->orderMock ->expects ($ this ->once ())->method ('getEntityId ' )->willReturn ($ orderId );
170158 $ this ->orderMock
171159 ->expects ($ this ->once ())
@@ -198,23 +186,25 @@ public function testAfterGetGiftMessageOnItemLevel()
198186 $ this ->plugin ->afterGet ($ this ->orderRepositoryMock , $ this ->orderMock );
199187 }
200188
201- public function testGetAfterWhenMessagesAreNotSet ()
189+ /**
190+ * @return void
191+ */
192+ public function testGetAfterWhenMessagesAreNotSet (): void
202193 {
203194 $ orderId = 1 ;
204195 $ orderItemId = 2 ;
205196 //set Gift Message for Order
206- $ this ->orderMock ->expects ($ this ->exactly (2 ))->method ('getEntityId ' )->willReturn ($ orderId );
207- $ this ->orderItemMock ->expects ($ this ->once ())->method ('getItemId ' )->willReturn ($ orderItemId );
197+ $ this ->orderMock ->expects ($ this ->never ())->method ('getEntityId ' );
198+ $ this ->orderItemMock ->expects ($ this ->never ())->method ('getItemId ' );
199+ $ this ->orderItemMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn (null );
208200 $ this ->orderMock
209201 ->expects ($ this ->once ())
210202 ->method ('getExtensionAttributes ' )
211203 ->willReturn ($ this ->orderExtensionMock );
212204 $ this ->orderExtensionMock ->expects ($ this ->once ())->method ('getGiftMessage ' )->willReturn ([]);
213205 $ this ->giftMessageOrderRepositoryMock
214- ->expects ($ this ->once ())
215- ->method ('get ' )
216- ->with ($ orderId )
217- ->willThrowException (new NoSuchEntityException ());
206+ ->expects ($ this ->never ())
207+ ->method ('get ' );
218208 $ this ->orderExtensionMock
219209 ->expects ($ this ->never ())
220210 ->method ('setGiftMessage ' );
@@ -227,22 +217,25 @@ public function testGetAfterWhenMessagesAreNotSet()
227217 ->willReturn ($ this ->orderItemExtensionMock );
228218 $ this ->orderItemExtensionMock ->expects ($ this ->once ())->method ('getGiftMessage ' )->willReturn ([]);
229219 $ this ->giftMessageOrderItemRepositoryMock
230- ->expects ($ this ->once ())
231- ->method ('get ' )
232- ->with ($ orderId , $ orderItemId )
233- ->willThrowException (new NoSuchEntityException ());
220+ ->expects ($ this ->never ())
221+ ->method ('get ' );
234222 $ this ->orderItemExtensionMock
235223 ->expects ($ this ->never ())
236224 ->method ('setGiftMessage ' );
237225
238226 $ this ->plugin ->afterGet ($ this ->orderRepositoryMock , $ this ->orderMock );
239227 }
240228
241- public function testAfterGetList ()
229+ /**
230+ * @return void
231+ */
232+ public function testAfterGetList (): void
242233 {
243234 //set Gift Message List for Order
244235 $ orderId = 1 ;
236+ $ messageId = 1 ;
245237 $ this ->orderMock ->expects ($ this ->once ())->method ('getEntityId ' )->willReturn ($ orderId );
238+ $ this ->orderMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn ($ messageId );
246239 $ this ->orderMock
247240 ->expects ($ this ->once ())
248241 ->method ('getExtensionAttributes ' )
@@ -269,38 +262,4 @@ public function testAfterGetList()
269262 $ this ->collectionMock ->expects ($ this ->once ())->method ('getItems ' )->willReturn ([$ this ->orderMock ]);
270263 $ this ->plugin ->afterGetList ($ this ->orderRepositoryMock , $ this ->collectionMock );
271264 }
272-
273- /**
274- * Build order extension mock.
275- *
276- * @return MockObject
277- */
278- private function getOrderExtensionMock (): MockObject
279- {
280- $ mockObject = $ this ->getMockBuilder (OrderExtension::class);
281- try {
282- $ mockObject ->addMethods (['getGiftMessage ' , 'setGiftMessage ' ]);
283- } catch (RuntimeException $ e ) {
284- // Order extension already generated.
285- }
286-
287- return $ mockObject ->getMock ();
288- }
289-
290- /**
291- * Build order item extension mock.
292- *
293- * @return MockObject
294- */
295- private function getOrderItemExtensionMock (): MockObject
296- {
297- $ mockObject = $ this ->getMockBuilder (OrderItemExtension::class);
298- try {
299- $ mockObject ->addMethods (['getGiftMessage ' , 'setGiftMessage ' ]);
300- } catch (RuntimeException $ e ) {
301- // Order extension already generated.
302- }
303-
304- return $ mockObject ->getMock ();
305- }
306265}
0 commit comments