@@ -234,7 +234,7 @@ public function dataForAssembleEavAttribute()
234
234
public function testLoadFirstVariationWithSwatchImage ($ imageTypes , $ expected , $ requiredAttributes )
235
235
{
236
236
$ this ->getSwatchAttributes ($ this ->productMock );
237
- $ this ->getUsedProducts ($ imageTypes + $ requiredAttributes );
237
+ $ this ->getUsedProducts ($ imageTypes + $ requiredAttributes, $ imageTypes );
238
238
239
239
$ result = $ this ->swatchHelperObject ->loadFirstVariationWithSwatchImage ($ this ->productMock , $ requiredAttributes );
240
240
@@ -293,7 +293,7 @@ public function testLoadVariationByFallback($product)
293
293
public function testLoadFirstVariationWithImage ($ imageTypes , $ expected , $ requiredAttributes )
294
294
{
295
295
$ this ->getSwatchAttributes ($ this ->productMock );
296
- $ this ->getUsedProducts ($ imageTypes + $ requiredAttributes );
296
+ $ this ->getUsedProducts ($ imageTypes + $ requiredAttributes, $ imageTypes );
297
297
298
298
$ result = $ this ->swatchHelperObject ->loadFirstVariationWithImage ($ this ->productMock , $ requiredAttributes );
299
299
@@ -346,53 +346,68 @@ public function testLoadVariationByFallbackWithoutProduct()
346
346
*/
347
347
public function testGetProductMediaGallery ($ mediaGallery , $ image )
348
348
{
349
- $ this ->productMock ->expects ($ this ->once ())->method ('getMediaAttributeValues ' )->willReturn ($ mediaGallery );
350
- $ this ->productMock ->expects ($ this ->any ())->method ('getId ' )->willReturn (95 );
351
-
352
- $ this ->imageHelperMock ->expects ($ this ->any ())
353
- ->method ('init ' )
354
- ->willReturnMap ([
355
- [$ this ->productMock , 'product_page_image_large ' , [], $ this ->imageHelperMock ],
356
- [$ this ->productMock , 'product_page_image_medium ' , [], $ this ->imageHelperMock ],
357
- [$ this ->productMock , 'product_page_image_small ' , [], $ this ->imageHelperMock ],
358
- ]);
359
-
360
- $ this ->imageHelperMock ->expects ($ this ->any ())
361
- ->method ('setImageFile ' )
362
- ->with ($ image )
363
- ->willReturnSelf ();
364
- $ this ->imageHelperMock ->expects ($ this ->any ())
365
- ->method ('constrainOnly ' )
366
- ->willReturnSelf ();
367
- $ this ->imageHelperMock ->expects ($ this ->any ())
368
- ->method ('keepAspectRatio ' )
369
- ->willReturnSelf ();
370
- $ this ->imageHelperMock ->expects ($ this ->any ())
371
- ->method ('keepFrame ' )
372
- ->willReturnSelf ();
373
- $ this ->imageHelperMock ->expects ($ this ->any ())
374
- ->method ('getUrl ' )
375
- ->willReturn ('http://full_path_to_image/magento1.png ' );
376
-
377
- $ this ->productRepoMock ->expects ($ this ->any ())
378
- ->method ('getById ' )
379
- ->with (95 )
380
- ->willReturn ($ this ->productMock );
381
-
382
- $ mediaObject = $ this ->getMock (\Magento \Framework \DataObject::class, [], [], '' , false );
383
- $ iterator = new \ArrayIterator ([$ mediaObject ]);
384
- $ mediaCollectionMock = $ this ->getMock (\Magento \Framework \Data \Collection::class, [], [], '' , false );
385
- $ mediaCollectionMock ->expects ($ this ->any ())->method ('getIterator ' )->willReturn ($ iterator );
386
- $ mediaObject ->method ('getData ' )->withConsecutive (
387
- ['value_id ' ],
388
- ['file ' ]
389
- )->willReturnOnConsecutiveCalls (
390
- 0 ,
391
- $ image
392
- );
393
- $ this ->productMock ->method ('getMediaGalleryImages ' )->willReturn ($ mediaCollectionMock );
349
+ $ mediaGalleryEntries = [];
350
+ $ id = 0 ;
351
+ $ mediaUrls = [];
352
+ foreach ($ mediaGallery as $ mediaType => $ mediaFile ) {
353
+ $ mediaGalleryEntryMock = $ this ->getMockBuilder (
354
+ \Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterface::class
355
+ )->getMock ();
356
+ $ mediaGalleryEntryMock ->expects ($ this ->atLeastOnce ())
357
+ ->method ('isDisabled ' )
358
+ ->willReturn (false );
359
+ $ mediaGalleryEntryMock ->expects ($ this ->atLeastOnce ())
360
+ ->method ('getTypes ' )
361
+ ->willReturn ([$ mediaType ]);
362
+ $ mediaGalleryEntryMock ->expects ($ this ->atLeastOnce ())
363
+ ->method ('getFile ' )
364
+ ->willReturn ($ mediaFile );
365
+ $ mediaGalleryEntryMock ->expects ($ this ->atLeastOnce ())
366
+ ->method ('getId ' )
367
+ ->willReturn (++$ id );
368
+
369
+ $ mediaGalleryEntries [] = $ mediaGalleryEntryMock ;
370
+ $ mediaUrls [] = ['http://full_path_to_image ' . $ mediaFile ]; //large
371
+ $ mediaUrls [] = ['http://full_path_to_image ' . $ mediaFile ]; //medium
372
+ $ mediaUrls [] = ['http://full_path_to_image ' . $ mediaFile ]; //small
373
+ }
374
+ $ this ->productMock ->expects ($ this ->once ())
375
+ ->method ('getMediaGalleryEntries ' )
376
+ ->willReturn ($ mediaGalleryEntries );
377
+
378
+ if ($ mediaGallery ) {
379
+ $ this ->imageHelperMock ->expects ($ this ->atLeastOnce ())
380
+ ->method ('init ' )
381
+ ->willReturnMap ([
382
+ [$ this ->productMock , 'product_page_image_large ' , [], $ this ->imageHelperMock ],
383
+ [$ this ->productMock , 'product_page_image_medium ' , [], $ this ->imageHelperMock ],
384
+ [$ this ->productMock , 'product_page_image_small ' , [], $ this ->imageHelperMock ],
385
+ ]);
386
+ $ this ->imageHelperMock ->expects ($ this ->atLeastOnce ())
387
+ ->method ('setImageFile ' )
388
+ ->willReturnSelf ();
389
+ $ this ->imageHelperMock ->expects ($ this ->atLeastOnce ())
390
+ ->method ('constrainOnly ' )
391
+ ->willReturnSelf ();
392
+ $ this ->imageHelperMock ->expects ($ this ->atLeastOnce ())
393
+ ->method ('keepAspectRatio ' )
394
+ ->willReturnSelf ();
395
+ $ this ->imageHelperMock ->expects ($ this ->atLeastOnce ())
396
+ ->method ('keepFrame ' )
397
+ ->willReturnSelf ();
398
+ $ this ->imageHelperMock ->expects ($ this ->atLeastOnce ())
399
+ ->method ('getUrl ' )
400
+ ->willReturnMap ($ mediaUrls );
401
+ }
394
402
395
- $ this ->swatchHelperObject ->getProductMediaGallery ($ this ->productMock );
403
+ $ productMediaGallery = $ this ->swatchHelperObject ->getProductMediaGallery ($ this ->productMock );
404
+ if ($ mediaGallery ) {
405
+ $ this ->assertContains ($ image , $ productMediaGallery ['large ' ]);
406
+ $ this ->assertContains ($ image , $ productMediaGallery ['medium ' ]);
407
+ $ this ->assertContains ($ image , $ productMediaGallery ['small ' ]);
408
+ } else {
409
+ $ this ->assertEmpty ($ productMediaGallery );
410
+ }
396
411
}
397
412
398
413
public function dataForMediaGallery ()
@@ -432,22 +447,48 @@ protected function getSwatchAttributes()
432
447
->willReturn ($ returnFromProvideMethod );
433
448
}
434
449
435
- protected function getUsedProducts (array $ attributes )
450
+ protected function getUsedProducts (array $ attributes, array $ imageTypes )
436
451
{
437
452
$ this ->productMock
438
453
->expects ($ this ->atLeastOnce ())
439
454
->method ('getTypeInstance ' )
440
455
->willReturn ($ this ->configurableMock );
441
456
442
- $ product1 = $ this ->getMock (\Magento \Catalog \Model \Product::class, ['hasData ' ], [], '' , false );
443
- $ product1 ->setData ($ attributes );
444
-
445
- $ product2 = $ this ->getMock (\Magento \Catalog \Model \Product::class, ['hasData ' ], [], '' , false );
446
- $ product2 ->setData ($ attributes );
447
-
448
- $ simpleProducts = [$ product2 , $ product1 ];
457
+ $ simpleProducts = [];
458
+ for ($ i = 0 ; $ i < 2 ; $ i ++) {
459
+ $ simpleProduct = $ this ->getMock (
460
+ \Magento \Catalog \Model \Product::class,
461
+ ['hasData ' , 'getMediaGalleryEntries ' ],
462
+ [],
463
+ '' ,
464
+ false
465
+ );
466
+ $ simpleProduct ->setData ($ attributes );
467
+
468
+ $ mediaGalleryEntries = [];
469
+ foreach ($ imageTypes as $ mediaType => $ mediaFile ) {
470
+ $ mediaGalleryEntryMock = $ this ->getMockBuilder (
471
+ \Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterface::class
472
+ )->getMock ();
473
+ $ mediaGalleryEntryMock ->expects ($ this ->any ())
474
+ ->method ('isDisabled ' )
475
+ ->willReturn (false );
476
+ $ mediaGalleryEntryMock ->expects ($ this ->any ())
477
+ ->method ('getTypes ' )
478
+ ->willReturn ([$ mediaType ]);
479
+
480
+ $ mediaGalleryEntries [] = $ mediaGalleryEntryMock ;
481
+ }
482
+ $ simpleProduct ->expects ($ this ->any ())
483
+ ->method ('getMediaGalleryEntries ' )
484
+ ->willReturn ($ mediaGalleryEntries );
485
+
486
+ $ simpleProducts [] = $ simpleProduct ;
487
+ }
449
488
450
- $ this ->configurableMock ->expects ($ this ->once ())->method ('getUsedProducts ' )->with ($ this ->productMock )
489
+ $ this ->configurableMock ->expects ($ this ->once ())
490
+ ->method ('getUsedProducts ' )
491
+ ->with ($ this ->productMock )
451
492
->willReturn ($ simpleProducts );
452
493
453
494
}
0 commit comments