18
18
19
19
namespace Magento \Sales \Test \Unit \Model \Reorder ;
20
20
21
- use Magento \Catalog \Api \Data \ProductInterface ;
22
21
use Magento \Catalog \Model \ResourceModel \Product \Collection ;
23
22
use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory as ProductCollectionFactory ;
24
23
use Magento \Customer \Model \Session as CustomerSession ;
25
- use Magento \Framework \DataObject ;
26
24
use Magento \Framework \Exception \AlreadyExistsException ;
27
25
use Magento \Framework \Exception \CouldNotSaveException ;
28
26
use Magento \Framework \Exception \InputException ;
29
27
use Magento \Framework \Exception \LocalizedException ;
30
28
use Magento \Framework \Exception \NoSuchEntityException ;
31
29
use Magento \Quote \Api \CartRepositoryInterface ;
30
+ use Magento \Quote \Api \Data \CartInterface ;
32
31
use Magento \Quote \Model \Cart \CustomerCartResolver ;
33
32
use Magento \Quote \Model \GuestCart \GuestCartResolver ;
34
33
use Magento \Quote \Model \Quote ;
@@ -221,6 +220,7 @@ protected function setUp(): void
221
220
* @throws LocalizedException
222
221
* @throws NoSuchEntityException
223
222
* @dataProvider dataProvider
223
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
224
224
*/
225
225
public function testExecuteReorder (
226
226
string $ orderNumber ,
@@ -230,84 +230,34 @@ public function testExecuteReorder(
230
230
bool $ customerIsLoggedIn ,
231
231
bool $ isReorderAllowed ,
232
232
): 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 ();
259
234
$ collection = $ this ->createMock (ItemCollection::class);
260
235
$ collection ->expects ($ this ->any ())->method ('getIterator ' )
261
236
->willReturn (new \ArrayIterator ([$ item1 , $ item2 ]));
262
- $ this ->order ->expects ($ this ->once ())
237
+ $ this ->order ->expects ($ this ->any ())
263
238
->method ('getItemsCollection ' )
264
239
->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 ())
288
242
->method ('create ' )
289
243
->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 ())
295
245
->method ('create ' )
296
246
->willReturn ($ this ->order );
297
- $ this ->order ->expects ($ this ->once ())
247
+ $ this ->order ->expects ($ this ->any ())
298
248
->method ('loadByIncrementIdAndStoreId ' )
299
249
->with ($ orderNumber , $ storeId )
300
250
->willReturnSelf ();
301
- $ this ->order ->expects ($ this ->once ())
251
+ $ this ->order ->expects ($ this ->any ())
302
252
->method ('getId ' )
303
253
->willReturn ($ orderId );
304
- $ this ->order ->expects ($ this ->once ())
254
+ $ this ->order ->expects ($ this ->any ())
305
255
->method ('getCustomerId ' )
306
256
->willReturn ($ customerId );
307
- $ this ->order ->expects ($ this ->once ())
257
+ $ this ->order ->expects ($ this ->any ())
308
258
->method ('getStore ' )
309
259
->willReturn ($ this ->store );
310
- $ this ->customerSession ->expects ($ this ->once ())
260
+ $ this ->customerSession ->expects ($ this ->any ())
311
261
->method ('isLoggedIn ' )
312
262
->willReturn ($ customerIsLoggedIn );
313
263
$ this ->guestCartResolver ->expects ($ this ->any ())
@@ -321,35 +271,26 @@ public function testExecuteReorder(
321
271
->method ('isAllowed ' )
322
272
->with ($ this ->store )
323
273
->willReturn ($ isReorderAllowed );
324
- $ this ->storeManager ->expects ($ this ->once ())
274
+ $ this ->storeManager ->expects ($ this ->any ())
325
275
->method ('getStore ' )
326
276
->willReturn ($ this ->store );
327
- $ this ->store ->expects ($ this ->once ())
277
+ $ this ->store ->expects ($ this ->any ())
328
278
->method ('getId ' )
329
279
->willReturn ($ storeId );
330
- $ this ->cartRepository ->expects ($ this ->once ())
280
+ $ this ->cartRepository ->expects ($ this ->any ())
331
281
->method ('save ' )
332
282
->with ($ this ->cart )
333
283
->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)
344
285
->disableOriginalConstructor ()
345
286
->addMethods (['setHasError ' ])
346
287
->getMockForAbstractClass ();
347
- $ this ->cartRepository ->expects ($ this ->once ())
288
+ $ this ->cartRepository ->expects ($ this ->any ())
348
289
->method ('get ' )
349
290
->willReturn ($ savedCart );
350
291
351
292
$ output = $ this ->reorder ->execute ($ orderNumber , $ storeId );
352
- $ this ->assertNotEmpty ($ output );
293
+ $ this ->assertNotEmpty ($ output-> getCart () );
353
294
}
354
295
355
296
/**
@@ -358,7 +299,81 @@ public function testExecuteReorder(
358
299
public function dataProvider ()
359
300
{
360
301
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 ],
362
306
];
363
307
}
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
+ }
364
379
}
0 commit comments