18
18
use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
19
19
use Magento \Framework \Api \AttributeValue ;
20
20
use Magento \Framework \Api \Data \ImageContentInterface ;
21
+ use Magento \Framework \Api \Data \ImageContentInterfaceFactory ;
21
22
use 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 ;
22
28
use PHPUnit \Framework \MockObject \MockObject ;
23
29
use PHPUnit \Framework \TestCase ;
30
+ use Magento \Framework \Filesystem \Io \File ;
31
+ use Magento \Catalog \Model \Product \Media \ConfigInterface as MediaConfig ;
24
32
25
33
/**
26
34
* Tests for \Magento\Catalog\Model\Product\Gallery\GalleryManagement.
@@ -74,6 +82,26 @@ class GalleryManagementTest extends TestCase
74
82
*/
75
83
private $ newProductMock ;
76
84
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
+
77
105
/**
78
106
* @inheritDoc
79
107
*/
@@ -83,6 +111,12 @@ protected function setUp(): void
83
111
$ this ->contentValidatorMock = $ this ->getMockForAbstractClass (ImageContentValidatorInterface::class);
84
112
$ this ->productInterfaceFactory = $ this ->createMock (ProductInterfaceFactory::class);
85
113
$ 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);
86
120
$ this ->productMock = $ this ->createPartialMock (
87
121
Product::class,
88
122
[
@@ -93,7 +127,8 @@ protected function setUp(): void
93
127
'getCustomAttribute ' ,
94
128
'getMediaGalleryEntries ' ,
95
129
'setMediaGalleryEntries ' ,
96
- 'getMediaAttributes '
130
+ 'getMediaAttributes ' ,
131
+ 'getMediaConfig '
97
132
]
98
133
);
99
134
$ this ->mediaGalleryEntryMock =
@@ -102,7 +137,11 @@ protected function setUp(): void
102
137
$ this ->productRepositoryMock ,
103
138
$ this ->contentValidatorMock ,
104
139
$ this ->productInterfaceFactory ,
105
- $ this ->deleteValidator
140
+ $ this ->deleteValidator ,
141
+ $ this ->imageContentInterface ,
142
+ $ this ->filesystem ,
143
+ $ this ->mime ,
144
+ $ this ->file ,
106
145
);
107
146
$ this ->attributeValueMock = $ this ->getMockBuilder (AttributeValue::class)
108
147
->disableOriginalConstructor ()
@@ -381,6 +420,55 @@ public function testGet(): void
381
420
$ existingEntryMock ->expects ($ this ->once ())->method ('getId ' )->willReturn (42 );
382
421
$ this ->productMock ->expects ($ this ->once ())->method ('getMediaGalleryEntries ' )
383
422
->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 );
384
472
$ this ->assertEquals ($ existingEntryMock , $ this ->model ->get ($ productSku , $ imageId ));
385
473
}
386
474
@@ -395,6 +483,57 @@ public function testGetList(): void
395
483
$ entryMock = $ this ->getMockForAbstractClass (ProductAttributeMediaGalleryEntryInterface::class);
396
484
$ this ->productMock ->expects ($ this ->once ())->method ('getMediaGalleryEntries ' )
397
485
->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 );
398
537
$ this ->assertEquals ([$ entryMock ], $ this ->model ->getList ($ productSku ));
399
538
}
400
539
}
0 commit comments