Skip to content

Commit

Permalink
Merge pull request #638 from magento-extensibility/develop
Browse files Browse the repository at this point in the history
[Extensibility] MAGETWO-51566
  • Loading branch information
Korshenko, Oleksii(okorshenko) committed May 18, 2016
2 parents 6144265 + b4d2dc9 commit 26064de
Show file tree
Hide file tree
Showing 11 changed files with 505 additions and 144 deletions.
6 changes: 1 addition & 5 deletions bin/magento
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
use \Magento\Framework\App\ProductMetadata;
use \Magento\Framework\Composer\ComposerJsonFinder;
use Magento\Framework\App\Filesystem\DirectoryList;

if (PHP_SAPI !== 'cli') {
echo 'bin/magento must be run as a CLI application';
Expand All @@ -22,8 +19,7 @@ try {
try {
$handler = new \Magento\Framework\App\ErrorHandler();
set_error_handler([$handler, 'handler']);
$productMetadata = new ProductMetadata(new ComposerJsonFinder(new DirectoryList(BP)));
$application = new Magento\Framework\Console\Cli('Magento CLI', $productMetadata->getVersion());
$application = new Magento\Framework\Console\Cli('Magento CLI');
$application->run();
} catch (\Exception $e) {
while ($e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class ComposerInformationTest extends \PHPUnit_Framework_TestCase
*/
private $composerJsonFinder;

/**
* @var ComposerFactory
*/
private $composerFactory;

public function setUp()
{
$this->objectManager = Bootstrap::getObjectManager();
Expand Down Expand Up @@ -58,6 +63,7 @@ private function setupDirectory($composerDir)
);

$this->composerJsonFinder = new ComposerJsonFinder($this->directoryList);
$this->composerFactory = new ComposerFactory($this->directoryList, $this->composerJsonFinder);
}

/**
Expand All @@ -72,12 +78,7 @@ public function testGetRequiredPhpVersion($composerDir)
/** @var \Magento\Framework\Composer\ComposerInformation $composerInfo */
$composerInfo = $this->objectManager->create(
'Magento\Framework\Composer\ComposerInformation',
[
'applicationFactory' => new MagentoComposerApplicationFactory(
$this->composerJsonFinder,
$this->directoryList
)
]
['composerFactory' => $this->composerFactory]
);

$this->assertEquals("~5.5.0|~5.6.0|~7.0.0", $composerInfo->getRequiredPhpVersion());
Expand All @@ -96,12 +97,7 @@ public function testGetRequiredExtensions($composerDir)
/** @var \Magento\Framework\Composer\ComposerInformation $composerInfo */
$composerInfo = $this->objectManager->create(
'Magento\Framework\Composer\ComposerInformation',
[
'applicationFactory' => new MagentoComposerApplicationFactory(
$this->composerJsonFinder,
$this->directoryList
)
]
['composerFactory' => $this->composerFactory]
);

$actualRequiredExtensions = $composerInfo->getRequiredExtensions();
Expand All @@ -120,12 +116,7 @@ public function testGetSuggestedPackages($composerDir)
$this->setupDirectory($composerDir);
$composerInfo = $this->objectManager->create(
'Magento\Framework\Composer\ComposerInformation',
[
'applicationFactory' => new MagentoComposerApplicationFactory(
$this->composerJsonFinder,
$this->directoryList
)
]
['composerFactory' => $this->composerFactory]
);
$actualSuggestedExtensions = $composerInfo->getSuggestedPackages();
$this->assertArrayHasKey('psr/log', $actualSuggestedExtensions);
Expand All @@ -143,12 +134,7 @@ public function testGetRootRequiredPackagesAndTypes($composerDir)
/** @var \Magento\Framework\Composer\ComposerInformation $composerInfo */
$composerInfo = $this->objectManager->create(
'Magento\Framework\Composer\ComposerInformation',
[
'applicationFactory' => new MagentoComposerApplicationFactory(
$this->composerJsonFinder,
$this->directoryList
)
]
['composerFactory' => $this->composerFactory]
);

$requiredPackagesAndTypes = $composerInfo->getRootRequiredPackageTypesByName();
Expand All @@ -171,37 +157,14 @@ public function getRequiredPhpVersionDataProvider()
];
}

/**
* @expectedException \Exception
* @expectedExceptionMessage Composer file not found
*/
public function testNoLock()
{
$this->setupDirectory('notARealDirectory');
$this->objectManager->create(
'Magento\Framework\Composer\ComposerInformation',
[
'applicationFactory' => new MagentoComposerApplicationFactory(
$this->composerJsonFinder,
$this->directoryList
)
]
);
}

public function testIsPackageInComposerJson()
{
$this->setupDirectory('testSkeleton');

/** @var \Magento\Framework\Composer\ComposerInformation $composerInfo */
$composerInfo = $this->objectManager->create(
'Magento\Framework\Composer\ComposerInformation',
[
'applicationFactory' => new MagentoComposerApplicationFactory(
$this->composerJsonFinder,
$this->directoryList
)
]
['composerFactory' => $this->composerFactory]
);

$packageName = 'magento/sample-module-minimal';
Expand All @@ -222,17 +185,33 @@ public function testGetRootRepositories($composerDir)
/** @var \Magento\Framework\Composer\ComposerInformation $composerInfo */
$composerInfo = $this->objectManager->create(
'Magento\Framework\Composer\ComposerInformation',
[
'applicationFactory' => new MagentoComposerApplicationFactory(
$this->composerJsonFinder,
$this->directoryList
)
]
['composerFactory' => $this->composerFactory]
);
if ($composerDir === 'testFromCreateProject') {
$this->assertEquals(['https://repo.magento.com/'], $composerInfo->getRootRepositories());
} else {
$this->assertEquals([], $composerInfo->getRootRepositories());
}
}

/**
* @param $composerDir string Directory under _files that contains composer files
*
* @dataProvider getRequiredPhpVersionDataProvider
*/
public function testIsMagentoRoot($composerDir)
{
$this->setupDirectory($composerDir);

/** @var \Magento\Framework\Composer\ComposerInformation $composerInfo */
$composerInfo = $this->objectManager->create(
'Magento\Framework\Composer\ComposerInformation',
['composerFactory' => $this->composerFactory]
);
if ($composerDir === 'testFromClone') {
$this->assertTrue($composerInfo->isMagentoRoot());
} else {
$this->assertFalse($composerInfo->isMagentoRoot());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Magento\Setup\Model;

use Magento\Framework\Composer\ComposerFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Framework\Composer\ComposerJsonFinder;
use Magento\Framework\Composer\MagentoComposerApplicationFactory;
Expand Down Expand Up @@ -66,9 +67,9 @@ private function setupDirectory($composerDir)
$this->composerInformation = $this->objectManager->create(
'Magento\Framework\Composer\ComposerInformation',
[
'applicationFactory' => new MagentoComposerApplicationFactory(
$this->composerJsonFinder,
$this->directoryList
'composerFactory' => new ComposerFactory(
$this->directoryList,
$this->composerJsonFinder
)
]
);
Expand Down
79 changes: 63 additions & 16 deletions lib/internal/Magento/Framework/App/ProductMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,25 @@
*/
namespace Magento\Framework\App;

use Magento\Framework\Composer\ComposerFactory;
use \Magento\Framework\Composer\ComposerJsonFinder;
use \Magento\Framework\App\Filesystem\DirectoryList;
use \Magento\Framework\Composer\ComposerInformation;

/**
* Class ProductMetadata
* @package Magento\Framework\App
*/
class ProductMetadata implements ProductMetadataInterface
{
/**
* Magento product edition
*/
const EDITION_NAME = 'Community';

/**
* Magento product name
*/
const PRODUCT_NAME = 'Magento';

/**
Expand All @@ -21,13 +37,19 @@ class ProductMetadata implements ProductMetadataInterface

/**
* @var \Magento\Framework\Composer\ComposerJsonFinder
* @deprecated
*/
protected $composerJsonFinder;

/**
* @param \Magento\Framework\Composer\ComposerJsonFinder $composerJsonFinder
* @var \Magento\Framework\Composer\ComposerInformation
*/
public function __construct(\Magento\Framework\Composer\ComposerJsonFinder $composerJsonFinder)
private $composerInformation;

/**
* @param ComposerJsonFinder $composerJsonFinder
*/
public function __construct(ComposerJsonFinder $composerJsonFinder)
{
$this->composerJsonFinder = $composerJsonFinder;
}
Expand All @@ -36,25 +58,17 @@ public function __construct(\Magento\Framework\Composer\ComposerJsonFinder $comp
* Get Product version
*
* @return string
* @throws \Exception
*/
public function getVersion()
{
if (!$this->version) {
$composerJsonFile = $this->composerJsonFinder->findComposerJson();

$composerContent = file_get_contents($composerJsonFile);
if ($composerContent === false) {
throw new \Exception('Composer file content is empty');
if (!($this->version = $this->getSystemPackageVersion())) {
if ($this->getComposerInformation()->isMagentoRoot()) {
$this->version = $this->getComposerInformation()->getRootPackage()->getPrettyVersion();
} else {
$this->version = 'UNKNOWN';
}
}
$composerContent = json_decode($composerContent, true);
if (!$composerContent
|| !is_array($composerContent)
|| !array_key_exists('version', $composerContent)
) {
throw new \Exception('Unable to decode Composer file');
}
$this->version = $composerContent['version'];
}
return $this->version;
}
Expand All @@ -78,4 +92,37 @@ public function getName()
{
return self::PRODUCT_NAME;
}

/**
* Get version from system package
*
* @return string
* @deprecated
*/
private function getSystemPackageVersion()
{
$packages = $this->getComposerInformation()->getSystemPackages();
foreach ($packages as $package) {
if (isset($package['name']) && isset($package['version'])) {
return $package['version'];
}
}
return '';
}

/**
* Load composerInformation
*
* @return ComposerInformation
* @deprecated
*/
private function getComposerInformation()
{
if (!$this->composerInformation) {
$directoryList = new DirectoryList(BP);
$composerFactory = new ComposerFactory($directoryList, $this->composerJsonFinder);
$this->composerInformation = new ComposerInformation($composerFactory);
}
return $this->composerInformation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,67 @@
*/
namespace Magento\Framework\App\Test\Unit;

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

class ProductMetadataTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Framework\App\ProductMetadata
* @var ProductMetadata
*/
protected $productMetadata;
private $productMetadata;

/**
* @var \Magento\Framework\Composer\ComposerInformation|\PHPUnit_Framework_MockObject_MockObject
*/
private $composerInformationMock;

protected function setUp()
{
$composerJsonFinder = $this->getMockBuilder('Magento\Framework\Composer\ComposerJsonFinder')
->disableOriginalConstructor()->setMethods(['findComposerJson'])->getMock();
$composerJsonFinder->expects($this->any())->method('findComposerJson')
->willReturn(realpath(__DIR__ . '/_files/test.composer.json'));
$this->composerInformationMock = $this->getMockBuilder(\Magento\Framework\Composer\ComposerInformation::class)
->disableOriginalConstructor()->getMock();

$objectManager = new ObjectManager($this);
$this->productMetadata = $objectManager->getObject(
'Magento\Framework\App\ProductMetadata',
['composerJsonFinder' => $composerJsonFinder]
);
$this->productMetadata = $objectManager->getObject(ProductMetadata::class);
$reflectionProperty = new \ReflectionProperty($this->productMetadata, 'composerInformation');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->productMetadata, $this->composerInformationMock);
}

public function testGetVersion()
/**
* @param array $packageList
* @param string $expectedVersion
* @dataProvider testGetVersionGitInstallationDataProvider
*/
public function testGetVersion($packageList, $expectedVersion)
{
$this->composerInformationMock->expects($this->any())->method('getSystemPackages')->willReturn($packageList);
$productVersion = $this->productMetadata->getVersion();
$this->assertNotEmpty($productVersion, 'Empty product version');
preg_match('/^([0-9\.]+)/', $productVersion, $matches);
$this->assertArrayHasKey(1, $matches, 'Invalid product version');
$this->assertNotEmpty($matches, 'Empty product version');
$this->assertEquals($expectedVersion, $productVersion);
}

public function testGetVersionGitInstallationDataProvider()
{
return [
[
[
0 => [
'name' => 'magento/product-community-edition',
'version' => '123.456.789'
],
1 => [
'name' => 'magento/product-other-edition',
'version' => '987.654.321'
],
],
'123.456.789'
],
[
[],
'UNKNOWN'
]
];
}

public function testGetEdition()
Expand Down
Loading

0 comments on commit 26064de

Please sign in to comment.