55 */
66namespace Magento \Catalog \Test \Unit \Model \Product ;
77
8- use Magento \Catalog \Model \View \Asset \Image \ContextFactory ;
98use Magento \Catalog \Model \View \Asset \ImageFactory ;
109use Magento \Catalog \Model \View \Asset \PlaceholderFactory ;
1110use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
1211use Magento \Framework \App \Filesystem \DirectoryList ;
13- use Magento \Framework \View \Asset \ContextInterface ;
1412
1513/**
1614 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -73,10 +71,24 @@ class ImageTest extends \PHPUnit\Framework\TestCase
7371 */
7472 private $ viewAssetPlaceholderFactory ;
7573
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+
7684 protected function setUp ()
7785 {
7886 $ objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
7987 $ 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 ));
8092
8193 $ this ->storeManager = $ this ->getMockBuilder (\Magento \Store \Model \StoreManager::class)
8294 ->disableOriginalConstructor ()
@@ -112,17 +124,36 @@ protected function setUp()
112124 ->disableOriginalConstructor ()
113125 ->setMethods (['create ' ])
114126 ->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+ );
115144
116145 $ this ->image = $ objectManager ->getObject (
117146 \Magento \Catalog \Model \Product \Image::class,
118147 [
148+ 'context ' => $ this ->context ,
119149 'storeManager ' => $ this ->storeManager ,
120150 'catalogProductMediaConfig ' => $ this ->config ,
121151 'coreFileStorageDatabase ' => $ this ->coreFileHelper ,
122152 'filesystem ' => $ this ->filesystem ,
123153 'imageFactory ' => $ this ->factory ,
124154 'viewAssetImageFactory ' => $ this ->viewAssetImageFactory ,
125- 'viewAssetPlaceholderFactory ' => $ this ->viewAssetPlaceholderFactory
155+ 'viewAssetPlaceholderFactory ' => $ this ->viewAssetPlaceholderFactory ,
156+ 'serializer ' => $ this ->serializer
126157 ]
127158 );
128159
@@ -354,12 +385,16 @@ public function testIsCached()
354385 $ this ->testSetGetBaseFile ();
355386 $ absolutePath = dirname (dirname (__DIR__ )) . '/_files/catalog/product/watermark/somefile.png ' ;
356387 $ 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+ );
357391 $ this ->assertTrue ($ this ->image ->isCached ());
358392 }
359393
360394 public function testClearCache ()
361395 {
362396 $ this ->coreFileHelper ->expects ($ this ->once ())->method ('deleteFolder ' )->will ($ this ->returnValue (true ));
397+ $ this ->cacheManager ->expects ($ this ->once ())->method ('clean ' );
363398 $ this ->image ->clearCache ();
364399 }
365400
@@ -383,4 +418,24 @@ public function testIsBaseFilePlaceholder()
383418 {
384419 $ this ->assertFalse ($ this ->image ->isBaseFilePlaceholder ());
385420 }
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+ }
386441}
0 commit comments