Skip to content

Commit aa5b19e

Browse files
committed
MC-5421: Create test to check dependencies between modules in Declarative Schema
1 parent 373fd25 commit aa5b19e

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

dev/tests/static/framework/Magento/TestFramework/Dependency/DiRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ private function getPattern()
7777
*/
7878
public function getDependencyInfo($currentModule, $fileType, $file, &$contents)
7979
{
80+
//phpcs:ignore Magento2.Functions.DiscouragedFunction
8081
if (pathinfo($file, PATHINFO_BASENAME) !== 'di.xml') {
8182
return [];
8283
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function ($file) {
8888
$result[] = $this->getErrorMessage($name) . "\n" . implode("\t\n", $modules) . "\n";
8989
}
9090
}
91-
if (count($result)) {
91+
if (!empty($result)) {
9292
$this->fail(
9393
'Module ' . $moduleName . ' has undeclared dependencies: ' . "\n" . implode("\t\n", $result)
9494
);
@@ -170,6 +170,7 @@ private function readJsonFile(string $file, bool $asArray = false)
170170
{
171171
$decodedJson = json_decode(file_get_contents($file), $asArray);
172172
if (null == $decodedJson) {
173+
//phpcs:ignore Magento2.Exceptions.DirectThrow
173174
throw new \Exception("Invalid Json: $file");
174175
}
175176

dev/tests/static/testsuite/Magento/Test/Integrity/Dependency/DeclarativeSchemaDependencyProvider.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ private function readJsonFile(string $file, bool $asArray = false)
174174
{
175175
$decodedJson = json_decode(file_get_contents($file), $asArray);
176176
if (null == $decodedJson) {
177+
//phpcs:ignore Magento2.Exceptions.DirectThrow
177178
throw new \Exception("Invalid Json: $file");
178179
}
179180

@@ -304,10 +305,8 @@ private function resolveEntityDependencies(string $tableName, string $entityType
304305
case self::SCHEMA_ENTITY_INDEX:
305306
return $this->getDeclarativeSchema()
306307
[self::SCHEMA_ENTITY_TABLE][$tableName][$entityType][$entityName]['modules'];
307-
break;
308308
case self::SCHEMA_ENTITY_TABLE:
309309
return $this->getDeclarativeSchema()[self::SCHEMA_ENTITY_TABLE][$tableName]['modules'];
310-
break;
311310
default:
312311
return [];
313312
}
@@ -593,7 +592,7 @@ public static function decodeDependencyId(string $id): array
593592
*/
594593
private function collectDependencies($currentModuleName, $dependencies = [])
595594
{
596-
if (!count($dependencies)) {
595+
if (empty($dependencies)) {
597596
return [];
598597
}
599598
foreach ($dependencies as $dependencyName => $dependency) {

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ protected static function _initThemes()
221221
$defaultThemes = [];
222222
foreach (self::$_listConfigXml as $file) {
223223
$config = simplexml_load_file($file);
224+
//phpcs:ignore Generic.PHP.NoSilencedErrors
224225
$nodes = @($config->xpath("/config/*/design/theme/full_name") ?: []);
225226
foreach ($nodes as $node) {
226227
$defaultThemes[] = (string)$node;
@@ -237,6 +238,7 @@ protected static function _initRules()
237238
$replaceFilePattern = str_replace('\\', '/', realpath(__DIR__)) . '/_files/dependency_test/tables_*.php';
238239
$dbRuleTables = [];
239240
foreach (glob($replaceFilePattern) as $fileName) {
241+
//phpcs:ignore Generic.PHP.NoSilencedErrors
240242
$dbRuleTables = array_merge($dbRuleTables, @include $fileName);
241243
}
242244
self::$_rulesInstances = [
@@ -339,12 +341,12 @@ function ($fileType, $file) {
339341
$result = [];
340342
foreach ($undeclaredDependency as $type => $modules) {
341343
$modules = array_unique($modules);
342-
if (!count($modules)) {
344+
if (empty($modules)) {
343345
continue;
344346
}
345347
$result[] = sprintf("%s [%s]", $type, implode(', ', $modules));
346348
}
347-
if (count($result)) {
349+
if (!empty($result)) {
348350
$this->fail('Module ' . $module . ' has undeclared dependencies: ' . implode(', ', $result));
349351
}
350352
},
@@ -473,7 +475,7 @@ public function testRedundant()
473475
foreach (array_keys(self::$mapDependencies) as $module) {
474476
$result = [];
475477
$redundant = $this->_getDependencies($module, self::TYPE_HARD, self::MAP_TYPE_REDUNDANT);
476-
if (count($redundant)) {
478+
if (!empty($redundant)) {
477479
$result[] = sprintf(
478480
"\r\nModule %s: %s [%s]",
479481
$module,
@@ -482,11 +484,11 @@ public function testRedundant()
482484
);
483485
}
484486

485-
if (count($result)) {
487+
if (!empty($result)) {
486488
$output[] = implode(', ', $result);
487489
}
488490
}
489-
if (count($output)) {
491+
if (!empty($output)) {
490492
$this->fail("Redundant dependencies found!\r\n" . implode(' ', $output));
491493
}
492494
}
@@ -654,7 +656,7 @@ protected static function _prepareMapLayoutBlocks()
654656
$area = 'default';
655657
if (preg_match('/[\/](?<area>adminhtml|frontend)[\/]/', $file, $matches)) {
656658
$area = $matches['area'];
657-
self::$_mapLayoutBlocks[$area] = @(self::$_mapLayoutBlocks[$area] ?: []);
659+
self::$_mapLayoutBlocks[$area] = self::$_mapLayoutBlocks[$area] ?? [];
658660
}
659661
if (preg_match('/(?<namespace>[A-Z][a-z]+)[_\/\\\\](?<module>[A-Z][a-zA-Z]+)/', $file, $matches)) {
660662
$module = $matches['namespace'] . '\\' . $matches['module'];
@@ -664,7 +666,7 @@ protected static function _prepareMapLayoutBlocks()
664666
$attributes = $element->attributes();
665667
$block = (string)$attributes->name;
666668
if (!empty($block)) {
667-
self::$_mapLayoutBlocks[$area][$block] = @(self::$_mapLayoutBlocks[$area][$block] ?: []);
669+
self::$_mapLayoutBlocks[$area][$block] = self::$_mapLayoutBlocks[$area][$block] ?? [];
668670
self::$_mapLayoutBlocks[$area][$block][$module] = $module;
669671
}
670672
}
@@ -682,7 +684,7 @@ protected static function _prepareMapLayoutHandles()
682684
$area = 'default';
683685
if (preg_match('/\/(?<area>adminhtml|frontend)\//', $file, $matches)) {
684686
$area = $matches['area'];
685-
self::$_mapLayoutHandles[$area] = @(self::$_mapLayoutHandles[$area] ?: []);
687+
self::$_mapLayoutHandles[$area] = self::$_mapLayoutHandles[$area] ?? [];
686688
}
687689
if (preg_match('/app\/code\/(?<namespace>[A-Z][a-z]+)[_\/\\\\](?<module>[A-Z][a-zA-Z]+)/', $file, $matches)
688690
) {
@@ -691,7 +693,7 @@ protected static function _prepareMapLayoutHandles()
691693
foreach ((array)$xml->xpath('/layout/child::*') as $element) {
692694
/** @var \SimpleXMLElement $element */
693695
$handle = $element->getName();
694-
self::$_mapLayoutHandles[$area][$handle] = @(self::$_mapLayoutHandles[$area][$handle] ?: []);
696+
self::$_mapLayoutHandles[$area][$handle] = self::$_mapLayoutHandles[$area][$handle] ?? [];
695697
self::$_mapLayoutHandles[$area][$handle][$module] = $module;
696698
}
697699
}
@@ -751,6 +753,7 @@ protected static function _initDependencies()
751753
$contents = file_get_contents($file);
752754
$decodedJson = json_decode($contents);
753755
if (null == $decodedJson) {
756+
//phpcs:ignore Magento2.Exceptions.DirectThrow
754757
throw new \Exception("Invalid Json: $file");
755758
}
756759
$json = new \Magento\Framework\Config\Composer\Package(json_decode($contents));
@@ -839,6 +842,7 @@ private static function getPackageModuleMapping(): array
839842
$contents = file_get_contents($file);
840843
$composerJson = json_decode($contents);
841844
if (null == $composerJson) {
845+
//phpcs:ignore Magento2.Exceptions.DirectThrow
842846
throw new \Exception("Invalid Json: $file");
843847
}
844848
$moduleXml = simplexml_load_file(dirname($file) . '/etc/module.xml');

0 commit comments

Comments
 (0)