private static function ClassWriter::alterTestCase in Drupal 9
Alters the TestCase class.
Parameters
\Composer\Autoload\ClassLoader $autoloader: The autoloader.
Throws
\ReflectionException
1 call to ClassWriter::alterTestCase()
- ClassWriter::mutateTestBase in core/
tests/ Drupal/ TestTools/ PhpUnitCompatibility/ PhpUnit8/ ClassWriter.php - Mutates PHPUnit classes to make it compatible with Drupal.
File
- core/
tests/ Drupal/ TestTools/ PhpUnitCompatibility/ PhpUnit8/ ClassWriter.php, line 71
Class
- ClassWriter
- Helper class to rewrite PHPUnit's TestCase class.
Namespace
Drupal\TestTools\PhpUnitCompatibility\PhpUnit8Code
private static function alterTestCase(ClassLoader $autoloader) : void {
// If the class exists already there is nothing we can do. Hopefully this
// is happening because this has been called already. The call from
// \Drupal\Core\Test\TestDiscovery::registerTestNamespaces() necessitates
// this protection.
if (class_exists('PHPUnit\\Framework\\TestCase', FALSE)) {
return;
}
// Mutate TestCase code to make it forward compatible with different PhpUnit
// versions, by adding Symfony's PHPUnit-bridge PolyfillTestCaseTrait.
$alteredFile = $autoloader
->findFile('PHPUnit\\Framework\\TestCase');
$phpunit_dir = dirname($alteredFile, 3);
$alteredCode = file_get_contents($alteredFile);
$alteredCode = preg_replace('/abstract class TestCase[^\\{]+\\{/', '$0 ' . \PHP_EOL . " use \\Symfony\\Bridge\\PhpUnit\\Legacy\\PolyfillTestCaseTrait;" . \PHP_EOL, $alteredCode, 1);
$alteredCode = str_replace("__DIR__ . '/../Util/", "'{$phpunit_dir}/src/Util/", $alteredCode);
// While Drupal still allows methods in test base classes that inherit from
// TestCase with no void return typehints specified, we also alter TestCase
// to remove the typehints.
// @see https://www.drupal.org/project/drupal/issues/3182103
$alteredCode = preg_replace('/^ ((?:protected|public)(?: static)? function \\w+\\(\\)): void/m', ' $1', $alteredCode);
include static::flushAlteredCodeToFile('TestCase.php', $alteredCode);
}