@@ -195,6 +195,7 @@ protected function setUp()
195
195
'load ' ,
196
196
'getOptions ' ,
197
197
'getSku ' ,
198
+ 'getId ' ,
198
199
'hasGalleryAttribute ' ,
199
200
'getMediaConfig ' ,
200
201
'getMediaAttributes ' ,
@@ -282,7 +283,7 @@ protected function setUp()
282
283
*/
283
284
public function testGetAbsentProduct ()
284
285
{
285
- $ this ->productFactory ->expects ($ this ->once ())->method ('create ' )
286
+ $ this ->productFactory ->expects ($ this ->never ())->method ('create ' )
286
287
->will ($ this ->returnValue ($ this ->product ));
287
288
$ this ->resourceModelMock ->expects ($ this ->once ())->method ('getIdBySku ' )->with ('test_sku ' )
288
289
->will ($ this ->returnValue (null ));
@@ -293,38 +294,41 @@ public function testGetAbsentProduct()
293
294
public function testCreateCreatesProduct ()
294
295
{
295
296
$ sku = 'test_sku ' ;
296
- $ this ->productFactory ->expects ($ this ->once ())->method ('create ' )
297
- ->will ($ this ->returnValue ($ this ->product ));
298
297
$ this ->resourceModelMock ->expects ($ this ->once ())->method ('getIdBySku ' )->with ($ sku )
299
298
->will ($ this ->returnValue ('test_id ' ));
299
+ $ this ->productFactory ->expects ($ this ->once ())->method ('create ' )
300
+ ->will ($ this ->returnValue ($ this ->product ));
300
301
$ this ->product ->expects ($ this ->once ())->method ('load ' )->with ('test_id ' );
301
- $ this ->product ->expects ($ this ->once ())->method ('getSku ' )->willReturn ($ sku );
302
+ $ this ->product ->expects ($ this ->any ())->method ('getId ' )->willReturn ('test_id ' );
303
+ $ this ->product ->expects ($ this ->any ())->method ('getSku ' )->willReturn ($ sku );
302
304
$ this ->assertEquals ($ this ->product , $ this ->model ->get ($ sku ));
303
305
}
304
306
305
307
public function testGetProductInEditMode ()
306
308
{
307
309
$ sku = 'test_sku ' ;
308
- $ this ->productFactory ->expects ($ this ->once ())->method ('create ' )
309
- ->will ($ this ->returnValue ($ this ->product ));
310
310
$ this ->resourceModelMock ->expects ($ this ->once ())->method ('getIdBySku ' )->with ($ sku )
311
311
->will ($ this ->returnValue ('test_id ' ));
312
+ $ this ->productFactory ->expects ($ this ->once ())->method ('create ' )
313
+ ->will ($ this ->returnValue ($ this ->product ));
312
314
$ this ->product ->expects ($ this ->once ())->method ('setData ' )->with ('_edit_mode ' , true );
313
315
$ this ->product ->expects ($ this ->once ())->method ('load ' )->with ('test_id ' );
314
- $ this ->product ->expects ($ this ->once ())->method ('getSku ' )->willReturn ($ sku );
316
+ $ this ->product ->expects ($ this ->any ())->method ('getId ' )->willReturn ('test_id ' );
317
+ $ this ->product ->expects ($ this ->any ())->method ('getSku ' )->willReturn ($ sku );
315
318
$ this ->assertEquals ($ this ->product , $ this ->model ->get ($ sku , true ));
316
319
}
317
320
318
321
public function testGetBySkuWithSpace ()
319
322
{
320
323
$ trimmedSku = 'test_sku ' ;
321
324
$ sku = 'test_sku ' ;
322
- $ this ->productFactory ->expects ($ this ->once ())->method ('create ' )
323
- ->will ($ this ->returnValue ($ this ->product ));
324
325
$ this ->resourceModelMock ->expects ($ this ->once ())->method ('getIdBySku ' )->with ($ sku )
325
326
->will ($ this ->returnValue ('test_id ' ));
327
+ $ this ->productFactory ->expects ($ this ->once ())->method ('create ' )
328
+ ->will ($ this ->returnValue ($ this ->product ));
326
329
$ this ->product ->expects ($ this ->once ())->method ('load ' )->with ('test_id ' );
327
- $ this ->product ->expects ($ this ->once ())->method ('getSku ' )->willReturn ($ trimmedSku );
330
+ $ this ->product ->expects ($ this ->any ())->method ('getId ' )->willReturn ('test_id ' );
331
+ $ this ->product ->expects ($ this ->any ())->method ('getSku ' )->willReturn ($ trimmedSku );
328
332
$ this ->assertEquals ($ this ->product , $ this ->model ->get ($ sku ));
329
333
}
330
334
@@ -333,12 +337,12 @@ public function testGetWithSetStoreId()
333
337
$ productId = 123 ;
334
338
$ sku = 'test-sku ' ;
335
339
$ storeId = 7 ;
336
- $ this ->productFactory ->expects ($ this ->once ())->method ('create ' )->willReturn ($ this ->product );
337
340
$ this ->resourceModelMock ->expects ($ this ->once ())->method ('getIdBySku ' )->with ($ sku )->willReturn ($ productId );
341
+ $ this ->productFactory ->expects ($ this ->once ())->method ('create ' )->willReturn ($ this ->product );
338
342
$ this ->product ->expects ($ this ->once ())->method ('setData ' )->with ('store_id ' , $ storeId );
339
343
$ this ->product ->expects ($ this ->once ())->method ('load ' )->with ($ productId );
340
- $ this ->product ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ productId );
341
- $ this ->product ->expects ($ this ->once ())->method ('getSku ' )->willReturn ($ sku );
344
+ $ this ->product ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ productId );
345
+ $ this ->product ->expects ($ this ->any ())->method ('getSku ' )->willReturn ($ sku );
342
346
$ this ->assertSame ($ this ->product , $ this ->model ->get ($ sku , false , $ storeId ));
343
347
}
344
348
@@ -498,13 +502,13 @@ public function testGetForcedReload()
498
502
$ editMode = false ;
499
503
$ storeId = 0 ;
500
504
505
+ $ this ->resourceModelMock ->expects ($ this ->exactly (2 ))->method ('getIdBySku ' )
506
+ ->with ($ sku )->willReturn ($ id );
501
507
$ this ->productFactory ->expects ($ this ->exactly (2 ))->method ('create ' )
502
508
->will ($ this ->returnValue ($ this ->product ));
503
509
$ this ->product ->expects ($ this ->exactly (2 ))->method ('load ' );
504
- $ this ->product ->expects ($ this ->exactly (2 ))->method ('getId ' )->willReturn ($ sku );
505
- $ this ->resourceModelMock ->expects ($ this ->exactly (2 ))->method ('getIdBySku ' )
506
- ->with ($ sku )->willReturn ($ id );
507
- $ this ->product ->expects ($ this ->exactly (2 ))->method ('getSku ' )->willReturn ($ sku );
510
+ $ this ->product ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ id );
511
+ $ this ->product ->expects ($ this ->any ())->method ('getSku ' )->willReturn ($ sku );
508
512
509
513
$ this ->assertEquals ($ this ->product , $ this ->model ->get ($ sku , $ editMode , $ storeId ));
510
514
//second invocation should just return from cache
@@ -550,8 +554,9 @@ public function testGetBySkuFromCacheInitializedInGetById()
550
554
551
555
public function testSaveExisting ()
552
556
{
557
+ $ id = 100 ;
553
558
$ this ->storeManagerMock ->expects ($ this ->any ())->method ('getWebsites ' )->willReturn ([1 => 'default ' ]);
554
- $ this ->resourceModelMock ->expects ($ this ->any ())->method ('getIdBySku ' )->will ($ this ->returnValue (100 ));
559
+ $ this ->resourceModelMock ->expects ($ this ->any ())->method ('getIdBySku ' )->will ($ this ->returnValue ($ id ));
555
560
$ this ->productFactory ->expects ($ this ->any ())
556
561
->method ('create ' )
557
562
->will ($ this ->returnValue ($ this ->product ));
@@ -565,15 +570,20 @@ public function testSaveExisting()
565
570
->will ($ this ->returnValue ($ this ->productData ));
566
571
$ this ->product ->expects ($ this ->once ())->method ('getWebsiteIds ' )->willReturn ([]);
567
572
$ this ->product ->expects ($ this ->atLeastOnce ())->method ('getSku ' )->willReturn ($ this ->productData ['sku ' ]);
573
+ $ this ->product ->expects ($ this ->at (0 ))->method ('getId ' )->willReturn (null );
574
+ $ this ->product ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ id );
568
575
569
576
$ this ->assertEquals ($ this ->product , $ this ->model ->save ($ this ->product ));
570
577
}
571
578
572
579
public function testSaveNew ()
573
580
{
581
+ $ id = 100 ;
574
582
$ this ->storeManagerMock ->expects ($ this ->any ())->method ('getWebsites ' )->willReturn ([1 => 'default ' ]);
575
583
$ this ->resourceModelMock ->expects ($ this ->at (0 ))->method ('getIdBySku ' )->will ($ this ->returnValue (null ));
576
- $ this ->resourceModelMock ->expects ($ this ->at (3 ))->method ('getIdBySku ' )->will ($ this ->returnValue (100 ));
584
+ $ this ->resourceModelMock ->expects ($ this ->at (3 ))->method ('getIdBySku ' )->will ($ this ->returnValue ($ id ));
585
+ $ this ->product ->expects ($ this ->at (0 ))->method ('getId ' )->willReturn (null );
586
+ $ this ->product ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ id );
577
587
$ this ->productFactory ->expects ($ this ->any ())
578
588
->method ('create ' )
579
589
->will ($ this ->returnValue ($ this ->product ));
@@ -603,7 +613,7 @@ public function testSaveUnableToSaveException()
603
613
$ this ->resourceModelMock ->expects ($ this ->exactly (1 ))
604
614
->method ('getIdBySku ' )
605
615
->willReturn (null );
606
- $ this ->productFactory ->expects ($ this ->exactly (2 ))
616
+ $ this ->productFactory ->expects ($ this ->exactly (1 ))
607
617
->method ('create ' )
608
618
->will ($ this ->returnValue ($ this ->product ));
609
619
$ this ->initializationHelperMock ->expects ($ this ->never ())->method ('initialize ' );
@@ -633,7 +643,7 @@ public function testSaveException()
633
643
$ this ->resourceModelMock ->expects ($ this ->exactly (1 ))
634
644
->method ('getIdBySku ' )
635
645
->willReturn (null );
636
- $ this ->productFactory ->expects ($ this ->exactly (2 ))
646
+ $ this ->productFactory ->expects ($ this ->exactly (1 ))
637
647
->method ('create ' )
638
648
->will ($ this ->returnValue ($ this ->product ));
639
649
$ this ->initializationHelperMock ->expects ($ this ->never ())->method ('initialize ' );
@@ -662,7 +672,7 @@ public function testSaveInvalidProductException()
662
672
{
663
673
$ this ->storeManagerMock ->expects ($ this ->any ())->method ('getWebsites ' )->willReturn ([1 => 'default ' ]);
664
674
$ this ->resourceModelMock ->expects ($ this ->exactly (1 ))->method ('getIdBySku ' )->will ($ this ->returnValue (null ));
665
- $ this ->productFactory ->expects ($ this ->exactly (2 ))
675
+ $ this ->productFactory ->expects ($ this ->exactly (1 ))
666
676
->method ('create ' )
667
677
->will ($ this ->returnValue ($ this ->product ));
668
678
$ this ->initializationHelperMock ->expects ($ this ->never ())->method ('initialize ' );
@@ -744,6 +754,7 @@ public function testDeleteById()
744
754
->will ($ this ->returnValue ('42 ' ));
745
755
$ this ->product ->expects ($ this ->once ())->method ('load ' )->with ('42 ' );
746
756
$ this ->product ->expects ($ this ->atLeastOnce ())->method ('getSku ' )->willReturn ($ sku );
757
+ $ this ->product ->expects ($ this ->atLeastOnce ())->method ('getId ' )->willReturn (42 );
747
758
$ this ->assertTrue ($ this ->model ->deleteById ($ sku ));
748
759
}
749
760
@@ -842,8 +853,9 @@ public function cacheKeyDataProvider()
842
853
*/
843
854
public function testSaveExistingWithOptions (array $ newOptions , array $ existingOptions , array $ expectedData )
844
855
{
856
+ $ id = 100 ;
845
857
$ this ->storeManagerMock ->expects ($ this ->any ())->method ('getWebsites ' )->willReturn ([1 => 'default ' ]);
846
- $ this ->resourceModelMock ->expects ($ this ->any ())->method ('getIdBySku ' )->will ($ this ->returnValue (100 ));
858
+ $ this ->resourceModelMock ->expects ($ this ->any ())->method ('getIdBySku ' )->will ($ this ->returnValue ($ id ));
847
859
$ this ->productFactory ->expects ($ this ->any ())
848
860
->method ('create ' )
849
861
->will ($ this ->returnValue ($ this ->initializedProductMock ));
@@ -863,6 +875,8 @@ public function testSaveExistingWithOptions(array $newOptions, array $existingOp
863
875
$ this ->initializedProductMock ->expects ($ this ->atLeastOnce ())
864
876
->method ('getSku ' )->willReturn ($ this ->productData ['sku ' ]);
865
877
$ this ->product ->expects ($ this ->atLeastOnce ())->method ('getSku ' )->willReturn ($ this ->productData ['sku ' ]);
878
+ $ this ->initializedProductMock ->expects ($ this ->at (0 ))->method ('getId ' )->willReturn (null );
879
+ $ this ->initializedProductMock ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ id );
866
880
867
881
$ this ->assertEquals ($ this ->initializedProductMock , $ this ->model ->save ($ this ->product ));
868
882
}
@@ -1019,6 +1033,7 @@ public function testSaveWithLinks(array $newLinks, array $existingLinks, array $
1019
1033
$ this ->productFactory ->expects ($ this ->any ())
1020
1034
->method ('create ' )
1021
1035
->will ($ this ->returnValue ($ this ->initializedProductMock ));
1036
+ $ this ->initializedProductMock ->method ('getId ' )->willReturn (100 );
1022
1037
$ this ->initializationHelperMock ->expects ($ this ->never ())->method ('initialize ' );
1023
1038
$ this ->resourceModelMock ->expects ($ this ->once ())->method ('validate ' )->with ($ this ->initializedProductMock )
1024
1039
->willReturn (true );
@@ -1260,6 +1275,8 @@ public function testSaveExistingWithNewMediaGalleryEntries()
1260
1275
$ this ->initializedProductMock ->expects ($ this ->atLeastOnce ())
1261
1276
->method ('getSku ' )->willReturn ($ this ->productData ['sku ' ]);
1262
1277
$ this ->product ->expects ($ this ->atLeastOnce ())->method ('getSku ' )->willReturn ($ this ->productData ['sku ' ]);
1278
+ $ this ->initializedProductMock ->expects ($ this ->at (0 ))->method ('getId ' )->willReturn (null );
1279
+ $ this ->initializedProductMock ->expects ($ this ->any ())->method ('getId ' )->willReturn (100 );
1263
1280
1264
1281
$ this ->model ->save ($ this ->product );
1265
1282
}
@@ -1301,6 +1318,8 @@ public function testSaveWithDifferentWebsites()
1301
1318
$ this ->product ->method ('setWebsiteIds ' )->willReturn ([2 ,3 ]);
1302
1319
$ this ->product ->method ('getSku ' )
1303
1320
->willReturn ('simple ' );
1321
+ $ this ->product ->expects ($ this ->at (0 ))->method ('getId ' )->willReturn (null );
1322
+ $ this ->product ->expects ($ this ->any ())->method ('getId ' )->willReturn (100 );
1304
1323
1305
1324
$ this ->assertEquals ($ this ->product , $ this ->model ->save ($ this ->product ));
1306
1325
}
@@ -1353,6 +1372,7 @@ public function testSaveExistingWithMediaGalleryEntries()
1353
1372
->method ('getSku ' )->willReturn ($ this ->productData ['sku ' ]);
1354
1373
$ this ->product ->expects ($ this ->atLeastOnce ())->method ('getSku ' )->willReturn ($ this ->productData ['sku ' ]);
1355
1374
$ this ->product ->expects ($ this ->any ())->method ('getMediaGalleryEntries ' )->willReturn (null );
1375
+ $ this ->initializedProductMock ->expects ($ this ->any ())->method ('getId ' )->willReturn (100 );
1356
1376
$ this ->model ->save ($ this ->product );
1357
1377
}
1358
1378
}
0 commit comments