@@ -41,6 +41,16 @@ class ComposerTest extends \PHPUnit\Framework\TestCase
41
41
*/
42
42
private static $ objectManager ;
43
43
44
+ /**
45
+ * @var string[]
46
+ */
47
+ private static $ rootComposerModuleBlacklist = [];
48
+
49
+ /**
50
+ * @var string[]
51
+ */
52
+ private static $ moduleNameBlacklist ;
53
+
44
54
public static function setUpBeforeClass ()
45
55
{
46
56
self ::$ root = BP ;
@@ -52,6 +62,26 @@ public static function setUpBeforeClass()
52
62
}
53
63
self ::$ dependencies = [];
54
64
self ::$ objectManager = Bootstrap::create (BP , $ _SERVER )->getObjectManager ();
65
+ // A block can be whitelisted and thus not be required to be public
66
+ self ::$ rootComposerModuleBlacklist = self ::getBlacklist (
67
+ __DIR__ . '/_files/blacklist/composer_root_modules*.txt '
68
+ );
69
+ self ::$ moduleNameBlacklist = self ::getBlacklist (__DIR__ . '/_files/blacklist/composer_module_names*.txt ' );
70
+ }
71
+
72
+ /**
73
+ * Return aggregated blacklist
74
+ *
75
+ * @param string $pattern
76
+ * @return string[]
77
+ */
78
+ public static function getBlacklist (string $ pattern )
79
+ {
80
+ $ blacklist = [];
81
+ foreach (glob ($ pattern ) as $ list ) {
82
+ $ blacklist = array_merge ($ blacklist , file ($ list , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ));
83
+ }
84
+ return $ blacklist ;
55
85
}
56
86
57
87
public function testValidComposerJson ()
@@ -248,12 +278,15 @@ private function assertNoMap(\StdClass $json)
248
278
*/
249
279
private function assertConsistentModuleName (\SimpleXMLElement $ xml , $ packageName )
250
280
{
251
- $ moduleName = (string )$ xml ->module ->attributes ()->name ;
252
- $ this ->assertEquals (
253
- $ packageName ,
254
- $ this ->convertModuleToPackageName ($ moduleName ),
255
- "For the module ' {$ moduleName }', the expected package name is ' {$ packageName }' "
256
- );
281
+ if (!in_array ($ packageName , self ::$ moduleNameBlacklist )) {
282
+ $ moduleName = (string )$ xml ->module ->attributes ()->name ;
283
+ $ expectedPackageName = $ this ->convertModuleToPackageName ($ moduleName );
284
+ $ this ->assertEquals (
285
+ $ expectedPackageName ,
286
+ $ packageName ,
287
+ "For the module ' {$ moduleName }', the expected package name is ' {$ expectedPackageName }' "
288
+ );
289
+ }
257
290
}
258
291
259
292
/**
@@ -314,36 +347,49 @@ private function assertPhpVersionInSync($name, $phpVersion)
314
347
* Make sure requirements of components are reflected in root composer.json
315
348
*
316
349
* @param \StdClass $json
350
+ * @return void
317
351
*/
318
352
private function assertRequireInSync (\StdClass $ json )
319
353
{
320
- $ name = $ json ->name ;
321
354
if (preg_match ('/magento\/project-*/ ' , self ::$ rootJson ['name ' ]) == 1 ) {
322
355
return ;
323
356
}
324
- if (isset ($ json ->require )) {
325
- $ errors = [];
326
- foreach (array_keys ((array )$ json ->require ) as $ depName ) {
327
- if ($ depName == 'magento/magento-composer-installer ' ) {
328
- // Magento Composer Installer is not needed for already existing components
329
- continue ;
330
- }
331
- if (!isset (self ::$ mainComposerModules [$ depName ])) {
332
- $ errors [] = "' $ name' depends on ' $ depName' " ;
333
- }
357
+ if (!in_array ($ json ->name , self ::$ rootComposerModuleBlacklist ) && isset ($ json ->require )) {
358
+ $ this ->checkPackageInRootComposer ($ json );
359
+ }
360
+ }
361
+
362
+ /**
363
+ * Check if package is reflected in root composer.json
364
+ *
365
+ * @param \StdClass $json
366
+ * @return void
367
+ */
368
+ private function checkPackageInRootComposer (\StdClass $ json )
369
+ {
370
+ $ name = $ json ->name ;
371
+ $ errors = [];
372
+ foreach (array_keys ((array )$ json ->require ) as $ depName ) {
373
+ if ($ depName == 'magento/magento-composer-installer ' ) {
374
+ // Magento Composer Installer is not needed for already existing components
375
+ continue ;
334
376
}
335
377
336
- if (!empty ($ errors )) {
337
- $ this ->fail (
338
- "The following dependencies are missing in root 'composer.json', "
339
- . " while declared in child components. \n"
340
- . "Consider adding them to 'require-dev' section (if needed for child components only), "
341
- . " to 'replace' section (if they are present in the project), "
342
- . " to 'require' section (if needed for the skeleton). \n"
343
- . join ("\n" , $ errors )
344
- );
378
+ if (!isset (self ::$ rootJson ['require-dev ' ][$ depName ]) && !isset (self ::$ rootJson ['require ' ][$ depName ])
379
+ && !isset (self ::$ rootJson ['replace ' ][$ depName ])) {
380
+ $ errors [] = "' $ name' depends on ' $ depName' " ;
345
381
}
346
382
}
383
+ if (!empty ($ errors )) {
384
+ $ this ->fail (
385
+ "The following dependencies are missing in root 'composer.json', "
386
+ . " while declared in child components. \n"
387
+ . "Consider adding them to 'require-dev' section (if needed for child components only), "
388
+ . " to 'replace' section (if they are present in the project), "
389
+ . " to 'require' section (if needed for the skeleton). \n"
390
+ . join ("\n" , $ errors )
391
+ );
392
+ }
347
393
}
348
394
349
395
/**
@@ -386,6 +432,10 @@ private function assertPackageVersions(\StdClass $json)
386
432
*/
387
433
private function checkDiscrepancy ($ componentConfig , $ packageName )
388
434
{
435
+ if (in_array ($ packageName , self ::$ rootComposerModuleBlacklist )) {
436
+ return false ;
437
+ }
438
+
389
439
$ rootConstraint = (new VersionParser ())->parseConstraints (self ::$ mainComposerModules [$ packageName ]);
390
440
$ componentConstraint = (new VersionParser ())->parseConstraints ($ componentConfig ->require ->$ packageName );
391
441
@@ -484,7 +534,11 @@ private function checkProject()
484
534
}
485
535
}
486
536
sort ($ dependenciesListed );
487
- $ nonDeclaredDependencies = array_diff (self ::$ dependencies , $ dependenciesListed );
537
+ $ nonDeclaredDependencies = array_diff (
538
+ self ::$ dependencies ,
539
+ $ dependenciesListed ,
540
+ self ::$ rootComposerModuleBlacklist
541
+ );
488
542
$ nonexistentDependencies = array_diff ($ dependenciesListed , self ::$ dependencies );
489
543
$ this ->assertEmpty (
490
544
$ nonDeclaredDependencies ,
0 commit comments