Skip to content

Commit 9e77c0d

Browse files
committed
Merge branch 'MAGETWO-83834' of https://github.com/magento-helix/magento2ce into pr-tests
2 parents 9c88e66 + 4816276 commit 9e77c0d

File tree

1 file changed

+61
-2
lines changed

1 file changed

+61
-2
lines changed

dev/tests/static/testsuite/Magento/Test/Integrity/ComposerTest.php

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Test\Integrity;
77

8+
use Composer\Semver\VersionParser;
89
use Magento\Framework\App\Bootstrap;
910
use Magento\Framework\Component\ComponentRegistrar;
1011
use Magento\Framework\Composer\MagentoComponent;
@@ -20,6 +21,11 @@ class ComposerTest extends \PHPUnit\Framework\TestCase
2021
*/
2122
private static $root;
2223

24+
/**
25+
* @var array
26+
*/
27+
private static $mainComposerModules;
28+
2329
/**
2430
* @var \stdClass
2531
*/
@@ -39,6 +45,11 @@ public static function setUpBeforeClass()
3945
{
4046
self::$root = BP;
4147
self::$rootJson = json_decode(file_get_contents(self::$root . '/composer.json'), true);
48+
$availableSections = ['require', 'require-dev', 'replace'];
49+
self::$mainComposerModules = [];
50+
foreach ($availableSections as $availableSection) {
51+
self::$mainComposerModules = array_merge(self::$mainComposerModules, self::$rootJson[$availableSection]);
52+
}
4253
self::$dependencies = [];
4354
self::$objectManager = Bootstrap::create(BP, $_SERVER)->getObjectManager();
4455
}
@@ -174,6 +185,8 @@ private function assertMagentoConventions($dir, $packageType, \StdClass $json)
174185
default:
175186
throw new \InvalidArgumentException("Unknown package type {$packageType}");
176187
}
188+
189+
$this->assertPackageVersions($json);
177190
}
178191

179192
/**
@@ -290,11 +303,11 @@ private function assertRequireInSync(\StdClass $json)
290303
// Magento Composer Installer is not needed for already existing components
291304
continue;
292305
}
293-
if (!isset(self::$rootJson['require-dev'][$depName]) && !isset(self::$rootJson['require'][$depName])
294-
&& !isset(self::$rootJson['replace'][$depName])) {
306+
if (!isset(self::$mainComposerModules[$depName])) {
295307
$errors[] = "'$name' depends on '$depName'";
296308
}
297309
}
310+
298311
if (!empty($errors)) {
299312
$this->fail(
300313
"The following dependencies are missing in root 'composer.json',"
@@ -308,6 +321,52 @@ private function assertRequireInSync(\StdClass $json)
308321
}
309322
}
310323

324+
/**
325+
*
326+
*
327+
* @param \StdClass $json
328+
*/
329+
private function assertPackageVersions(\StdClass $json)
330+
{
331+
$name = $json->name;
332+
if (preg_match('/magento\/project-*/', self::$rootJson['name']) == 1) {
333+
return;
334+
}
335+
if (isset($json->require)) {
336+
$errors = [];
337+
$errorTemplate = "root composer.json has dependency '%s:%s' BUT '%s' composer.json has dependency '%s:%s'";
338+
foreach (array_keys((array)$json->require) as $depName) {
339+
if ($this->checkDiscrepancy($json, $depName)) {
340+
$errors[] = sprintf(
341+
$errorTemplate,
342+
$depName,
343+
self::$mainComposerModules[$depName],
344+
$name,
345+
$depName,
346+
$json->require->$depName
347+
);
348+
}
349+
}
350+
351+
if (!empty($errors)) {
352+
$this->fail(join("\n", $errors));
353+
}
354+
}
355+
}
356+
357+
/**
358+
* @param $componentConfig
359+
* @param $packageName
360+
* @return bool
361+
*/
362+
private function checkDiscrepancy($componentConfig, $packageName)
363+
{
364+
$rootConstraint = (new VersionParser())->parseConstraints(self::$mainComposerModules[$packageName]);
365+
$componentConstraint = (new VersionParser())->parseConstraints($componentConfig->require->$packageName);
366+
367+
return !$rootConstraint->matches($componentConstraint);
368+
}
369+
311370
/**
312371
* Convert a fully qualified module name to a composer package name according to conventions
313372
*

0 commit comments

Comments
 (0)