Skip to content

Commit 54a61f8

Browse files
authored
MQE-1124: [Github Issue] Allow running tests for modules installed in… (#228)
* MQE-1124: [Github Issue] Allow running tests for modules installed in Magento instance via vendor directory. #162 - Adjusted Module Resolver to look Recursively from Project Root, Magento BP and dev/tests - Updated Module Resolver Unit Tests to reflect changes * MQE-1124: [Github Issue] Allow running tests for modules installed in Magento instance via vendor directory. #162 - Reintroduced recursive_merge to prevent similar directories with different contents being missed * MQE-1124: [Github Issue] Allow running tests for modules installed in Magento instance via vendor directory. - Updated to include checking through registered Magento modules * MQE-1124: [Github Issue] Allow running tests for modules installed in Magento instance via vendor directory. - Used class constant instead of just adding it * MQE-1124: [Github Issue] Allow running tests for modules installed in Magento instance via vendor directory. - Adding requested changes * MQE-1124: [Github Issue] Allow running tests for modules installed in Magento instance via vendor directory. - Specified additional error in case of bad Magento basepath
1 parent 1de0283 commit 54a61f8

File tree

2 files changed

+77
-25
lines changed

2 files changed

+77
-25
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/Util/ModuleResolverTest.php

+5-12
Original file line numberDiff line numberDiff line change
@@ -96,27 +96,22 @@ public function testGetModulePathsLocations()
9696
);
9797

9898
// Define the Module paths from app/code
99-
$appCodePath = MAGENTO_BP
100-
. DIRECTORY_SEPARATOR
101-
. 'app' . DIRECTORY_SEPARATOR
102-
. 'code' . DIRECTORY_SEPARATOR;
99+
$magentoBaseCodePath = MAGENTO_BP;
103100

104101
// Define the Module paths from default TESTS_MODULE_PATH
105102
$modulePath = defined('TESTS_MODULE_PATH') ? TESTS_MODULE_PATH : TESTS_BP;
106103

107104
// Define the Module paths from vendor modules
108-
$vendorCodePath = PROJECT_ROOT
109-
. DIRECTORY_SEPARATOR
110-
. 'vendor' . DIRECTORY_SEPARATOR;
105+
$projectRootCodePath = PROJECT_ROOT;
111106

112107
$mockResolver->verifyInvoked('globRelevantPaths', [$modulePath, '']);
113108
$mockResolver->verifyInvoked(
114109
'globRelevantPaths',
115-
[$appCodePath, DIRECTORY_SEPARATOR . 'Test' . DIRECTORY_SEPARATOR .'Mftf']
110+
[$magentoBaseCodePath, 'Test' . DIRECTORY_SEPARATOR .'Mftf']
116111
);
117112
$mockResolver->verifyInvoked(
118113
'globRelevantPaths',
119-
[$vendorCodePath, DIRECTORY_SEPARATOR . 'Test' . DIRECTORY_SEPARATOR .'Mftf']
114+
[$projectRootCodePath, 'Test' . DIRECTORY_SEPARATOR .'Mftf']
120115
);
121116
}
122117

@@ -151,8 +146,6 @@ public function testGetModulePathsBlacklist()
151146
function ($arg1, $arg2) {
152147
if ($arg2 === "") {
153148
$mockValue = ["somePath" => "somePath"];
154-
} elseif (strpos($arg1, "app")) {
155-
$mockValue = ["otherPath" => "otherPath"];
156149
} else {
157150
$mockValue = ["lastPath" => "lastPath"];
158151
}
@@ -161,7 +154,7 @@ function ($arg1, $arg2) {
161154
);
162155
$resolver = ModuleResolver::getInstance();
163156
$this->setMockResolverProperties($resolver, null, null, ["somePath"]);
164-
$this->assertEquals(["otherPath", "lastPath"], $resolver->getModulesPath());
157+
$this->assertEquals(["lastPath", "lastPath"], $resolver->getModulesPath());
165158
TestLoggingUtil::getInstance()->validateMockLogStatement(
166159
'info',
167160
'excluding module',

src/Magento/FunctionalTestingFramework/Util/ModuleResolver.php

+72-13
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ class ModuleResolver
2828
*/
2929
const CUSTOM_MODULE_PATHS = 'CUSTOM_MODULE_PATHS';
3030

31+
/**
32+
* List of path types present in Magento Component Registrar
33+
*/
34+
const PATHS = ['module', 'library', 'theme', 'language'];
35+
36+
/**
37+
* Magento Registrar Class
38+
*/
39+
const REGISTRAR_CLASS = "\Magento\Framework\Component\ComponentRegistrar";
40+
41+
/**
42+
* Magento Directory Structure Name Prefix
43+
*/
44+
const MAGENTO_PREFIX = "Magento_";
45+
3146
/**
3247
* Enabled modules.
3348
*
@@ -218,25 +233,20 @@ private function aggregateTestModulePaths()
218233
{
219234
$allModulePaths = [];
220235

221-
// Define the Module paths from app/code
222-
$appCodePath = MAGENTO_BP
223-
. DIRECTORY_SEPARATOR
224-
. 'app' . DIRECTORY_SEPARATOR
225-
. 'code' . DIRECTORY_SEPARATOR;
236+
// Define the Module paths from magento bp
237+
$magentoBaseCodePath = MAGENTO_BP;
226238

227239
// Define the Module paths from default TESTS_MODULE_PATH
228240
$modulePath = defined('TESTS_MODULE_PATH') ? TESTS_MODULE_PATH : TESTS_BP;
229241
$modulePath = rtrim($modulePath, DIRECTORY_SEPARATOR);
230242

231-
// Define the Module paths from vendor modules
232-
$vendorCodePath = PROJECT_ROOT
233-
. DIRECTORY_SEPARATOR
234-
. 'vendor' . DIRECTORY_SEPARATOR;
243+
// Define the Module paths from project root
244+
$projectRootCodePath = PROJECT_ROOT;
235245

236246
$codePathsToPattern = [
237247
$modulePath => '',
238-
$appCodePath => DIRECTORY_SEPARATOR . 'Test' . DIRECTORY_SEPARATOR . 'Mftf',
239-
$vendorCodePath => DIRECTORY_SEPARATOR . 'Test' . DIRECTORY_SEPARATOR . 'Mftf'
248+
$magentoBaseCodePath => 'Test' . DIRECTORY_SEPARATOR . 'Mftf',
249+
$projectRootCodePath => 'Test' . DIRECTORY_SEPARATOR . 'Mftf'
240250
];
241251

242252
foreach ($codePathsToPattern as $codePath => $pattern) {
@@ -264,8 +274,11 @@ private function globRelevantPaths($testPath, $pattern)
264274
$relevantPaths = $this->globRelevantWrapper($testPath, $pattern);
265275
}
266276

277+
$allComponents = $this->getRegisteredModuleList();
278+
267279
foreach ($relevantPaths as $codePath) {
268-
$mainModName = basename(str_replace($pattern, '', $codePath));
280+
$mainModName = array_search($codePath, $allComponents) ?: basename(str_replace($pattern, '', $codePath));
281+
$mainModName = str_replace(self::MAGENTO_PREFIX, "", $mainModName);
269282
$modulePaths[$mainModName][] = $codePath;
270283

271284
if (MftfApplicationConfig::getConfig()->verboseEnabled()) {
@@ -288,7 +301,15 @@ private function globRelevantPaths($testPath, $pattern)
288301
*/
289302
private static function globRelevantWrapper($testPath, $pattern)
290303
{
291-
return glob($testPath . '*' . DIRECTORY_SEPARATOR . '*' . $pattern);
304+
if ($pattern == "") {
305+
return glob($testPath . '*' . DIRECTORY_SEPARATOR . '*' . $pattern);
306+
}
307+
$subDirectory = "*" . DIRECTORY_SEPARATOR;
308+
$directories = glob($testPath . $subDirectory . $pattern, GLOB_ONLYDIR);
309+
foreach (glob($testPath . $subDirectory, GLOB_ONLYDIR) as $dir) {
310+
$directories = array_merge_recursive($directories, self::globRelevantWrapper($dir, $pattern));
311+
}
312+
return $directories;
292313
}
293314

294315
/**
@@ -504,4 +525,42 @@ private function getModuleBlacklist()
504525
{
505526
return $this->moduleBlacklist;
506527
}
528+
529+
/**
530+
* Calls Magento method for determining registered modules.
531+
*
532+
* @return string[]
533+
*/
534+
private function getRegisteredModuleList()
535+
{
536+
if (array_key_exists('MAGENTO_BP', $_ENV)) {
537+
$autoloadPath = realpath(MAGENTO_BP . "/app/autoload.php");
538+
if ($autoloadPath) {
539+
require_once($autoloadPath);
540+
} else {
541+
throw new TestFrameworkException("Magento app/autoload.php not found with given MAGENTO_BP:"
542+
. MAGENTO_BP);
543+
}
544+
}
545+
546+
try {
547+
$allComponents = [];
548+
if (!class_exists(self::REGISTRAR_CLASS)) {
549+
throw new TestFrameworkException("Magento Installation not found when loading registered modules.\n");
550+
}
551+
$components = new \Magento\Framework\Component\ComponentRegistrar();
552+
foreach (self::PATHS as $componentType) {
553+
$allComponents = array_merge($allComponents, $components->getPaths($componentType));
554+
}
555+
array_walk($allComponents, function (&$value) {
556+
$value .= DIRECTORY_SEPARATOR . 'Test' . DIRECTORY_SEPARATOR . 'Mftf';
557+
});
558+
return $allComponents;
559+
} catch (TestFrameworkException $e) {
560+
LoggingUtil::getInstance()->getLogger(ModuleResolver::class)->warning(
561+
"$e"
562+
);
563+
}
564+
return [];
565+
}
507566
}

0 commit comments

Comments
 (0)