11<?php
22/**
3- * Scan source code for incorrect or undeclared modules dependencies
4- *
53 * Copyright © Magento, Inc. All rights reserved.
64 * See COPYING.txt for license details.
7- *
85 */
96namespace Magento \Test \Integrity ;
107
2926use Magento \TestFramework \Dependency \VirtualType \VirtualTypeMapper ;
3027
3128/**
29+ * Scan source code for incorrect or undeclared modules dependencies
30+ *
3231 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
3332 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
33+ * @SuppressWarnings(PHPMD.TooManyFields)
3434 */
3535class DependencyTest extends \PHPUnit \Framework \TestCase
3636{
3737 /**
3838 * Soft dependency between modules
3939 */
40- const TYPE_SOFT = 'soft ' ;
40+ public const TYPE_SOFT = 'soft ' ;
4141
4242 /**
4343 * Hard dependency between modules
4444 */
45- const TYPE_HARD = 'hard ' ;
45+ public const TYPE_HARD = 'hard ' ;
4646
4747 /**
4848 * The identifier of dependency for mapping.
4949 */
50- const MAP_TYPE_DECLARED = 'declared ' ;
50+ public const MAP_TYPE_DECLARED = 'declared ' ;
5151
5252 /**
5353 * The identifier of dependency for mapping.
5454 */
55- const MAP_TYPE_FOUND = 'found ' ;
55+ public const MAP_TYPE_FOUND = 'found ' ;
5656
5757 /**
5858 * The identifier of dependency for mapping.
5959 */
60- const MAP_TYPE_REDUNDANT = 'redundant ' ;
60+ public const MAP_TYPE_REDUNDANT = 'redundant ' ;
6161
6262 /**
6363 * Count of directories in path
6464 */
65- const DIR_PATH_COUNT = 4 ;
65+ public const DIR_PATH_COUNT = 4 ;
6666
6767 /**
6868 * List of config.xml files by modules
@@ -155,15 +155,11 @@ class DependencyTest extends \PHPUnit\Framework\TestCase
155155 private static $ whiteList = [];
156156
157157 /**
158- * Routes whitelist
159- *
160158 * @var array|null
161159 */
162160 private static $ routesWhitelist = null ;
163161
164162 /**
165- * Redundant dependencies whitelist
166- *
167163 * @var array|null
168164 */
169165 private static $ redundantDependenciesWhitelist = null ;
@@ -188,6 +184,16 @@ class DependencyTest extends \PHPUnit\Framework\TestCase
188184 */
189185 private $ undeclaredDependencyBlacklist ;
190186
187+ /**
188+ * @var array|null
189+ */
190+ private static $ extensionConflicts = null ;
191+
192+ /**
193+ * @var array|null
194+ */
195+ private static $ allowedDependencies = null ;
196+
191197 /**
192198 * Sets up data
193199 *
@@ -1202,4 +1208,112 @@ protected function _isFake($module)
12021208 {
12031209 return isset (self ::$ mapDependencies [$ module ]) ? false : true ;
12041210 }
1211+
1212+ /**
1213+ * Test modules don't have direct dependencies on modules that might be disabled by 3rd-party Magento extensions.
1214+ *
1215+ * @inheritdoc
1216+ * @throws \Exception
1217+ * @return void
1218+ */
1219+ public function testDirectExtensionDependencies ()
1220+ {
1221+ $ invoker = new \Magento \Framework \App \Utility \AggregateInvoker ($ this );
1222+
1223+ $ extensionConflictList = self ::getExtensionConflicts ();
1224+ $ allowedDependencies = self ::getAllowedDependencies ();
1225+
1226+ $ invoker (
1227+ /**
1228+ * Check modules dependencies for specified file
1229+ *
1230+ * @param string $fileType
1231+ * @param string $file
1232+ */
1233+ function ($ fileType , $ file ) use ($ extensionConflictList , $ allowedDependencies ) {
1234+ $ module = $ this ->getModuleNameForRelevantFile ($ file );
1235+ if (!$ module ) {
1236+ return ;
1237+ }
1238+
1239+ $ contents = $ this ->_getCleanedFileContents ($ fileType , $ file );
1240+
1241+ $ dependencies = $ this ->getDependenciesFromFiles ($ module , $ fileType , $ file , $ contents );
1242+
1243+ $ modules = [];
1244+ foreach ($ dependencies as $ dependency ) {
1245+ $ modules [] = $ dependency ['modules ' ];
1246+ }
1247+
1248+ $ modulesDependencies = array_merge (...$ modules );
1249+
1250+ foreach ($ extensionConflictList as $ extension => $ disabledModules ) {
1251+ $ modulesThatMustBeDisabled = \array_unique (array_intersect ($ modulesDependencies , $ disabledModules ));
1252+ if (!empty ($ modulesThatMustBeDisabled )) {
1253+
1254+ foreach ($ modulesThatMustBeDisabled as $ foundedModule ) {
1255+ if (!empty ($ allowedDependencies [$ foundedModule ])
1256+ && \in_array ($ module , $ allowedDependencies [$ foundedModule ])
1257+ ) {
1258+ // skip, this dependency is allowed
1259+ continue ;
1260+ }
1261+
1262+ $ this ->fail (
1263+ \sprintf (
1264+ 'Module "%s" has dependency on: "%s". ' .
1265+ ' No direct dependencies must be added on "%s", ' .
1266+ ' because it must be disabled when "%s" extension is used. ' .
1267+ ' See AC-2516 for more details ' ,
1268+ $ module ,
1269+ \implode (', ' , $ modulesThatMustBeDisabled ),
1270+ $ module ,
1271+ $ extension
1272+ )
1273+ );
1274+ }
1275+ }
1276+ }
1277+ },
1278+ $ this ->getAllFiles ()
1279+ );
1280+ }
1281+
1282+ /**
1283+ * Initialize extension conflicts list.
1284+ *
1285+ * @return array
1286+ */
1287+ private static function getExtensionConflicts (): array
1288+ {
1289+ if (null === self ::$ extensionConflicts ) {
1290+ $ extensionConflictsFilePattern =
1291+ realpath (__DIR__ ) . '/_files/extension_dependencies_test/extension_conflicts/*.php ' ;
1292+ $ extensionConflicts = [];
1293+ foreach (glob ($ extensionConflictsFilePattern ) as $ fileName ) {
1294+ $ extensionConflicts [] = include $ fileName ;
1295+ }
1296+ self ::$ extensionConflicts = \array_merge_recursive ([], ...$ extensionConflicts );
1297+ }
1298+ return self ::$ extensionConflicts ;
1299+ }
1300+
1301+ /**
1302+ * Initialize allowed dependencies.
1303+ *
1304+ * @return array
1305+ */
1306+ private static function getAllowedDependencies (): array
1307+ {
1308+ if (null === self ::$ allowedDependencies ) {
1309+ $ allowedDependenciesFilePattern =
1310+ realpath (__DIR__ ) . '/_files/extension_dependencies_test/allowed_dependencies/*.php ' ;
1311+ $ allowedDependencies = [];
1312+ foreach (glob ($ allowedDependenciesFilePattern ) as $ fileName ) {
1313+ $ allowedDependencies [] = include $ fileName ;
1314+ }
1315+ self ::$ allowedDependencies = \array_merge_recursive ([], ...$ allowedDependencies );
1316+ }
1317+ return self ::$ allowedDependencies ;
1318+ }
12051319}
0 commit comments