@@ -21,6 +21,11 @@ class ComposerTest extends \PHPUnit\Framework\TestCase
21
21
*/
22
22
private static $ root ;
23
23
24
+ /**
25
+ * @var array
26
+ */
27
+ private static $ mainComposerModules ;
28
+
24
29
/**
25
30
* @var \stdClass
26
31
*/
@@ -40,6 +45,11 @@ public static function setUpBeforeClass()
40
45
{
41
46
self ::$ root = BP ;
42
47
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
+ }
43
53
self ::$ dependencies = [];
44
54
self ::$ objectManager = Bootstrap::create (BP , $ _SERVER )->getObjectManager ();
45
55
}
@@ -175,6 +185,8 @@ private function assertMagentoConventions($dir, $packageType, \StdClass $json)
175
185
default :
176
186
throw new \InvalidArgumentException ("Unknown package type {$ packageType }" );
177
187
}
188
+
189
+ $ this ->assertPackageVersions ($ json );
178
190
}
179
191
180
192
/**
@@ -291,28 +303,11 @@ private function assertRequireInSync(\StdClass $json)
291
303
// Magento Composer Installer is not needed for already existing components
292
304
continue ;
293
305
}
294
- if (!isset (self ::$ rootJson ['require-dev ' ][$ depName ]) && !isset (self ::$ rootJson ['require ' ][$ depName ])
295
- && !isset (self ::$ rootJson ['replace ' ][$ depName ])) {
306
+ if (!isset (self ::$ mainComposerModules [$ depName ])) {
296
307
$ errors [] = "' $ name' depends on ' $ depName' " ;
297
- } else {
298
- if (isset (self ::$ rootJson ['require-dev ' ][$ depName ])
299
- && $ this ->checkDiscrepancy ($ json , $ depName , 'require-dev ' )) {
300
- $ errors [] = "root composer.json has dependency ' "
301
- . $ depName . ": " . self ::$ rootJson ['require-dev ' ][$ depName ] . " BUT "
302
- . "' $ name' composer.json has dependency ' $ depName: {$ json ->require ->$ depName }' " ;
303
- } elseif (isset (self ::$ rootJson ['require ' ][$ depName ])
304
- && $ this ->checkDiscrepancy ($ json , $ depName , 'require ' )) {
305
- $ errors [] = "root composer.json has dependency ' "
306
- . $ depName . ": " . self ::$ rootJson ['require ' ][$ depName ] . " BUT "
307
- . "' $ name' composer.json has dependency ' $ depName: {$ json ->require ->$ depName }' " ;
308
- } elseif (isset (self ::$ rootJson ['replace ' ][$ depName ])
309
- && $ this ->checkDiscrepancy ($ json , $ depName , 'replace ' )) {
310
- $ errors [] = "root composer.json has dependency ' "
311
- . $ depName . ": " . self ::$ rootJson ['replace ' ][$ depName ] . " BUT "
312
- . "' $ name' composer.json has dependency ' $ depName: {$ json ->require ->$ depName }' " ;
313
- }
314
308
}
315
309
}
310
+
316
311
if (!empty ($ errors )) {
317
312
$ this ->fail (
318
313
"The following dependencies are missing in root 'composer.json', "
@@ -326,15 +321,47 @@ private function assertRequireInSync(\StdClass $json)
326
321
}
327
322
}
328
323
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
+
329
357
/**
330
358
* @param $componentConfig
331
359
* @param $packageName
332
- * @param $section
333
360
* @return bool
334
361
*/
335
- private function checkDiscrepancy ($ componentConfig , $ packageName, $ section )
362
+ private function checkDiscrepancy ($ componentConfig , $ packageName )
336
363
{
337
- $ rootConstraint = (new VersionParser ())->parseConstraints (self ::$ rootJson [ $ section ] [$ packageName ]);
364
+ $ rootConstraint = (new VersionParser ())->parseConstraints (self ::$ mainComposerModules [$ packageName ]);
338
365
$ componentConstraint = (new VersionParser ())->parseConstraints ($ componentConfig ->require ->$ packageName );
339
366
340
367
return !$ rootConstraint ->matches ($ componentConstraint );
0 commit comments