Skip to content

Commit 3e86102

Browse files
committed
MAGETWO-84480: Add cache for getimagesize() function for product images
1 parent 164e946 commit 3e86102

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

app/code/Magento/Catalog/Model/Product/Image.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ public function clearCache()
791791
$this->_mediaDirectory->delete($directory);
792792

793793
$this->_coreFileStorageDatabase->deleteFolder($this->_mediaDirectory->getAbsolutePath($directory));
794+
$this->clearImageInfoFromCache();
794795
}
795796

796797
/**
@@ -898,7 +899,8 @@ private function saveImageInfoToCache(array $imageInfo, string $imagePath)
898899
$imagePath = $this->cachePrefix . $imagePath;
899900
$this->_cacheManager->save(
900901
$this->serializer->serialize($imageInfo),
901-
$imagePath
902+
$imagePath,
903+
[$this->cachePrefix]
902904
);
903905
}
904906

@@ -918,4 +920,14 @@ private function loadImageInfoFromCache(string $imagePath)
918920
return $this->serializer->unserialize($cacheData);
919921
}
920922
}
923+
924+
/**
925+
* Clear image data from cache
926+
*
927+
* @return void
928+
*/
929+
private function clearImageInfoFromCache()
930+
{
931+
$this->_cacheManager->clean([$this->cachePrefix]);
932+
}
921933
}

app/code/Magento/Catalog/Test/Unit/Model/Product/ImageTest.php

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
*/
66
namespace Magento\Catalog\Test\Unit\Model\Product;
77

8-
use Magento\Catalog\Model\View\Asset\Image\ContextFactory;
98
use Magento\Catalog\Model\View\Asset\ImageFactory;
109
use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
1110
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1211
use 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

Comments
 (0)