Skip to content

Commit

Permalink
Add tests for cached getVersion and update class description
Browse files Browse the repository at this point in the history
  • Loading branch information
luklewluk committed Dec 13, 2019
1 parent 16ab63b commit ab6cabd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/internal/Magento/Framework/App/ProductMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
use Magento\Framework\Composer\ComposerInformation;

/**
* Class ProductMetadata
*
* @package Magento\Framework\App
* Magento application product metadata
*/
class ProductMetadata implements ProductMetadataInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Framework\App\Test\Unit;

use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\ProductMetadata;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

Expand All @@ -20,16 +21,27 @@ class ProductMetadataTest extends \PHPUnit\Framework\TestCase
*/
private $composerInformationMock;

/**
* @var CacheInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $cacheMock;

protected function setUp()
{
$this->composerInformationMock = $this->getMockBuilder(\Magento\Framework\Composer\ComposerInformation::class)
->disableOriginalConstructor()->getMock();

$this->cacheMock = $this->getMockBuilder(CacheInterface::class)->getMock();

$objectManager = new ObjectManager($this);
$this->productMetadata = $objectManager->getObject(ProductMetadata::class);
$reflectionProperty = new \ReflectionProperty($this->productMetadata, 'composerInformation');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->productMetadata, $this->composerInformationMock);

$reflectionProperty = new \ReflectionProperty($this->productMetadata, 'cache');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->productMetadata, $this->cacheMock);
}

/**
Expand All @@ -40,11 +52,21 @@ protected function setUp()
public function testGetVersion($packageList, $expectedVersion)
{
$this->composerInformationMock->expects($this->any())->method('getSystemPackages')->willReturn($packageList);
$this->cacheMock->expects($this->once())->method('save')->with($expectedVersion);
$productVersion = $this->productMetadata->getVersion();
$this->assertNotEmpty($productVersion, 'Empty product version');
$this->assertEquals($expectedVersion, $productVersion);
}

public function testGetVersionCached()
{
$expectedVersion = '1.2.3';
$this->composerInformationMock->expects($this->never())->method('getSystemPackages');
$this->cacheMock->expects($this->once())->method('load')->willReturn($expectedVersion);
$productVersion = $this->productMetadata->getVersion();
$this->assertEquals($expectedVersion, $productVersion);
}

/**
* @return array
*/
Expand Down

0 comments on commit ab6cabd

Please sign in to comment.