Skip to content

Commit 26064de

Browse files
author
Korshenko, Oleksii(okorshenko)
committed
Merge pull request #638 from magento-extensibility/develop
[Extensibility] MAGETWO-51566
2 parents 6144265 + b4d2dc9 commit 26064de

File tree

11 files changed

+505
-144
lines changed

11 files changed

+505
-144
lines changed

bin/magento

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
* Copyright © 2016 Magento. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7-
use \Magento\Framework\App\ProductMetadata;
8-
use \Magento\Framework\Composer\ComposerJsonFinder;
9-
use Magento\Framework\App\Filesystem\DirectoryList;
107

118
if (PHP_SAPI !== 'cli') {
129
echo 'bin/magento must be run as a CLI application';
@@ -22,8 +19,7 @@ try {
2219
try {
2320
$handler = new \Magento\Framework\App\ErrorHandler();
2421
set_error_handler([$handler, 'handler']);
25-
$productMetadata = new ProductMetadata(new ComposerJsonFinder(new DirectoryList(BP)));
26-
$application = new Magento\Framework\Console\Cli('Magento CLI', $productMetadata->getVersion());
22+
$application = new Magento\Framework\Console\Cli('Magento CLI');
2723
$application->run();
2824
} catch (\Exception $e) {
2925
while ($e) {

dev/tests/integration/testsuite/Magento/Framework/Composer/ComposerInformationTest.php

Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class ComposerInformationTest extends \PHPUnit_Framework_TestCase
2929
*/
3030
private $composerJsonFinder;
3131

32+
/**
33+
* @var ComposerFactory
34+
*/
35+
private $composerFactory;
36+
3237
public function setUp()
3338
{
3439
$this->objectManager = Bootstrap::getObjectManager();
@@ -58,6 +63,7 @@ private function setupDirectory($composerDir)
5863
);
5964

6065
$this->composerJsonFinder = new ComposerJsonFinder($this->directoryList);
66+
$this->composerFactory = new ComposerFactory($this->directoryList, $this->composerJsonFinder);
6167
}
6268

6369
/**
@@ -72,12 +78,7 @@ public function testGetRequiredPhpVersion($composerDir)
7278
/** @var \Magento\Framework\Composer\ComposerInformation $composerInfo */
7379
$composerInfo = $this->objectManager->create(
7480
'Magento\Framework\Composer\ComposerInformation',
75-
[
76-
'applicationFactory' => new MagentoComposerApplicationFactory(
77-
$this->composerJsonFinder,
78-
$this->directoryList
79-
)
80-
]
81+
['composerFactory' => $this->composerFactory]
8182
);
8283

8384
$this->assertEquals("~5.5.0|~5.6.0|~7.0.0", $composerInfo->getRequiredPhpVersion());
@@ -96,12 +97,7 @@ public function testGetRequiredExtensions($composerDir)
9697
/** @var \Magento\Framework\Composer\ComposerInformation $composerInfo */
9798
$composerInfo = $this->objectManager->create(
9899
'Magento\Framework\Composer\ComposerInformation',
99-
[
100-
'applicationFactory' => new MagentoComposerApplicationFactory(
101-
$this->composerJsonFinder,
102-
$this->directoryList
103-
)
104-
]
100+
['composerFactory' => $this->composerFactory]
105101
);
106102

107103
$actualRequiredExtensions = $composerInfo->getRequiredExtensions();
@@ -120,12 +116,7 @@ public function testGetSuggestedPackages($composerDir)
120116
$this->setupDirectory($composerDir);
121117
$composerInfo = $this->objectManager->create(
122118
'Magento\Framework\Composer\ComposerInformation',
123-
[
124-
'applicationFactory' => new MagentoComposerApplicationFactory(
125-
$this->composerJsonFinder,
126-
$this->directoryList
127-
)
128-
]
119+
['composerFactory' => $this->composerFactory]
129120
);
130121
$actualSuggestedExtensions = $composerInfo->getSuggestedPackages();
131122
$this->assertArrayHasKey('psr/log', $actualSuggestedExtensions);
@@ -143,12 +134,7 @@ public function testGetRootRequiredPackagesAndTypes($composerDir)
143134
/** @var \Magento\Framework\Composer\ComposerInformation $composerInfo */
144135
$composerInfo = $this->objectManager->create(
145136
'Magento\Framework\Composer\ComposerInformation',
146-
[
147-
'applicationFactory' => new MagentoComposerApplicationFactory(
148-
$this->composerJsonFinder,
149-
$this->directoryList
150-
)
151-
]
137+
['composerFactory' => $this->composerFactory]
152138
);
153139

154140
$requiredPackagesAndTypes = $composerInfo->getRootRequiredPackageTypesByName();
@@ -171,37 +157,14 @@ public function getRequiredPhpVersionDataProvider()
171157
];
172158
}
173159

174-
/**
175-
* @expectedException \Exception
176-
* @expectedExceptionMessage Composer file not found
177-
*/
178-
public function testNoLock()
179-
{
180-
$this->setupDirectory('notARealDirectory');
181-
$this->objectManager->create(
182-
'Magento\Framework\Composer\ComposerInformation',
183-
[
184-
'applicationFactory' => new MagentoComposerApplicationFactory(
185-
$this->composerJsonFinder,
186-
$this->directoryList
187-
)
188-
]
189-
);
190-
}
191-
192160
public function testIsPackageInComposerJson()
193161
{
194162
$this->setupDirectory('testSkeleton');
195163

196164
/** @var \Magento\Framework\Composer\ComposerInformation $composerInfo */
197165
$composerInfo = $this->objectManager->create(
198166
'Magento\Framework\Composer\ComposerInformation',
199-
[
200-
'applicationFactory' => new MagentoComposerApplicationFactory(
201-
$this->composerJsonFinder,
202-
$this->directoryList
203-
)
204-
]
167+
['composerFactory' => $this->composerFactory]
205168
);
206169

207170
$packageName = 'magento/sample-module-minimal';
@@ -222,17 +185,33 @@ public function testGetRootRepositories($composerDir)
222185
/** @var \Magento\Framework\Composer\ComposerInformation $composerInfo */
223186
$composerInfo = $this->objectManager->create(
224187
'Magento\Framework\Composer\ComposerInformation',
225-
[
226-
'applicationFactory' => new MagentoComposerApplicationFactory(
227-
$this->composerJsonFinder,
228-
$this->directoryList
229-
)
230-
]
188+
['composerFactory' => $this->composerFactory]
231189
);
232190
if ($composerDir === 'testFromCreateProject') {
233191
$this->assertEquals(['https://repo.magento.com/'], $composerInfo->getRootRepositories());
234192
} else {
235193
$this->assertEquals([], $composerInfo->getRootRepositories());
236194
}
237195
}
196+
197+
/**
198+
* @param $composerDir string Directory under _files that contains composer files
199+
*
200+
* @dataProvider getRequiredPhpVersionDataProvider
201+
*/
202+
public function testIsMagentoRoot($composerDir)
203+
{
204+
$this->setupDirectory($composerDir);
205+
206+
/** @var \Magento\Framework\Composer\ComposerInformation $composerInfo */
207+
$composerInfo = $this->objectManager->create(
208+
'Magento\Framework\Composer\ComposerInformation',
209+
['composerFactory' => $this->composerFactory]
210+
);
211+
if ($composerDir === 'testFromClone') {
212+
$this->assertTrue($composerInfo->isMagentoRoot());
213+
} else {
214+
$this->assertFalse($composerInfo->isMagentoRoot());
215+
}
216+
}
238217
}

dev/tests/integration/testsuite/Magento/Setup/Model/UpdatePackagesCacheTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\Setup\Model;
88

9+
use Magento\Framework\Composer\ComposerFactory;
910
use Magento\TestFramework\Helper\Bootstrap;
1011
use Magento\Framework\Composer\ComposerJsonFinder;
1112
use Magento\Framework\Composer\MagentoComposerApplicationFactory;
@@ -66,9 +67,9 @@ private function setupDirectory($composerDir)
6667
$this->composerInformation = $this->objectManager->create(
6768
'Magento\Framework\Composer\ComposerInformation',
6869
[
69-
'applicationFactory' => new MagentoComposerApplicationFactory(
70-
$this->composerJsonFinder,
71-
$this->directoryList
70+
'composerFactory' => new ComposerFactory(
71+
$this->directoryList,
72+
$this->composerJsonFinder
7273
)
7374
]
7475
);

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

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,25 @@
77
*/
88
namespace Magento\Framework\App;
99

10+
use Magento\Framework\Composer\ComposerFactory;
11+
use \Magento\Framework\Composer\ComposerJsonFinder;
12+
use \Magento\Framework\App\Filesystem\DirectoryList;
13+
use \Magento\Framework\Composer\ComposerInformation;
14+
15+
/**
16+
* Class ProductMetadata
17+
* @package Magento\Framework\App
18+
*/
1019
class ProductMetadata implements ProductMetadataInterface
1120
{
21+
/**
22+
* Magento product edition
23+
*/
1224
const EDITION_NAME = 'Community';
25+
26+
/**
27+
* Magento product name
28+
*/
1329
const PRODUCT_NAME = 'Magento';
1430

1531
/**
@@ -21,13 +37,19 @@ class ProductMetadata implements ProductMetadataInterface
2137

2238
/**
2339
* @var \Magento\Framework\Composer\ComposerJsonFinder
40+
* @deprecated
2441
*/
2542
protected $composerJsonFinder;
2643

2744
/**
28-
* @param \Magento\Framework\Composer\ComposerJsonFinder $composerJsonFinder
45+
* @var \Magento\Framework\Composer\ComposerInformation
2946
*/
30-
public function __construct(\Magento\Framework\Composer\ComposerJsonFinder $composerJsonFinder)
47+
private $composerInformation;
48+
49+
/**
50+
* @param ComposerJsonFinder $composerJsonFinder
51+
*/
52+
public function __construct(ComposerJsonFinder $composerJsonFinder)
3153
{
3254
$this->composerJsonFinder = $composerJsonFinder;
3355
}
@@ -36,25 +58,17 @@ public function __construct(\Magento\Framework\Composer\ComposerJsonFinder $comp
3658
* Get Product version
3759
*
3860
* @return string
39-
* @throws \Exception
4061
*/
4162
public function getVersion()
4263
{
4364
if (!$this->version) {
44-
$composerJsonFile = $this->composerJsonFinder->findComposerJson();
45-
46-
$composerContent = file_get_contents($composerJsonFile);
47-
if ($composerContent === false) {
48-
throw new \Exception('Composer file content is empty');
65+
if (!($this->version = $this->getSystemPackageVersion())) {
66+
if ($this->getComposerInformation()->isMagentoRoot()) {
67+
$this->version = $this->getComposerInformation()->getRootPackage()->getPrettyVersion();
68+
} else {
69+
$this->version = 'UNKNOWN';
70+
}
4971
}
50-
$composerContent = json_decode($composerContent, true);
51-
if (!$composerContent
52-
|| !is_array($composerContent)
53-
|| !array_key_exists('version', $composerContent)
54-
) {
55-
throw new \Exception('Unable to decode Composer file');
56-
}
57-
$this->version = $composerContent['version'];
5872
}
5973
return $this->version;
6074
}
@@ -78,4 +92,37 @@ public function getName()
7892
{
7993
return self::PRODUCT_NAME;
8094
}
95+
96+
/**
97+
* Get version from system package
98+
*
99+
* @return string
100+
* @deprecated
101+
*/
102+
private function getSystemPackageVersion()
103+
{
104+
$packages = $this->getComposerInformation()->getSystemPackages();
105+
foreach ($packages as $package) {
106+
if (isset($package['name']) && isset($package['version'])) {
107+
return $package['version'];
108+
}
109+
}
110+
return '';
111+
}
112+
113+
/**
114+
* Load composerInformation
115+
*
116+
* @return ComposerInformation
117+
* @deprecated
118+
*/
119+
private function getComposerInformation()
120+
{
121+
if (!$this->composerInformation) {
122+
$directoryList = new DirectoryList(BP);
123+
$composerFactory = new ComposerFactory($directoryList, $this->composerJsonFinder);
124+
$this->composerInformation = new ComposerInformation($composerFactory);
125+
}
126+
return $this->composerInformation;
127+
}
81128
}

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

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,67 @@
55
*/
66
namespace Magento\Framework\App\Test\Unit;
77

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

1011
class ProductMetadataTest extends \PHPUnit_Framework_TestCase
1112
{
1213
/**
13-
* @var \Magento\Framework\App\ProductMetadata
14+
* @var ProductMetadata
1415
*/
15-
protected $productMetadata;
16+
private $productMetadata;
17+
18+
/**
19+
* @var \Magento\Framework\Composer\ComposerInformation|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
private $composerInformationMock;
1622

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

2428
$objectManager = new ObjectManager($this);
25-
$this->productMetadata = $objectManager->getObject(
26-
'Magento\Framework\App\ProductMetadata',
27-
['composerJsonFinder' => $composerJsonFinder]
28-
);
29+
$this->productMetadata = $objectManager->getObject(ProductMetadata::class);
30+
$reflectionProperty = new \ReflectionProperty($this->productMetadata, 'composerInformation');
31+
$reflectionProperty->setAccessible(true);
32+
$reflectionProperty->setValue($this->productMetadata, $this->composerInformationMock);
2933
}
3034

31-
public function testGetVersion()
35+
/**
36+
* @param array $packageList
37+
* @param string $expectedVersion
38+
* @dataProvider testGetVersionGitInstallationDataProvider
39+
*/
40+
public function testGetVersion($packageList, $expectedVersion)
3241
{
42+
$this->composerInformationMock->expects($this->any())->method('getSystemPackages')->willReturn($packageList);
3343
$productVersion = $this->productMetadata->getVersion();
3444
$this->assertNotEmpty($productVersion, 'Empty product version');
35-
preg_match('/^([0-9\.]+)/', $productVersion, $matches);
36-
$this->assertArrayHasKey(1, $matches, 'Invalid product version');
37-
$this->assertNotEmpty($matches, 'Empty product version');
45+
$this->assertEquals($expectedVersion, $productVersion);
46+
}
47+
48+
public function testGetVersionGitInstallationDataProvider()
49+
{
50+
return [
51+
[
52+
[
53+
0 => [
54+
'name' => 'magento/product-community-edition',
55+
'version' => '123.456.789'
56+
],
57+
1 => [
58+
'name' => 'magento/product-other-edition',
59+
'version' => '987.654.321'
60+
],
61+
],
62+
'123.456.789'
63+
],
64+
[
65+
[],
66+
'UNKNOWN'
67+
]
68+
];
3869
}
3970

4071
public function testGetEdition()

0 commit comments

Comments
 (0)