5
5
*/
6
6
namespace Magento \Catalog \Test \Unit \Model \Product ;
7
7
8
- use Magento \Catalog \Model \View \Asset \Image \ContextFactory ;
9
8
use Magento \Catalog \Model \View \Asset \ImageFactory ;
10
9
use Magento \Catalog \Model \View \Asset \PlaceholderFactory ;
11
10
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
12
11
use Magento \Framework \App \Filesystem \DirectoryList ;
13
- use Magento \Framework \View \Asset \ContextInterface ;
14
12
15
13
/**
16
14
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -73,10 +71,24 @@ class ImageTest extends \PHPUnit\Framework\TestCase
73
71
*/
74
72
private $ viewAssetPlaceholderFactory ;
75
73
74
+ /**
75
+ * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
76
+ */
77
+ private $ serializer ;
78
+
79
+ /**
80
+ * @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
81
+ */
82
+ private $ cacheManager ;
83
+
76
84
protected function setUp ()
77
85
{
78
86
$ objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
79
87
$ this ->context = $ this ->createMock (\Magento \Framework \Model \Context::class);
88
+ $ this ->cacheManager = $ this ->getMockBuilder (\Magento \Framework \App \CacheInterface::class)
89
+ ->disableOriginalConstructor ()
90
+ ->getMockForAbstractClass ();
91
+ $ this ->context ->expects ($ this ->any ())->method ('getCacheManager ' )->will ($ this ->returnValue ($ this ->cacheManager ));
80
92
81
93
$ this ->storeManager = $ this ->getMockBuilder (\Magento \Store \Model \StoreManager::class)
82
94
->disableOriginalConstructor ()
@@ -112,17 +124,36 @@ protected function setUp()
112
124
->disableOriginalConstructor ()
113
125
->setMethods (['create ' ])
114
126
->getMock ();
127
+ $ this ->serializer = $ this ->getMockBuilder (
128
+ \Magento \Framework \Serialize \SerializerInterface::class
129
+ )->getMockForAbstractClass ();
130
+ $ this ->serializer ->expects ($ this ->any ())
131
+ ->method ('serialize ' )
132
+ ->willReturnCallback (
133
+ function ($ value ) {
134
+ return json_encode ($ value );
135
+ }
136
+ );
137
+ $ this ->serializer ->expects ($ this ->any ())
138
+ ->method ('unserialize ' )
139
+ ->willReturnCallback (
140
+ function ($ value ) {
141
+ return json_decode ($ value , true );
142
+ }
143
+ );
115
144
116
145
$ this ->image = $ objectManager ->getObject (
117
146
\Magento \Catalog \Model \Product \Image::class,
118
147
[
148
+ 'context ' => $ this ->context ,
119
149
'storeManager ' => $ this ->storeManager ,
120
150
'catalogProductMediaConfig ' => $ this ->config ,
121
151
'coreFileStorageDatabase ' => $ this ->coreFileHelper ,
122
152
'filesystem ' => $ this ->filesystem ,
123
153
'imageFactory ' => $ this ->factory ,
124
154
'viewAssetImageFactory ' => $ this ->viewAssetImageFactory ,
125
- 'viewAssetPlaceholderFactory ' => $ this ->viewAssetPlaceholderFactory
155
+ 'viewAssetPlaceholderFactory ' => $ this ->viewAssetPlaceholderFactory ,
156
+ 'serializer ' => $ this ->serializer
126
157
]
127
158
);
128
159
@@ -354,12 +385,16 @@ public function testIsCached()
354
385
$ this ->testSetGetBaseFile ();
355
386
$ absolutePath = dirname (dirname (__DIR__ )) . '/_files/catalog/product/watermark/somefile.png ' ;
356
387
$ this ->imageAsset ->expects ($ this ->any ())->method ('getPath ' )->willReturn ($ absolutePath );
388
+ $ this ->cacheManager ->expects ($ this ->once ())->method ('load ' )->willReturn (
389
+ json_encode (['size ' => ['image data ' ]])
390
+ );
357
391
$ this ->assertTrue ($ this ->image ->isCached ());
358
392
}
359
393
360
394
public function testClearCache ()
361
395
{
362
396
$ this ->coreFileHelper ->expects ($ this ->once ())->method ('deleteFolder ' )->will ($ this ->returnValue (true ));
397
+ $ this ->cacheManager ->expects ($ this ->once ())->method ('clean ' );
363
398
$ this ->image ->clearCache ();
364
399
}
365
400
@@ -383,4 +418,24 @@ public function testIsBaseFilePlaceholder()
383
418
{
384
419
$ this ->assertFalse ($ this ->image ->isBaseFilePlaceholder ());
385
420
}
421
+
422
+ public function testGetResizedImageInfoWithCache ()
423
+ {
424
+ $ absolutePath = dirname (dirname (__DIR__ )) . '/_files/catalog/product/watermark/somefile.png ' ;
425
+ $ this ->imageAsset ->expects ($ this ->any ())->method ('getPath ' )->willReturn ($ absolutePath );
426
+ $ this ->cacheManager ->expects ($ this ->once ())->method ('load ' )->willReturn (
427
+ json_encode (['size ' => ['image data ' ]])
428
+ );
429
+ $ this ->cacheManager ->expects ($ this ->never ())->method ('save ' );
430
+ $ this ->assertEquals (['image data ' ], $ this ->image ->getResizedImageInfo ());
431
+ }
432
+
433
+ public function testGetResizedImageInfoEmptyCache ()
434
+ {
435
+ $ absolutePath = dirname (dirname (__DIR__ )) . '/_files/catalog/product/watermark/somefile.png ' ;
436
+ $ this ->imageAsset ->expects ($ this ->any ())->method ('getPath ' )->willReturn ($ absolutePath );
437
+ $ this ->cacheManager ->expects ($ this ->once ())->method ('load ' )->willReturn (false );
438
+ $ this ->cacheManager ->expects ($ this ->once ())->method ('save ' );
439
+ $ this ->assertTrue (is_array ($ this ->image ->getResizedImageInfo ()));
440
+ }
386
441
}
0 commit comments