function ClassWriter::alterTestCase

Same name in other branches
  1. 9 core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit8/ClassWriter.php \Drupal\TestTools\PhpUnitCompatibility\PhpUnit8\ClassWriter::alterTestCase()
  2. 10 core/tests/Drupal/TestTools/PhpUnitCompatibility/ClassWriter.php \Drupal\TestTools\PhpUnitCompatibility\ClassWriter::alterTestCase()

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/ClassWriter.php
Mutates PHPUnit classes to make it compatible with Drupal.

File

core/tests/Drupal/TestTools/PhpUnitCompatibility/ClassWriter.php, line 73

Class

ClassWriter
Helper class to rewrite PHPUnit's TestCase class.

Namespace

Drupal\TestTools\PhpUnitCompatibility

Code

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);
    include static::flushAlteredCodeToFile('TestCase.php', $alteredCode);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.