Skip to content

Commit 243acff

Browse files
ENGCOM-6463: Fix caching Magento Metadata getVersion #26001
2 parents c125248 + 246fa5c commit 243acff

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/internal/Magento/Framework/App/ProductMetadata.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
use Magento\Framework\Composer\ComposerInformation;
1414

1515
/**
16-
* Class ProductMetadata
17-
*
18-
* @package Magento\Framework\App
16+
* Magento application product metadata
1917
*/
2018
class ProductMetadata implements ProductMetadataInterface
2119
{
@@ -85,8 +83,8 @@ public function getVersion()
8583
} else {
8684
$this->version = 'UNKNOWN';
8785
}
88-
$this->cache->save($this->version, self::VERSION_CACHE_KEY, [Config::CACHE_TAG]);
8986
}
87+
$this->cache->save($this->version, self::VERSION_CACHE_KEY, [Config::CACHE_TAG]);
9088
}
9189
return $this->version;
9290
}

lib/internal/Magento/Framework/App/Test/Unit/ProductMetadataTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Framework\App\Test\Unit;
77

8+
use Magento\Framework\App\CacheInterface;
89
use Magento\Framework\App\ProductMetadata;
910
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1011

@@ -20,13 +21,20 @@ class ProductMetadataTest extends \PHPUnit\Framework\TestCase
2021
*/
2122
private $composerInformationMock;
2223

24+
/**
25+
* @var CacheInterface|\PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
private $cacheMock;
28+
2329
protected function setUp()
2430
{
2531
$this->composerInformationMock = $this->getMockBuilder(\Magento\Framework\Composer\ComposerInformation::class)
2632
->disableOriginalConstructor()->getMock();
2733

34+
$this->cacheMock = $this->getMockBuilder(CacheInterface::class)->getMock();
35+
2836
$objectManager = new ObjectManager($this);
29-
$this->productMetadata = $objectManager->getObject(ProductMetadata::class);
37+
$this->productMetadata = $objectManager->getObject(ProductMetadata::class, ['cache' => $this->cacheMock]);
3038
$reflectionProperty = new \ReflectionProperty($this->productMetadata, 'composerInformation');
3139
$reflectionProperty->setAccessible(true);
3240
$reflectionProperty->setValue($this->productMetadata, $this->composerInformationMock);
@@ -40,11 +48,22 @@ protected function setUp()
4048
public function testGetVersion($packageList, $expectedVersion)
4149
{
4250
$this->composerInformationMock->expects($this->any())->method('getSystemPackages')->willReturn($packageList);
51+
$this->cacheMock->expects($this->once())->method('save')->with($expectedVersion);
4352
$productVersion = $this->productMetadata->getVersion();
4453
$this->assertNotEmpty($productVersion, 'Empty product version');
4554
$this->assertEquals($expectedVersion, $productVersion);
4655
}
4756

57+
public function testGetVersionCached()
58+
{
59+
$expectedVersion = '1.2.3';
60+
$this->composerInformationMock->expects($this->never())->method('getSystemPackages');
61+
$this->cacheMock->expects($this->once())->method('load')->willReturn($expectedVersion);
62+
$this->cacheMock->expects($this->never())->method('save');
63+
$productVersion = $this->productMetadata->getVersion();
64+
$this->assertEquals($expectedVersion, $productVersion);
65+
}
66+
4867
/**
4968
* @return array
5069
*/

0 commit comments

Comments
 (0)