MDL-66903 testing: Add support for a \tests\ namespace during tests

This commit:
- introduces a \tests\ sub-namespace for use in unit tests only
- the path to this the tests/classes directory of the owning parent
- files here are excluded from unit test runs

This is agreed per policy in MDL-80855.
This commit is contained in:
Andrew Nicols
2024-07-15 12:21:54 +08:00
parent b15b73848a
commit 6b3d8fd889
5 changed files with 231 additions and 60 deletions
+32
View File
@@ -172,6 +172,38 @@ class core_component {
require($file);
return;
}
if (PHPUNIT_TEST) {
// For unit tests we support classes in `\frankenstyle_component\tests\` to be loaded from
// `path/to/frankenstyle/component/tests/classes` directory.
// Note: We do *not* support the legacy `\frankenstyle_component_tests_style_classnames`.
if ($component = self::get_component_from_classname($classname)) {
$pathoptions = [
'/tests/classes' => "{$component}\\tests\\",
'/tests/behat' => "{$component}\\behat\\",
];
foreach ($pathoptions as $path => $testnamespace) {
if (preg_match("#^" . preg_quote($testnamespace) . "#", $classname)) {
$path = self::get_component_directory($component) . $path;
$relativeclassname = str_replace(
$testnamespace,
'',
$classname,
);
$file = sprintf(
"%s/%s.php",
$path,
str_replace('\\', '/', $relativeclassname),
);
if (!empty($file) && file_exists($file)) {
require($file);
return;
}
break;
}
}
}
}
}
/**