5
5
* See COPYING.txt for license details.
6
6
*/
7
7
8
- declare (strict_types=1 );
9
-
10
8
namespace Magento \Catalog \Test \Unit \Model ;
11
9
12
10
use Magento \Catalog \Api \Data \ProductAttributeInterface ;
13
11
use Magento \Framework \Api \Data \ImageContentInterface ;
14
- use Magento \Framework \Api \Data \ImageContentInterfaceFactory ;
15
- use Magento \Framework \Api \ImageContentValidatorInterface ;
16
12
use Magento \Framework \Api \SearchCriteria \CollectionProcessorInterface ;
17
- use Magento \Framework \Api \SearchCriteriaBuilder ;
18
13
use Magento \Framework \DB \Adapter \ConnectionException ;
19
14
use Magento \Framework \Serialize \Serializer \Json ;
20
15
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
21
16
use Magento \Store \Api \Data \StoreInterface ;
22
- use PHPUnit_Framework_MockObject_MockObject as MockObject ;
23
17
24
18
/**
25
19
* Class ProductRepositoryTest
@@ -197,7 +191,6 @@ protected function setUp()
197
191
'load ' ,
198
192
'getOptions ' ,
199
193
'getSku ' ,
200
- 'getId ' ,
201
194
'hasGalleryAttribute ' ,
202
195
'getMediaConfig ' ,
203
196
'getMediaAttributes ' ,
@@ -312,7 +305,7 @@ function ($value) {
312
305
*/
313
306
public function testGetAbsentProduct ()
314
307
{
315
- $ this ->productFactoryMock ->expects ($ this ->never ())->method ('create ' )
308
+ $ this ->productFactoryMock ->expects ($ this ->once ())->method ('create ' )
316
309
->will ($ this ->returnValue ($ this ->productMock ));
317
310
$ this ->resourceModelMock ->expects ($ this ->once ())->method ('getIdBySku ' )->with ('test_sku ' )
318
311
->will ($ this ->returnValue (null ));
@@ -328,8 +321,7 @@ public function testCreateCreatesProduct()
328
321
$ this ->resourceModelMock ->expects ($ this ->once ())->method ('getIdBySku ' )->with ($ sku )
329
322
->will ($ this ->returnValue ('test_id ' ));
330
323
$ this ->productMock ->expects ($ this ->once ())->method ('load ' )->with ('test_id ' );
331
- $ this ->productMock ->expects ($ this ->any ())->method ('getId ' )->willReturn ('test_id ' );
332
- $ this ->productMock ->expects ($ this ->any ())->method ('getSku ' )->willReturn ($ sku );
324
+ $ this ->productMock ->expects ($ this ->once ())->method ('getSku ' )->willReturn ($ sku );
333
325
$ this ->assertEquals ($ this ->productMock , $ this ->model ->get ($ sku ));
334
326
}
335
327
@@ -342,7 +334,6 @@ public function testGetProductInEditMode()
342
334
->will ($ this ->returnValue ('test_id ' ));
343
335
$ this ->productMock ->expects ($ this ->once ())->method ('setData ' )->with ('_edit_mode ' , true );
344
336
$ this ->productMock ->expects ($ this ->once ())->method ('load ' )->with ('test_id ' );
345
- $ this ->productMock ->expects ($ this ->any ())->method ('getId ' )->willReturn ('test_id ' );
346
337
$ this ->productMock ->expects ($ this ->once ())->method ('getSku ' )->willReturn ($ sku );
347
338
$ this ->assertEquals ($ this ->productMock , $ this ->model ->get ($ sku , true ));
348
339
}
@@ -356,8 +347,7 @@ public function testGetBySkuWithSpace()
356
347
$ this ->resourceModelMock ->expects ($ this ->once ())->method ('getIdBySku ' )->with ($ sku )
357
348
->will ($ this ->returnValue ('test_id ' ));
358
349
$ this ->productMock ->expects ($ this ->once ())->method ('load ' )->with ('test_id ' );
359
- $ this ->productMock ->expects ($ this ->any ())->method ('getId ' )->willReturn ('test_id ' );
360
- $ this ->productMock ->expects ($ this ->any ())->method ('getSku ' )->willReturn ($ trimmedSku );
350
+ $ this ->productMock ->expects ($ this ->once ())->method ('getSku ' )->willReturn ($ trimmedSku );
361
351
$ this ->assertEquals ($ this ->productMock , $ this ->model ->get ($ sku ));
362
352
}
363
353
@@ -370,8 +360,8 @@ public function testGetWithSetStoreId()
370
360
$ this ->resourceModelMock ->expects ($ this ->once ())->method ('getIdBySku ' )->with ($ sku )->willReturn ($ productId );
371
361
$ this ->productMock ->expects ($ this ->once ())->method ('setData ' )->with ('store_id ' , $ storeId );
372
362
$ this ->productMock ->expects ($ this ->once ())->method ('load ' )->with ($ productId );
373
- $ this ->productMock ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ productId );
374
- $ this ->productMock ->expects ($ this ->any ())->method ('getSku ' )->willReturn ($ sku );
363
+ $ this ->productMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ productId );
364
+ $ this ->productMock ->expects ($ this ->once ())->method ('getSku ' )->willReturn ($ sku );
375
365
$ this ->assertSame ($ this ->productMock , $ this ->model ->get ($ sku , false , $ storeId ));
376
366
}
377
367
@@ -519,13 +509,13 @@ public function testGetForcedReload()
519
509
$ editMode = false ;
520
510
$ storeId = 0 ;
521
511
522
- $ this ->resourceModelMock ->expects ($ this ->exactly (2 ))->method ('getIdBySku ' )
523
- ->with ($ sku )->willReturn ($ id );
524
512
$ this ->productFactoryMock ->expects ($ this ->exactly (2 ))->method ('create ' )
525
513
->will ($ this ->returnValue ($ this ->productMock ));
526
514
$ this ->productMock ->expects ($ this ->exactly (2 ))->method ('load ' );
527
- $ this ->productMock ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ id );
528
- $ this ->productMock ->expects ($ this ->any ())->method ('getSku ' )->willReturn ($ sku );
515
+ $ this ->productMock ->expects ($ this ->exactly (2 ))->method ('getId ' )->willReturn ($ sku );
516
+ $ this ->resourceModelMock ->expects ($ this ->exactly (2 ))->method ('getIdBySku ' )
517
+ ->with ($ sku )->willReturn ($ id );
518
+ $ this ->productMock ->expects ($ this ->exactly (2 ))->method ('getSku ' )->willReturn ($ sku );
529
519
$ this ->serializerMock ->expects ($ this ->exactly (3 ))->method ('serialize ' );
530
520
531
521
$ this ->assertEquals ($ this ->productMock , $ this ->model ->get ($ sku , $ editMode , $ storeId ));
@@ -563,8 +553,7 @@ public function testGetBySkuFromCacheInitializedInGetById()
563
553
564
554
public function testSaveExisting ()
565
555
{
566
- $ id = 100 ;
567
- $ this ->resourceModelMock ->expects ($ this ->any ())->method ('getIdBySku ' )->will ($ this ->returnValue ($ id ));
556
+ $ this ->resourceModelMock ->expects ($ this ->any ())->method ('getIdBySku ' )->will ($ this ->returnValue (100 ));
568
557
$ this ->productFactoryMock ->expects ($ this ->any ())
569
558
->method ('create ' )
570
559
->will ($ this ->returnValue ($ this ->productMock ));
@@ -577,20 +566,15 @@ public function testSaveExisting()
577
566
->method ('toNestedArray ' )
578
567
->will ($ this ->returnValue ($ this ->productData ));
579
568
$ this ->productMock ->expects ($ this ->atLeastOnce ())->method ('getSku ' )->willReturn ($ this ->productData ['sku ' ]);
580
- $ this ->productMock ->expects ($ this ->at (0 ))->method ('getId ' )->willReturn (null );
581
- $ this ->productMock ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ id );
582
569
583
570
$ this ->assertEquals ($ this ->productMock , $ this ->model ->save ($ this ->productMock ));
584
571
}
585
572
586
573
public function testSaveNew ()
587
574
{
588
- $ id = 100 ;
589
575
$ this ->storeManagerMock ->expects ($ this ->any ())->method ('getWebsites ' )->willReturn ([1 => 'default ' ]);
590
576
$ this ->resourceModelMock ->expects ($ this ->at (0 ))->method ('getIdBySku ' )->will ($ this ->returnValue (null ));
591
- $ this ->resourceModelMock ->expects ($ this ->at (3 ))->method ('getIdBySku ' )->will ($ this ->returnValue ($ id ));
592
- $ this ->productMock ->expects ($ this ->at (0 ))->method ('getId ' )->willReturn (null );
593
- $ this ->productMock ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ id );
577
+ $ this ->resourceModelMock ->expects ($ this ->at (3 ))->method ('getIdBySku ' )->will ($ this ->returnValue (100 ));
594
578
$ this ->productFactoryMock ->expects ($ this ->any ())
595
579
->method ('create ' )
596
580
->will ($ this ->returnValue ($ this ->productMock ));
@@ -616,7 +600,7 @@ public function testSaveUnableToSaveException()
616
600
$ this ->storeManagerMock ->expects ($ this ->any ())->method ('getWebsites ' )->willReturn ([1 => 'default ' ]);
617
601
$ this ->resourceModelMock ->expects ($ this ->exactly (1 ))
618
602
->method ('getIdBySku ' )->willReturn (null );
619
- $ this ->productFactoryMock ->expects ($ this ->exactly (1 ))
603
+ $ this ->productFactoryMock ->expects ($ this ->exactly (2 ))
620
604
->method ('create ' )
621
605
->will ($ this ->returnValue ($ this ->productMock ));
622
606
$ this ->initializationHelperMock ->expects ($ this ->never ())->method ('initialize ' );
@@ -641,7 +625,7 @@ public function testSaveException()
641
625
{
642
626
$ this ->storeManagerMock ->expects ($ this ->any ())->method ('getWebsites ' )->willReturn ([1 => 'default ' ]);
643
627
$ this ->resourceModelMock ->expects ($ this ->exactly (1 ))->method ('getIdBySku ' )->will ($ this ->returnValue (null ));
644
- $ this ->productFactoryMock ->expects ($ this ->exactly (1 ))
628
+ $ this ->productFactoryMock ->expects ($ this ->exactly (2 ))
645
629
->method ('create ' )
646
630
->will ($ this ->returnValue ($ this ->productMock ));
647
631
$ this ->initializationHelperMock ->expects ($ this ->never ())->method ('initialize ' );
@@ -667,7 +651,7 @@ public function testSaveInvalidProductException()
667
651
{
668
652
$ this ->storeManagerMock ->expects ($ this ->any ())->method ('getWebsites ' )->willReturn ([1 => 'default ' ]);
669
653
$ this ->resourceModelMock ->expects ($ this ->exactly (1 ))->method ('getIdBySku ' )->will ($ this ->returnValue (null ));
670
- $ this ->productFactoryMock ->expects ($ this ->exactly (1 ))
654
+ $ this ->productFactoryMock ->expects ($ this ->exactly (2 ))
671
655
->method ('create ' )
672
656
->will ($ this ->returnValue ($ this ->productMock ));
673
657
$ this ->initializationHelperMock ->expects ($ this ->never ())->method ('initialize ' );
@@ -743,7 +727,6 @@ public function testDeleteById()
743
727
->will ($ this ->returnValue ('42 ' ));
744
728
$ this ->productMock ->expects ($ this ->once ())->method ('load ' )->with ('42 ' );
745
729
$ this ->productMock ->expects ($ this ->atLeastOnce ())->method ('getSku ' )->willReturn ($ sku );
746
- $ this ->productMock ->expects ($ this ->atLeastOnce ())->method ('getId ' )->willReturn (42 );
747
730
$ this ->assertTrue ($ this ->model ->deleteById ($ sku ));
748
731
}
749
732
@@ -838,9 +821,8 @@ public function cacheKeyDataProvider()
838
821
*/
839
822
public function testSaveExistingWithOptions (array $ newOptions , array $ existingOptions , array $ expectedData )
840
823
{
841
- $ id = 100 ;
842
824
$ this ->storeManagerMock ->expects ($ this ->any ())->method ('getWebsites ' )->willReturn ([1 => 'default ' ]);
843
- $ this ->resourceModelMock ->expects ($ this ->any ())->method ('getIdBySku ' )->will ($ this ->returnValue ($ id ));
825
+ $ this ->resourceModelMock ->expects ($ this ->any ())->method ('getIdBySku ' )->will ($ this ->returnValue (100 ));
844
826
$ this ->productFactoryMock ->expects ($ this ->any ())
845
827
->method ('create ' )
846
828
->will ($ this ->returnValue ($ this ->initializedProductMock ));
@@ -859,8 +841,6 @@ public function testSaveExistingWithOptions(array $newOptions, array $existingOp
859
841
$ this ->initializedProductMock ->expects ($ this ->atLeastOnce ())
860
842
->method ('getSku ' )->willReturn ($ this ->productData ['sku ' ]);
861
843
$ this ->productMock ->expects ($ this ->atLeastOnce ())->method ('getSku ' )->willReturn ($ this ->productData ['sku ' ]);
862
- $ this ->initializedProductMock ->expects ($ this ->at (0 ))->method ('getId ' )->willReturn (null );
863
- $ this ->initializedProductMock ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ id );
864
844
865
845
$ this ->assertEquals ($ this ->initializedProductMock , $ this ->model ->save ($ this ->productMock ));
866
846
}
@@ -1017,7 +997,6 @@ public function testSaveWithLinks(array $newLinks, array $existingLinks, array $
1017
997
$ this ->productFactoryMock ->expects ($ this ->any ())
1018
998
->method ('create ' )
1019
999
->will ($ this ->returnValue ($ this ->initializedProductMock ));
1020
- $ this ->initializedProductMock ->method ('getId ' )->willReturn (100 );
1021
1000
$ this ->initializationHelperMock ->expects ($ this ->never ())->method ('initialize ' );
1022
1001
$ this ->resourceModelMock ->expects ($ this ->once ())->method ('validate ' )->with ($ this ->initializedProductMock )
1023
1002
->willReturn (true );
@@ -1280,8 +1259,6 @@ public function testSaveExistingWithNewMediaGalleryEntries()
1280
1259
$ this ->initializedProductMock ->expects ($ this ->atLeastOnce ())
1281
1260
->method ('getSku ' )->willReturn ($ this ->productData ['sku ' ]);
1282
1261
$ this ->productMock ->expects ($ this ->atLeastOnce ())->method ('getSku ' )->willReturn ($ this ->productData ['sku ' ]);
1283
- $ this ->initializedProductMock ->expects ($ this ->at (0 ))->method ('getId ' )->willReturn (null );
1284
- $ this ->initializedProductMock ->expects ($ this ->any ())->method ('getId ' )->willReturn (100 );
1285
1262
1286
1263
$ this ->model ->save ($ this ->productMock );
1287
1264
}
@@ -1324,8 +1301,6 @@ public function testSaveWithDifferentWebsites()
1324
1301
]);
1325
1302
$ this ->productMock ->expects ($ this ->once ())->method ('setWebsiteIds ' )->willReturn ([2 ,3 ]);
1326
1303
$ this ->productMock ->method ('getSku ' )->willReturn ('simple ' );
1327
- $ this ->productMock ->expects ($ this ->at (0 ))->method ('getId ' )->willReturn (null );
1328
- $ this ->productMock ->expects ($ this ->any ())->method ('getId ' )->willReturn (100 );
1329
1304
1330
1305
$ this ->assertEquals ($ this ->productMock , $ this ->model ->save ($ this ->productMock ));
1331
1306
}
@@ -1399,7 +1374,6 @@ public function testSaveExistingWithMediaGalleryEntries()
1399
1374
->method ('getSku ' )->willReturn ($ this ->productData ['sku ' ]);
1400
1375
$ this ->productMock ->expects ($ this ->atLeastOnce ())->method ('getSku ' )->willReturn ($ this ->productData ['sku ' ]);
1401
1376
$ this ->productMock ->expects ($ this ->any ())->method ('getMediaGalleryEntries ' )->willReturn (null );
1402
- $ this ->initializedProductMock ->expects ($ this ->any ())->method ('getId ' )->willReturn (100 );
1403
1377
$ this ->model ->save ($ this ->productMock );
1404
1378
$ this ->assertEquals ($ expectedResult , $ this ->initializedProductMock ->getMediaGallery ('images ' ));
1405
1379
}
0 commit comments