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,25 @@ 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 ->getMockBuilder (OrderExtension::class)
90+ ->disableOriginalConstructor ()
91+ ->addMethods (['getGiftMessage ' , 'setGiftMessage ' ])
92+ ->getMock ();
11193 $ this ->giftMessageMock = $ this ->createMock (
11294 MessageInterface::class
11395 );
114- $ this ->orderItemMock = $ this ->createMock (
115- OrderItemInterface::class
116- );
117- $ this ->orderItemExtensionMock = $ this ->getOrderItemExtensionMock ();
96+ $ this ->orderItemMock = $ this ->getMockBuilder (OrderItemInterface::class)
97+ ->disableOriginalConstructor ()
98+ ->addMethods (['getGiftMessageId ' ])
99+ ->getMockForAbstractClass ();
100+ $ this ->orderItemExtensionMock = $ this ->getMockBuilder (OrderItemExtension::class)
101+ ->disableOriginalConstructor ()
102+ ->addMethods (['getGiftMessage ' , 'setGiftMessage ' ])
103+ ->getMock ();
118104 $ this ->orderRepositoryMock = $ this ->createMock (
119105 \Magento \Sales \Api \OrderRepositoryInterface::class
120106 );
@@ -123,16 +109,19 @@ protected function setUp(): void
123109
124110 $ this ->plugin = new OrderGet (
125111 $ this ->giftMessageOrderRepositoryMock ,
126- $ this ->giftMessageOrderItemRepositoryMock ,
127- $ this ->orderExtensionFactoryMock ,
128- $ this ->orderItemExtensionFactoryMock
112+ $ this ->giftMessageOrderItemRepositoryMock
129113 );
130114 }
131115
132- public function testAfterGetGiftMessageOnOrderLevel ()
116+ /**
117+ * @return void
118+ */
119+ public function testAfterGetGiftMessageOnOrderLevel (): void
133120 {
134121 //set Gift Message for Order
135122 $ orderId = 1 ;
123+ $ messageId = 1 ;
124+ $ this ->orderMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn ($ messageId );
136125 $ this ->orderMock ->expects ($ this ->once ())->method ('getEntityId ' )->willReturn ($ orderId );
137126 $ this ->orderMock
138127 ->expects ($ this ->once ())
@@ -160,12 +149,17 @@ public function testAfterGetGiftMessageOnOrderLevel()
160149 $ this ->plugin ->afterGet ($ this ->orderRepositoryMock , $ this ->orderMock );
161150 }
162151
163- public function testAfterGetGiftMessageOnItemLevel ()
152+ /**
153+ * @return void
154+ */
155+ public function testAfterGetGiftMessageOnItemLevel (): void
164156 {
165157 //set Gift Message for Order
166158 $ orderId = 1 ;
167159 $ orderItemId = 2 ;
160+ $ messageId = 1 ;
168161 $ this ->orderItemMock ->expects ($ this ->once ())->method ('getItemId ' )->willReturn ($ orderItemId );
162+ $ this ->orderItemMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn ($ messageId );
169163 $ this ->orderMock ->expects ($ this ->once ())->method ('getEntityId ' )->willReturn ($ orderId );
170164 $ this ->orderMock
171165 ->expects ($ this ->once ())
@@ -198,23 +192,23 @@ public function testAfterGetGiftMessageOnItemLevel()
198192 $ this ->plugin ->afterGet ($ this ->orderRepositoryMock , $ this ->orderMock );
199193 }
200194
201- public function testGetAfterWhenMessagesAreNotSet ()
195+ /**
196+ * @return void
197+ */
198+ public function testGetAfterWhenMessagesAreNotSet (): void
202199 {
203- $ orderId = 1 ;
204- $ orderItemId = 2 ;
205200 //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 );
201+ $ this ->orderMock ->expects ($ this ->never ())->method ('getEntityId ' );
202+ $ this ->orderItemMock ->expects ($ this ->never ())->method ('getItemId ' );
203+ $ this ->orderItemMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn (null );
208204 $ this ->orderMock
209205 ->expects ($ this ->once ())
210206 ->method ('getExtensionAttributes ' )
211207 ->willReturn ($ this ->orderExtensionMock );
212208 $ this ->orderExtensionMock ->expects ($ this ->once ())->method ('getGiftMessage ' )->willReturn ([]);
213209 $ this ->giftMessageOrderRepositoryMock
214- ->expects ($ this ->once ())
215- ->method ('get ' )
216- ->with ($ orderId )
217- ->willThrowException (new NoSuchEntityException ());
210+ ->expects ($ this ->never ())
211+ ->method ('get ' );
218212 $ this ->orderExtensionMock
219213 ->expects ($ this ->never ())
220214 ->method ('setGiftMessage ' );
@@ -227,22 +221,25 @@ public function testGetAfterWhenMessagesAreNotSet()
227221 ->willReturn ($ this ->orderItemExtensionMock );
228222 $ this ->orderItemExtensionMock ->expects ($ this ->once ())->method ('getGiftMessage ' )->willReturn ([]);
229223 $ this ->giftMessageOrderItemRepositoryMock
230- ->expects ($ this ->once ())
231- ->method ('get ' )
232- ->with ($ orderId , $ orderItemId )
233- ->willThrowException (new NoSuchEntityException ());
224+ ->expects ($ this ->never ())
225+ ->method ('get ' );
234226 $ this ->orderItemExtensionMock
235227 ->expects ($ this ->never ())
236228 ->method ('setGiftMessage ' );
237229
238230 $ this ->plugin ->afterGet ($ this ->orderRepositoryMock , $ this ->orderMock );
239231 }
240232
241- public function testAfterGetList ()
233+ /**
234+ * @return void
235+ */
236+ public function testAfterGetList (): void
242237 {
243238 //set Gift Message List for Order
244239 $ orderId = 1 ;
240+ $ messageId = 1 ;
245241 $ this ->orderMock ->expects ($ this ->once ())->method ('getEntityId ' )->willReturn ($ orderId );
242+ $ this ->orderMock ->expects ($ this ->once ())->method ('getGiftMessageId ' )->willReturn ($ messageId );
246243 $ this ->orderMock
247244 ->expects ($ this ->once ())
248245 ->method ('getExtensionAttributes ' )
@@ -269,38 +266,4 @@ public function testAfterGetList()
269266 $ this ->collectionMock ->expects ($ this ->once ())->method ('getItems ' )->willReturn ([$ this ->orderMock ]);
270267 $ this ->plugin ->afterGetList ($ this ->orderRepositoryMock , $ this ->collectionMock );
271268 }
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- }
306269}
0 commit comments