Skip to content

Commit 073f227

Browse files
authored
Merge pull request #493 from magento-falcons/MAGETWO-59433
Bugs: * MAGETWO-59256: [GitHub] Custom composer modules break Component Manager #6718
2 parents 54882fe + a817f97 commit 073f227

File tree

2 files changed

+186
-58
lines changed

2 files changed

+186
-58
lines changed

setup/src/Magento/Setup/Model/PackagesData.php

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ function ($item) {
409409
return in_array(
410410
$item['package_type'],
411411
[
412+
\Magento\Setup\Model\Grid\TypeMapper::LANGUAGE_PACKAGE_TYPE,
412413
\Magento\Setup\Model\Grid\TypeMapper::MODULE_PACKAGE_TYPE,
413414
\Magento\Setup\Model\Grid\TypeMapper::EXTENSION_PACKAGE_TYPE,
414415
\Magento\Setup\Model\Grid\TypeMapper::THEME_PACKAGE_TYPE,
@@ -491,26 +492,38 @@ private function getPackageAvailableVersions($package)
491492

492493
return array_keys($packageVersions);
493494
}
494-
} else {
495-
$versionsPattern = '/^versions\s*\:\s(.+)$/m';
496-
497-
$commandParams = [
498-
self::PARAM_COMMAND => self::COMPOSER_SHOW,
499-
self::PARAM_PACKAGE => $package,
500-
self::PARAM_AVAILABLE => true
501-
];
502-
503-
$applicationFactory = $this->objectManagerProvider->get()
504-
->get(\Magento\Framework\Composer\MagentoComposerApplicationFactory::class);
505-
/** @var \Magento\Composer\MagentoComposerApplication $application */
506-
$application = $applicationFactory->create();
507-
508-
$result = $application->runComposerCommand($commandParams);
509-
$matches = [];
510-
preg_match($versionsPattern, $result, $matches);
511-
if (isset($matches[1])) {
512-
return explode(', ', $matches[1]);
513-
}
495+
}
496+
497+
return $this->getAvailableVersionsFromAllRepositories($package);
498+
}
499+
500+
/**
501+
* Get available versions of package by "composer show" command
502+
*
503+
* @param string $package
504+
* @return array
505+
* @exception \RuntimeException
506+
*/
507+
private function getAvailableVersionsFromAllRepositories($package)
508+
{
509+
$versionsPattern = '/^versions\s*\:\s(.+)$/m';
510+
511+
$commandParams = [
512+
self::PARAM_COMMAND => self::COMPOSER_SHOW,
513+
self::PARAM_PACKAGE => $package,
514+
self::PARAM_AVAILABLE => true
515+
];
516+
517+
$applicationFactory = $this->objectManagerProvider->get()
518+
->get(\Magento\Framework\Composer\MagentoComposerApplicationFactory::class);
519+
/** @var \Magento\Composer\MagentoComposerApplication $application */
520+
$application = $applicationFactory->create();
521+
522+
$result = $application->runComposerCommand($commandParams);
523+
$matches = [];
524+
preg_match($versionsPattern, $result, $matches);
525+
if (isset($matches[1])) {
526+
return explode(', ', $matches[1]);
514527
}
515528

516529
throw new \RuntimeException(

setup/src/Magento/Setup/Test/Unit/Model/PackagesDataTest.php

Lines changed: 153 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,96 @@ class PackagesDataTest extends \PHPUnit_Framework_TestCase
2222
*/
2323
private $packagesData;
2424

25+
/**
26+
* @var ComposerInformation|MockObject
27+
*/
28+
private $composerInformation;
29+
30+
/**
31+
* @var \Magento\Setup\Model\DateTime\TimeZoneProvider|MockObject
32+
*/
33+
private $timeZoneProvider;
34+
35+
/**
36+
* @var \Magento\Setup\Model\PackagesAuth|MockObject
37+
*/
38+
private $packagesAuth;
39+
40+
/**
41+
* @var \Magento\Framework\Filesystem|MockObject
42+
*/
43+
private $filesystem;
44+
45+
/**
46+
* @var \Magento\Setup\Model\ObjectManagerProvider|MockObject
47+
*/
48+
private $objectManagerProvider;
49+
50+
/**
51+
* @var \Magento\Setup\Model\Grid\TypeMapper|MockObject
52+
*/
53+
private $typeMapper;
54+
2555
public function setUp()
2656
{
27-
$composerInformation = $this->getComposerInformation();
28-
$timeZoneProvider = $this->getMock(\Magento\Setup\Model\DateTime\TimeZoneProvider::class, [], [], '', false);
57+
$this->composerInformation = $this->getComposerInformation();
58+
$this->timeZoneProvider = $this->getMockBuilder(\Magento\Setup\Model\DateTime\TimeZoneProvider::class)
59+
->disableOriginalConstructor()
60+
->getMock();
2961
$timeZone = $this->getMock(\Magento\Framework\Stdlib\DateTime\Timezone::class, [], [], '', false);
30-
$timeZoneProvider->expects($this->any())->method('get')->willReturn($timeZone);
31-
$packagesAuth = $this->getMock(\Magento\Setup\Model\PackagesAuth::class, [], [], '', false);
32-
$filesystem = $this->getMock(\Magento\Framework\Filesystem::class, [], [], '', false);
33-
$objectManagerProvider = $this->getMock(\Magento\Setup\Model\ObjectManagerProvider::class, [], [], '', false);
62+
$this->timeZoneProvider->expects($this->any())->method('get')->willReturn($timeZone);
63+
$this->packagesAuth = $this->getMock(\Magento\Setup\Model\PackagesAuth::class, [], [], '', false);
64+
$this->filesystem = $this->getMock(\Magento\Framework\Filesystem::class, [], [], '', false);
65+
$this->objectManagerProvider = $this->getMockBuilder(\Magento\Setup\Model\ObjectManagerProvider::class)
66+
->disableOriginalConstructor()
67+
->getMock();
3468
$objectManager = $this->getMockForAbstractClass(\Magento\Framework\ObjectManagerInterface::class);
35-
$applicationFactory = $this->getMock(
36-
\Magento\Framework\Composer\MagentoComposerApplicationFactory::class,
37-
[],
38-
[],
39-
'',
40-
false
41-
);
69+
$appFactory = $this->getMockBuilder(\Magento\Framework\Composer\MagentoComposerApplicationFactory::class)
70+
->disableOriginalConstructor()
71+
->getMock();
4272
$application = $this->getMock(\Magento\Composer\MagentoComposerApplication::class, [], [], '', false);
4373
$application->expects($this->any())
4474
->method('runComposerCommand')
45-
->willReturn('versions: 2.0.1');
46-
$applicationFactory->expects($this->any())->method('create')->willReturn($application);
75+
->willReturnMap([
76+
[
77+
[
78+
PackagesData::PARAM_COMMAND => PackagesData::COMPOSER_SHOW,
79+
PackagesData::PARAM_PACKAGE => 'magento/package-1',
80+
PackagesData::PARAM_AVAILABLE => true,
81+
],
82+
null,
83+
'versions: 2.0.1'
84+
],
85+
[
86+
[
87+
PackagesData::PARAM_COMMAND => PackagesData::COMPOSER_SHOW,
88+
PackagesData::PARAM_PACKAGE => 'magento/package-2',
89+
PackagesData::PARAM_AVAILABLE => true,
90+
],
91+
null,
92+
'versions: 2.0.1'
93+
],
94+
[
95+
[
96+
PackagesData::PARAM_COMMAND => PackagesData::COMPOSER_SHOW,
97+
PackagesData::PARAM_PACKAGE => 'partner/package-3',
98+
PackagesData::PARAM_AVAILABLE => true,
99+
],
100+
null,
101+
'versions: 3.0.1'
102+
],
103+
]);
104+
$appFactory->expects($this->any())->method('create')->willReturn($application);
47105
$objectManager->expects($this->any())
48106
->method('get')
49107
->with(\Magento\Framework\Composer\MagentoComposerApplicationFactory::class)
50-
->willReturn($applicationFactory);
51-
$objectManagerProvider->expects($this->any())->method('get')->willReturn($objectManager);
108+
->willReturn($appFactory);
109+
$this->objectManagerProvider->expects($this->any())->method('get')->willReturn($objectManager);
52110

53111
$directoryWrite = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
54112
$directoryRead = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
55-
$filesystem->expects($this->any())->method('getDirectoryRead')->will($this->returnValue($directoryRead));
56-
$filesystem->expects($this->any())
113+
$this->filesystem->expects($this->any())->method('getDirectoryRead')->will($this->returnValue($directoryRead));
114+
$this->filesystem->expects($this->any())
57115
->method('getDirectoryWrite')
58116
->will($this->returnValue($directoryWrite));
59117
$directoryWrite->expects($this->any())->method('isExist')->willReturn(true);
@@ -81,32 +139,41 @@ public function setUp()
81139
. '}}}'
82140
);
83141

84-
$typeMapper = $this->getMockBuilder(\Magento\Setup\Model\Grid\TypeMapper::class)
142+
$this->typeMapper = $this->getMockBuilder(\Magento\Setup\Model\Grid\TypeMapper::class)
85143
->disableOriginalConstructor()
86144
->getMock();
87-
$typeMapper->expects(static::any())
145+
$this->typeMapper->expects(static::any())
88146
->method('map')
89147
->willReturnMap([
90148
[ComposerInformation::MODULE_PACKAGE_TYPE, \Magento\Setup\Model\Grid\TypeMapper::MODULE_PACKAGE_TYPE],
91149
]);
92150

151+
$this->createPackagesData();
152+
}
153+
154+
private function createPackagesData()
155+
{
93156
$this->packagesData = new PackagesData(
94-
$composerInformation,
95-
$timeZoneProvider,
96-
$packagesAuth,
97-
$filesystem,
98-
$objectManagerProvider,
99-
$typeMapper
157+
$this->composerInformation,
158+
$this->timeZoneProvider,
159+
$this->packagesAuth,
160+
$this->filesystem,
161+
$this->objectManagerProvider,
162+
$this->typeMapper
100163
);
101164
}
102165

103166
/**
167+
* @param array $requiredPackages
168+
* @param array $installedPackages
169+
* @param array $repo
104170
* @return ComposerInformation|MockObject
105171
*/
106-
private function getComposerInformation()
172+
private function getComposerInformation($requiredPackages = [], $installedPackages = [], $repo = [])
107173
{
108174
$composerInformation = $this->getMock(ComposerInformation::class, [], [], '', false);
109175
$composerInformation->expects($this->any())->method('getInstalledMagentoPackages')->willReturn(
176+
$installedPackages ?:
110177
[
111178
'magento/package-1' => [
112179
'name' => 'magento/package-1',
@@ -117,21 +184,30 @@ private function getComposerInformation()
117184
'name' => 'magento/package-2',
118185
'type' => 'magento2-module',
119186
'version'=> '1.0.1'
120-
]
187+
],
188+
'partner/package-3' => [
189+
'name' => 'partner/package-3',
190+
'type' => 'magento2-module',
191+
'version'=> '3.0.0'
192+
],
121193
]
122194
);
123195

124196
$composerInformation->expects($this->any())->method('getRootRepositories')
125-
->willReturn(['repo1', 'repo2']);
197+
->willReturn($repo ?: ['repo1', 'repo2']);
126198
$composerInformation->expects($this->any())->method('getPackagesTypes')
127199
->willReturn(['magento2-module']);
128200
$rootPackage = $this->getMock(RootPackage::class, [], ['magento/project', '2.1.0', '2']);
129201
$rootPackage->expects($this->any())
130202
->method('getRequires')
131-
->willReturn([
132-
'magento/package-1' => '1.0.0',
133-
'magento/package-2' => '1.0.1'
134-
]);
203+
->willReturn(
204+
$requiredPackages ?:
205+
[
206+
'magento/package-1' => '1.0.0',
207+
'magento/package-2' => '1.0.1',
208+
'partner/package-3' => '3.0.0',
209+
]
210+
);
135211
$composerInformation->expects($this->any())
136212
->method('getRootPackage')
137213
->willReturn($rootPackage);
@@ -146,19 +222,57 @@ public function testSyncPackagesData()
146222
$this->assertArrayHasKey('date', $latestData['lastSyncDate']);
147223
$this->assertArrayHasKey('time', $latestData['lastSyncDate']);
148224
$this->assertArrayHasKey('packages', $latestData);
149-
$this->assertSame(2, count($latestData['packages']));
150-
$this->assertSame(2, $latestData['countOfUpdate']);
225+
$this->assertSame(3, count($latestData['packages']));
226+
$this->assertSame(3, $latestData['countOfUpdate']);
151227
$this->assertArrayHasKey('installPackages', $latestData);
152228
$this->assertSame(1, count($latestData['installPackages']));
153229
$this->assertSame(1, $latestData['countOfInstall']);
154230
}
155231

156-
public function testGetPackagesForUpdate()
232+
/**
233+
* @expectedException \RuntimeException
234+
* @expectedExceptionMessage Couldn't get available versions for package partner/package-4
235+
*/
236+
public function testGetPackagesForUpdateWithException()
157237
{
238+
$requiredPackages = [
239+
'partner/package-4' => '4.0.4',
240+
];
241+
$installedPackages = [
242+
'partner/package-4' => [
243+
'name' => 'partner/package-4',
244+
'type' => 'magento2-module',
245+
'version'=> '4.0.4'
246+
],
247+
];
248+
$this->composerInformation = $this->getComposerInformation($requiredPackages, $installedPackages);
249+
$this->createPackagesData();
250+
$this->packagesData->getPackagesForUpdate();
251+
}
252+
253+
public function testPackagesForUpdateFromJson()
254+
{
255+
$this->composerInformation = $this->getComposerInformation([], [], ['https://repo1']);
256+
$this->packagesAuth->expects($this->atLeastOnce())
257+
->method('getCredentialBaseUrl')
258+
->willReturn('repo1');
259+
$this->createPackagesData();
158260
$packages = $this->packagesData->getPackagesForUpdate();
159261
$this->assertEquals(2, count($packages));
160262
$this->assertArrayHasKey('magento/package-1', $packages);
263+
$this->assertArrayHasKey('partner/package-3', $packages);
264+
$firstPackage = array_values($packages)[0];
265+
$this->assertArrayHasKey('latestVersion', $firstPackage);
266+
$this->assertArrayHasKey('versions', $firstPackage);
267+
}
268+
269+
public function testGetPackagesForUpdate()
270+
{
271+
$packages = $this->packagesData->getPackagesForUpdate();
272+
$this->assertEquals(3, count($packages));
273+
$this->assertArrayHasKey('magento/package-1', $packages);
161274
$this->assertArrayHasKey('magento/package-2', $packages);
275+
$this->assertArrayHasKey('partner/package-3', $packages);
162276
$firstPackage = array_values($packages)[0];
163277
$this->assertArrayHasKey('latestVersion', $firstPackage);
164278
$this->assertArrayHasKey('versions', $firstPackage);
@@ -167,9 +281,10 @@ public function testGetPackagesForUpdate()
167281
public function testGetInstalledPackages()
168282
{
169283
$installedPackages = $this->packagesData->getInstalledPackages();
170-
$this->assertEquals(2, count($installedPackages));
284+
$this->assertEquals(3, count($installedPackages));
171285
$this->assertArrayHasKey('magento/package-1', $installedPackages);
172286
$this->assertArrayHasKey('magento/package-2', $installedPackages);
287+
$this->assertArrayHasKey('partner/package-3', $installedPackages);
173288
}
174289

175290
public function testGetMetaPackagesMap()

0 commit comments

Comments
 (0)