1818
1919namespace Magento \Sales \Test \Unit \Model \Reorder ;
2020
21- use Magento \Catalog \Api \Data \ProductInterface ;
2221use Magento \Catalog \Model \ResourceModel \Product \Collection ;
2322use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory as ProductCollectionFactory ;
2423use Magento \Customer \Model \Session as CustomerSession ;
25- use Magento \Framework \DataObject ;
2624use Magento \Framework \Exception \AlreadyExistsException ;
2725use Magento \Framework \Exception \CouldNotSaveException ;
2826use Magento \Framework \Exception \InputException ;
2927use Magento \Framework \Exception \LocalizedException ;
3028use Magento \Framework \Exception \NoSuchEntityException ;
3129use Magento \Quote \Api \CartRepositoryInterface ;
30+ use Magento \Quote \Api \Data \CartInterface ;
3231use Magento \Quote \Model \Cart \CustomerCartResolver ;
3332use Magento \Quote \Model \GuestCart \GuestCartResolver ;
3433use Magento \Quote \Model \Quote ;
@@ -221,6 +220,7 @@ protected function setUp(): void
221220 * @throws LocalizedException
222221 * @throws NoSuchEntityException
223222 * @dataProvider dataProvider
223+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
224224 */
225225 public function testExecuteReorder (
226226 string $ orderNumber ,
@@ -230,84 +230,34 @@ public function testExecuteReorder(
230230 bool $ customerIsLoggedIn ,
231231 bool $ isReorderAllowed ,
232232 ): void {
233- $ item1 = $ this ->createPartialMock (
234- Item::class,
235- ['getParentItem ' , 'getProductId ' , 'getId ' ]
236- );
237- $ item1 ->expects ($ this ->any ())
238- ->method ('getParentItem ' )
239- ->willReturn (null );
240- $ item1 ->expects ($ this ->any ())
241- ->method ('getProductId ' )
242- ->willReturn (1 );
243- $ item1 ->expects ($ this ->any ())
244- ->method ('getId ' )
245- ->willReturn (5 );
246- $ item2 = $ this ->createPartialMock (
247- Item::class,
248- ['getParentItem ' , 'getProductId ' , 'getId ' ]
249- );
250- $ item2 ->expects ($ this ->any ())
251- ->method ('getParentItem ' )
252- ->willReturn (null );
253- $ item2 ->expects ($ this ->any ())
254- ->method ('getProductId ' )
255- ->willReturn (2 );
256- $ item2 ->expects ($ this ->any ())
257- ->method ('getId ' )
258- ->willReturn (5 );
233+ list ($ item1 , $ item2 ) = $ this ->getCollectionItems ();
259234 $ collection = $ this ->createMock (ItemCollection::class);
260235 $ collection ->expects ($ this ->any ())->method ('getIterator ' )
261236 ->willReturn (new \ArrayIterator ([$ item1 , $ item2 ]));
262- $ this ->order ->expects ($ this ->once ())
237+ $ this ->order ->expects ($ this ->any ())
263238 ->method ('getItemsCollection ' )
264239 ->willReturn ($ collection );
265- $ productCollection = $ this ->getMockBuilder (Collection::class)
266- ->onlyMethods (
267- [
268- 'getItems ' ,
269- 'addIdFilter ' ,
270- 'addStoreFilter ' ,
271- 'addAttributeToSelect ' ,
272- 'joinAttribute ' ,
273- 'addOptionsToResult ' ,
274- 'getIterator ' ,
275- 'setStore '
276- ]
277- )
278- ->addMethods (['getStore ' ])
279- ->disableOriginalConstructor ()
280- ->getMock ();
281- $ productCollection ->expects ($ this ->any ())->method ('setStore ' )->willReturnSelf ();
282- $ productCollection ->expects ($ this ->any ())->method ('addIdFilter ' )->willReturnSelf ();
283- $ productCollection ->expects ($ this ->any ())->method ('addStoreFilter ' )->willReturnSelf ();
284- $ productCollection ->expects ($ this ->once ())->method ('addAttributeToSelect ' )->willReturnSelf ();
285- $ productCollection ->expects ($ this ->any ())->method ('joinAttribute ' )->willReturnSelf ();
286- $ productCollection ->expects ($ this ->once ())->method ('addOptionsToResult ' )->willReturnSelf ();
287- $ this ->productCollectionFactory ->expects ($ this ->once ())
240+ $ productCollection = $ this ->getProductCollection ();
241+ $ this ->productCollectionFactory ->expects ($ this ->any ())
288242 ->method ('create ' )
289243 ->willReturn ($ productCollection );
290- // $product1 = $this->getMockForAbstractClass(ProductInterface::class);
291- // $product2 = $this->getMockForAbstractClass(ProductInterface::class);
292- // $productCollection->expects($this->once())->method('getItems')->willReturn([$product1, $product2]);
293- $ productCollection ->expects ($ this ->once ())->method ('getItems ' )->willReturn ([]);
294- $ this ->orderFactory ->expects ($ this ->once ())
244+ $ this ->orderFactory ->expects ($ this ->any ())
295245 ->method ('create ' )
296246 ->willReturn ($ this ->order );
297- $ this ->order ->expects ($ this ->once ())
247+ $ this ->order ->expects ($ this ->any ())
298248 ->method ('loadByIncrementIdAndStoreId ' )
299249 ->with ($ orderNumber , $ storeId )
300250 ->willReturnSelf ();
301- $ this ->order ->expects ($ this ->once ())
251+ $ this ->order ->expects ($ this ->any ())
302252 ->method ('getId ' )
303253 ->willReturn ($ orderId );
304- $ this ->order ->expects ($ this ->once ())
254+ $ this ->order ->expects ($ this ->any ())
305255 ->method ('getCustomerId ' )
306256 ->willReturn ($ customerId );
307- $ this ->order ->expects ($ this ->once ())
257+ $ this ->order ->expects ($ this ->any ())
308258 ->method ('getStore ' )
309259 ->willReturn ($ this ->store );
310- $ this ->customerSession ->expects ($ this ->once ())
260+ $ this ->customerSession ->expects ($ this ->any ())
311261 ->method ('isLoggedIn ' )
312262 ->willReturn ($ customerIsLoggedIn );
313263 $ this ->guestCartResolver ->expects ($ this ->any ())
@@ -321,35 +271,26 @@ public function testExecuteReorder(
321271 ->method ('isAllowed ' )
322272 ->with ($ this ->store )
323273 ->willReturn ($ isReorderAllowed );
324- $ this ->storeManager ->expects ($ this ->once ())
274+ $ this ->storeManager ->expects ($ this ->any ())
325275 ->method ('getStore ' )
326276 ->willReturn ($ this ->store );
327- $ this ->store ->expects ($ this ->once ())
277+ $ this ->store ->expects ($ this ->any ())
328278 ->method ('getId ' )
329279 ->willReturn ($ storeId );
330- $ this ->cartRepository ->expects ($ this ->once ())
280+ $ this ->cartRepository ->expects ($ this ->any ())
331281 ->method ('save ' )
332282 ->with ($ this ->cart )
333283 ->willReturnSelf ();
334- // $infoBuyRequest = new DataObject(['options' => [
335- // [
336- // 'option_id' => 1,
337- // 'option_value' => 2
338- // ]
339- // ]]);
340- // $this->orderInfoBuyRequestGetter->expects($this->once())
341- // ->method('getInfoBuyRequest')
342- // ->willReturn($infoBuyRequest);
343- $ savedCart = $ this ->getMockBuilder (\Magento \Quote \Api \Data \CartInterface::class)
284+ $ savedCart = $ this ->getMockBuilder (CartInterface::class)
344285 ->disableOriginalConstructor ()
345286 ->addMethods (['setHasError ' ])
346287 ->getMockForAbstractClass ();
347- $ this ->cartRepository ->expects ($ this ->once ())
288+ $ this ->cartRepository ->expects ($ this ->any ())
348289 ->method ('get ' )
349290 ->willReturn ($ savedCart );
350291
351292 $ output = $ this ->reorder ->execute ($ orderNumber , $ storeId );
352- $ this ->assertNotEmpty ($ output );
293+ $ this ->assertNotEmpty ($ output-> getCart () );
353294 }
354295
355296 /**
@@ -358,7 +299,81 @@ public function testExecuteReorder(
358299 public function dataProvider ()
359300 {
360301 return [
361- 'test case 1 ' => ['000001 ' , 1 , '1 ' , 1 , true , true ],
302+ 'test case when reorder is allowed and customer is logged in ' =>
303+ ['000001 ' , 1 , '1 ' , 1 , true , true ],
304+ 'test case when reorder is not allowed ' =>
305+ ['000001 ' , 1 , '1 ' , 1 , true , false ],
362306 ];
363307 }
308+
309+ /**
310+ * Get collection items
311+ *
312+ * @return array
313+ */
314+ private function getCollectionItems (): array
315+ {
316+ $ productId1 = 1 ;
317+ $ productId2 = 5 ;
318+ $ item1 = $ this ->createPartialMock (
319+ Item::class,
320+ ['getParentItem ' , 'getProductId ' , 'getId ' ]
321+ );
322+ $ item1 ->expects ($ this ->any ())
323+ ->method ('getParentItem ' )
324+ ->willReturn (null );
325+ $ item1 ->expects ($ this ->any ())
326+ ->method ('getProductId ' )
327+ ->willReturn ($ productId1 );
328+ $ item1 ->expects ($ this ->any ())
329+ ->method ('getId ' )
330+ ->willReturn ($ productId1 );
331+ $ item2 = $ this ->createPartialMock (
332+ Item::class,
333+ ['getParentItem ' , 'getProductId ' , 'getId ' ]
334+ );
335+ $ item2 ->expects ($ this ->any ())
336+ ->method ('getParentItem ' )
337+ ->willReturn (null );
338+ $ item2 ->expects ($ this ->any ())
339+ ->method ('getProductId ' )
340+ ->willReturn ($ productId2 );
341+ $ item2 ->expects ($ this ->any ())
342+ ->method ('getId ' )
343+ ->willReturn ($ productId2 );
344+ return [$ item1 , $ item2 ];
345+ }
346+
347+ /**
348+ * Get product collection mock
349+ *
350+ * @return MockObject|Collection
351+ */
352+ private function getProductCollection (): MockObject |Collection
353+ {
354+ $ productCollection = $ this ->getMockBuilder (Collection::class)
355+ ->onlyMethods (
356+ [
357+ 'getItems ' ,
358+ 'addIdFilter ' ,
359+ 'addStoreFilter ' ,
360+ 'addAttributeToSelect ' ,
361+ 'joinAttribute ' ,
362+ 'addOptionsToResult ' ,
363+ 'getIterator ' ,
364+ 'setStore '
365+ ]
366+ )
367+ ->addMethods (['getStore ' ])
368+ ->disableOriginalConstructor ()
369+ ->getMock ();
370+ $ productCollection ->expects ($ this ->any ())->method ('setStore ' )->willReturnSelf ();
371+ $ productCollection ->expects ($ this ->any ())->method ('addIdFilter ' )->willReturnSelf ();
372+ $ productCollection ->expects ($ this ->any ())->method ('addStoreFilter ' )->willReturnSelf ();
373+ $ productCollection ->expects ($ this ->any ())->method ('addAttributeToSelect ' )->willReturnSelf ();
374+ $ productCollection ->expects ($ this ->any ())->method ('joinAttribute ' )->willReturnSelf ();
375+ $ productCollection ->expects ($ this ->any ())->method ('addOptionsToResult ' )->willReturnSelf ();
376+ $ productCollection ->expects ($ this ->any ())->method ('getItems ' )->willReturn ([]);
377+ return $ productCollection ;
378+ }
364379}
0 commit comments