1818use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
1919use Magento \Framework \Api \AttributeValue ;
2020use Magento \Framework \Api \Data \ImageContentInterface ;
21+ use Magento \Framework \Api \Data \ImageContentInterfaceFactory ;
2122use Magento \Framework \Api \ImageContentValidatorInterface ;
23+ use Magento \Framework \App \Filesystem \DirectoryList ;
24+ use Magento \Framework \Filesystem ;
25+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
26+ use Magento \Framework \Filesystem \Driver \File \Mime ;
27+ use Magento \Framework \Filesystem \DriverInterface ;
2228use PHPUnit \Framework \MockObject \MockObject ;
2329use PHPUnit \Framework \TestCase ;
30+ use Magento \Framework \Filesystem \Io \File ;
31+ use Magento \Catalog \Model \Product \Media \ConfigInterface as MediaConfig ;
2432
2533/**
2634 * Tests for \Magento\Catalog\Model\Product\Gallery\GalleryManagement.
@@ -74,6 +82,26 @@ class GalleryManagementTest extends TestCase
7482 */
7583 private $ newProductMock ;
7684
85+ /**
86+ * @var ImageContentInterface|MockObject
87+ */
88+ private $ imageContentInterface ;
89+
90+ /**
91+ * @var Filesystem|MockObject
92+ */
93+ private $ filesystem ;
94+
95+ /**
96+ * @var Mime|MockObject
97+ */
98+ private $ mime ;
99+
100+ /**
101+ * @var File|MockObject
102+ */
103+ private $ file ;
104+
77105 /**
78106 * @inheritDoc
79107 */
@@ -83,6 +111,12 @@ protected function setUp(): void
83111 $ this ->contentValidatorMock = $ this ->getMockForAbstractClass (ImageContentValidatorInterface::class);
84112 $ this ->productInterfaceFactory = $ this ->createMock (ProductInterfaceFactory::class);
85113 $ this ->deleteValidator = $ this ->createMock (DeleteValidator::class);
114+ $ this ->imageContentInterface = $ this ->getMockBuilder (ImageContentInterfaceFactory::class)
115+ ->disableOriginalConstructor ()
116+ ->getMock ();
117+ $ this ->filesystem = $ this ->createMock (Filesystem::class);
118+ $ this ->mime = $ this ->createMock (Mime::class);
119+ $ this ->file = $ this ->createMock (File::class);
86120 $ this ->productMock = $ this ->createPartialMock (
87121 Product::class,
88122 [
@@ -93,7 +127,8 @@ protected function setUp(): void
93127 'getCustomAttribute ' ,
94128 'getMediaGalleryEntries ' ,
95129 'setMediaGalleryEntries ' ,
96- 'getMediaAttributes '
130+ 'getMediaAttributes ' ,
131+ 'getMediaConfig '
97132 ]
98133 );
99134 $ this ->mediaGalleryEntryMock =
@@ -102,7 +137,11 @@ protected function setUp(): void
102137 $ this ->productRepositoryMock ,
103138 $ this ->contentValidatorMock ,
104139 $ this ->productInterfaceFactory ,
105- $ this ->deleteValidator
140+ $ this ->deleteValidator ,
141+ $ this ->imageContentInterface ,
142+ $ this ->filesystem ,
143+ $ this ->mime ,
144+ $ this ->file ,
106145 );
107146 $ this ->attributeValueMock = $ this ->getMockBuilder (AttributeValue::class)
108147 ->disableOriginalConstructor ()
@@ -381,6 +420,55 @@ public function testGet(): void
381420 $ existingEntryMock ->expects ($ this ->once ())->method ('getId ' )->willReturn (42 );
382421 $ this ->productMock ->expects ($ this ->once ())->method ('getMediaGalleryEntries ' )
383422 ->willReturn ([$ existingEntryMock ]);
423+ $ mediaConfigMock = $ this ->getMockBuilder (MediaConfig::class)
424+ ->disableOriginalConstructor ()
425+ ->getMock ();
426+ $ mediaConfigMock ->expects ($ this ->once ())
427+ ->method ('getMediaPath ' )
428+ ->willReturn ("base/path/test123.jpg " );
429+ $ this ->productMock ->expects ($ this ->once ())
430+ ->method ('getMediaConfig ' )
431+ ->willReturn ($ mediaConfigMock );
432+ $ mediaDirectoryMock = $ this ->getMockBuilder (WriteInterface::class)
433+ ->disableOriginalConstructor ()
434+ ->getMock ();
435+ $ this ->filesystem ->expects ($ this ->once ())
436+ ->method ('getDirectoryWrite ' )
437+ ->with (DirectoryList::MEDIA )
438+ ->willReturn ($ mediaDirectoryMock );
439+ $ mediaDirectoryMock ->expects ($ this ->once ())
440+ ->method ('getAbsolutePath ' )
441+ ->with ('base/path/test123.jpg ' )
442+ ->willReturn ('absolute/path/base/path/test123.jpg ' );
443+ $ this ->file ->expects ($ this ->any ())
444+ ->method ('getPathInfo ' )
445+ ->willReturnCallback (
446+ function ($ path ) {
447+ return pathinfo ($ path );
448+ }
449+ );
450+ $ driverMock = $ this ->getMockBuilder (DriverInterface::class)
451+ ->disableOriginalConstructor ()
452+ ->getMock ();
453+ $ mediaDirectoryMock ->expects ($ this ->any ())->method ('getDriver ' )->willReturn ($ driverMock );
454+ $ driverMock ->expects ($ this ->once ())
455+ ->method ('fileGetContents ' )
456+ ->willReturn ('0123456789abcdefghijklmnopqrstuvwxyz ' );
457+ $ ImageContentInterface = $ this ->getMockBuilder (ImageContentInterface::class)
458+ ->disableOriginalConstructor ()
459+ ->getMock ();
460+ $ ImageContentInterface ->expects ($ this ->once ())
461+ ->method ('setName ' )
462+ ->willReturnSelf ();
463+ $ ImageContentInterface ->expects ($ this ->once ())
464+ ->method ('setBase64EncodedData ' )
465+ ->willReturnSelf ();
466+ $ ImageContentInterface ->expects ($ this ->once ())
467+ ->method ('setType ' )
468+ ->willReturnSelf ();
469+ $ this ->imageContentInterface ->expects ($ this ->once ())
470+ ->method ('create ' )
471+ ->willReturn ($ ImageContentInterface );
384472 $ this ->assertEquals ($ existingEntryMock , $ this ->model ->get ($ productSku , $ imageId ));
385473 }
386474
@@ -395,6 +483,57 @@ public function testGetList(): void
395483 $ entryMock = $ this ->getMockForAbstractClass (ProductAttributeMediaGalleryEntryInterface::class);
396484 $ this ->productMock ->expects ($ this ->once ())->method ('getMediaGalleryEntries ' )
397485 ->willReturn ([$ entryMock ]);
486+ $ this ->productMock ->expects ($ this ->once ())->method ('getMediaGalleryEntries ' )
487+ ->willReturn ([$ entryMock ]);
488+ $ mediaConfigMock = $ this ->getMockBuilder (MediaConfig::class)
489+ ->disableOriginalConstructor ()
490+ ->getMock ();
491+ $ mediaConfigMock ->expects ($ this ->once ())
492+ ->method ('getMediaPath ' )
493+ ->willReturn ("base/path/test123.jpg " );
494+ $ this ->productMock ->expects ($ this ->once ())
495+ ->method ('getMediaConfig ' )
496+ ->willReturn ($ mediaConfigMock );
497+ $ mediaDirectoryMock = $ this ->getMockBuilder (WriteInterface::class)
498+ ->disableOriginalConstructor ()
499+ ->getMock ();
500+ $ this ->filesystem ->expects ($ this ->once ())
501+ ->method ('getDirectoryWrite ' )
502+ ->with (DirectoryList::MEDIA )
503+ ->willReturn ($ mediaDirectoryMock );
504+ $ mediaDirectoryMock ->expects ($ this ->once ())
505+ ->method ('getAbsolutePath ' )
506+ ->with ('base/path/test123.jpg ' )
507+ ->willReturn ('absolute/path/base/path/test123.jpg ' );
508+ $ this ->file ->expects ($ this ->any ())
509+ ->method ('getPathInfo ' )
510+ ->willReturnCallback (
511+ function ($ path ) {
512+ return pathinfo ($ path );
513+ }
514+ );
515+ $ driverMock = $ this ->getMockBuilder (DriverInterface::class)
516+ ->disableOriginalConstructor ()
517+ ->getMock ();
518+ $ mediaDirectoryMock ->expects ($ this ->any ())->method ('getDriver ' )->willReturn ($ driverMock );
519+ $ driverMock ->expects ($ this ->once ())
520+ ->method ('fileGetContents ' )
521+ ->willReturn ('0123456789abcdefghijklmnopqrstuvwxyz ' );
522+ $ ImageContentInterface = $ this ->getMockBuilder (ImageContentInterface::class)
523+ ->disableOriginalConstructor ()
524+ ->getMock ();
525+ $ ImageContentInterface ->expects ($ this ->once ())
526+ ->method ('setName ' )
527+ ->willReturnSelf ();
528+ $ ImageContentInterface ->expects ($ this ->once ())
529+ ->method ('setBase64EncodedData ' )
530+ ->willReturnSelf ();
531+ $ ImageContentInterface ->expects ($ this ->once ())
532+ ->method ('setType ' )
533+ ->willReturnSelf ();
534+ $ this ->imageContentInterface ->expects ($ this ->once ())
535+ ->method ('create ' )
536+ ->willReturn ($ ImageContentInterface );
398537 $ this ->assertEquals ([$ entryMock ], $ this ->model ->getList ($ productSku ));
399538 }
400539}
0 commit comments