20
20
use Magento \Framework \Api \Data \ImageContentInterface ;
21
21
use Magento \Framework \Api \Data \ImageContentInterfaceFactory ;
22
22
use Magento \Framework \Api \ImageContentValidatorInterface ;
23
+ use Magento \Framework \App \Filesystem \DirectoryList ;
23
24
use Magento \Framework \Filesystem ;
25
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
24
26
use Magento \Framework \Filesystem \Driver \File \Mime ;
27
+ use Magento \Framework \Filesystem \DriverInterface ;
25
28
use PHPUnit \Framework \MockObject \MockObject ;
26
29
use PHPUnit \Framework \TestCase ;
27
30
use Magento \Framework \Filesystem \Io \File ;
31
+ use Magento \Catalog \Model \Product \Media \ConfigInterface as MediaConfig ;
28
32
29
33
/**
30
34
* Tests for \Magento\Catalog\Model\Product\Gallery\GalleryManagement.
@@ -107,7 +111,9 @@ protected function setUp(): void
107
111
$ this ->contentValidatorMock = $ this ->getMockForAbstractClass (ImageContentValidatorInterface::class);
108
112
$ this ->productInterfaceFactory = $ this ->createMock (ProductInterfaceFactory::class);
109
113
$ this ->deleteValidator = $ this ->createMock (DeleteValidator::class);
110
- $ this ->imageContentInterface = $ this ->createMock (ImageContentInterfaceFactory::class);
114
+ $ this ->imageContentInterface = $ this ->getMockBuilder (ImageContentInterfaceFactory::class)
115
+ ->disableOriginalConstructor ()
116
+ ->getMock ();
111
117
$ this ->filesystem = $ this ->createMock (Filesystem::class);
112
118
$ this ->mime = $ this ->createMock (Mime::class);
113
119
$ this ->file = $ this ->createMock (File::class);
@@ -121,7 +127,8 @@ protected function setUp(): void
121
127
'getCustomAttribute ' ,
122
128
'getMediaGalleryEntries ' ,
123
129
'setMediaGalleryEntries ' ,
124
- 'getMediaAttributes '
130
+ 'getMediaAttributes ' ,
131
+ 'getMediaConfig '
125
132
]
126
133
);
127
134
$ this ->mediaGalleryEntryMock =
@@ -146,6 +153,7 @@ protected function setUp(): void
146
153
->willReturn ($ this ->newProductMock );
147
154
}
148
155
156
+
149
157
/**
150
158
* @return void
151
159
*/
@@ -413,6 +421,55 @@ public function testGet(): void
413
421
$ existingEntryMock ->expects ($ this ->once ())->method ('getId ' )->willReturn (42 );
414
422
$ this ->productMock ->expects ($ this ->once ())->method ('getMediaGalleryEntries ' )
415
423
->willReturn ([$ existingEntryMock ]);
424
+ $ mediaConfigMock = $ this ->getMockBuilder (MediaConfig::class)
425
+ ->disableOriginalConstructor ()
426
+ ->getMock ();
427
+ $ mediaConfigMock ->expects ($ this ->once ())
428
+ ->method ('getMediaPath ' )
429
+ ->willReturn ("base/path/test123.jpg " );
430
+ $ this ->productMock ->expects ($ this ->once ())
431
+ ->method ('getMediaConfig ' )
432
+ ->willReturn ($ mediaConfigMock );
433
+ $ mediaDirectoryMock = $ this ->getMockBuilder (WriteInterface::class)
434
+ ->disableOriginalConstructor ()
435
+ ->getMock ();
436
+ $ this ->filesystem ->expects ($ this ->once ())
437
+ ->method ('getDirectoryWrite ' )
438
+ ->with (DirectoryList::MEDIA )
439
+ ->willReturn ($ mediaDirectoryMock );
440
+ $ mediaDirectoryMock ->expects ($ this ->once ())
441
+ ->method ('getAbsolutePath ' )
442
+ ->with ('base/path/test123.jpg ' )
443
+ ->willReturn ('absolute/path/base/path/test123.jpg ' );
444
+ $ this ->file ->expects ($ this ->any ())
445
+ ->method ('getPathInfo ' )
446
+ ->willReturnCallback (
447
+ function ($ path ) {
448
+ return pathinfo ($ path );
449
+ }
450
+ );
451
+ $ driverMock = $ this ->getMockBuilder (DriverInterface::class)
452
+ ->disableOriginalConstructor ()
453
+ ->getMock ();
454
+ $ mediaDirectoryMock ->expects ($ this ->any ())->method ('getDriver ' )->willReturn ($ driverMock );
455
+ $ driverMock ->expects ($ this ->once ())
456
+ ->method ('fileGetContents ' )
457
+ ->willReturn ('0123456789abcdefghijklmnopqrstuvwxyz ' );
458
+ $ ImageContentInterface = $ this ->getMockBuilder (ImageContentInterface::class)
459
+ ->disableOriginalConstructor ()
460
+ ->getMock ();
461
+ $ ImageContentInterface ->expects ($ this ->once ())
462
+ ->method ('setName ' )
463
+ ->willReturnSelf ();
464
+ $ ImageContentInterface ->expects ($ this ->once ())
465
+ ->method ('setBase64EncodedData ' )
466
+ ->willReturnSelf ();
467
+ $ ImageContentInterface ->expects ($ this ->once ())
468
+ ->method ('setType ' )
469
+ ->willReturnSelf ();
470
+ $ this ->imageContentInterface ->expects ($ this ->once ())
471
+ ->method ('create ' )
472
+ ->willReturn ($ ImageContentInterface );
416
473
$ this ->assertEquals ($ existingEntryMock , $ this ->model ->get ($ productSku , $ imageId ));
417
474
}
418
475
@@ -427,6 +484,57 @@ public function testGetList(): void
427
484
$ entryMock = $ this ->getMockForAbstractClass (ProductAttributeMediaGalleryEntryInterface::class);
428
485
$ this ->productMock ->expects ($ this ->once ())->method ('getMediaGalleryEntries ' )
429
486
->willReturn ([$ entryMock ]);
487
+ $ this ->productMock ->expects ($ this ->once ())->method ('getMediaGalleryEntries ' )
488
+ ->willReturn ([$ entryMock ]);
489
+ $ mediaConfigMock = $ this ->getMockBuilder (MediaConfig::class)
490
+ ->disableOriginalConstructor ()
491
+ ->getMock ();
492
+ $ mediaConfigMock ->expects ($ this ->once ())
493
+ ->method ('getMediaPath ' )
494
+ ->willReturn ("base/path/test123.jpg " );
495
+ $ this ->productMock ->expects ($ this ->once ())
496
+ ->method ('getMediaConfig ' )
497
+ ->willReturn ($ mediaConfigMock );
498
+ $ mediaDirectoryMock = $ this ->getMockBuilder (WriteInterface::class)
499
+ ->disableOriginalConstructor ()
500
+ ->getMock ();
501
+ $ this ->filesystem ->expects ($ this ->once ())
502
+ ->method ('getDirectoryWrite ' )
503
+ ->with (DirectoryList::MEDIA )
504
+ ->willReturn ($ mediaDirectoryMock );
505
+ $ mediaDirectoryMock ->expects ($ this ->once ())
506
+ ->method ('getAbsolutePath ' )
507
+ ->with ('base/path/test123.jpg ' )
508
+ ->willReturn ('absolute/path/base/path/test123.jpg ' );
509
+ $ this ->file ->expects ($ this ->any ())
510
+ ->method ('getPathInfo ' )
511
+ ->willReturnCallback (
512
+ function ($ path ) {
513
+ return pathinfo ($ path );
514
+ }
515
+ );
516
+ $ driverMock = $ this ->getMockBuilder (DriverInterface::class)
517
+ ->disableOriginalConstructor ()
518
+ ->getMock ();
519
+ $ mediaDirectoryMock ->expects ($ this ->any ())->method ('getDriver ' )->willReturn ($ driverMock );
520
+ $ driverMock ->expects ($ this ->once ())
521
+ ->method ('fileGetContents ' )
522
+ ->willReturn ('0123456789abcdefghijklmnopqrstuvwxyz ' );
523
+ $ ImageContentInterface = $ this ->getMockBuilder (ImageContentInterface::class)
524
+ ->disableOriginalConstructor ()
525
+ ->getMock ();
526
+ $ ImageContentInterface ->expects ($ this ->once ())
527
+ ->method ('setName ' )
528
+ ->willReturnSelf ();
529
+ $ ImageContentInterface ->expects ($ this ->once ())
530
+ ->method ('setBase64EncodedData ' )
531
+ ->willReturnSelf ();
532
+ $ ImageContentInterface ->expects ($ this ->once ())
533
+ ->method ('setType ' )
534
+ ->willReturnSelf ();
535
+ $ this ->imageContentInterface ->expects ($ this ->once ())
536
+ ->method ('create ' )
537
+ ->willReturn ($ ImageContentInterface );
430
538
$ this ->assertEquals ([$ entryMock ], $ this ->model ->getList ($ productSku ));
431
539
}
432
540
}
0 commit comments