function ClassWriter::alterAssert

Same name in other branches
  1. 10 core/tests/Drupal/TestTools/PhpUnitCompatibility/ClassWriter.php \Drupal\TestTools\PhpUnitCompatibility\ClassWriter::alterAssert()
  2. 11.x core/tests/Drupal/TestTools/PhpUnitCompatibility/ClassWriter.php \Drupal\TestTools\PhpUnitCompatibility\ClassWriter::alterAssert()

Alters the Assert class.

Parameters

\Composer\Autoload\ClassLoader $autoloader: The autoloader.

Throws

\ReflectionException

1 call to ClassWriter::alterAssert()
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 46

Class

ClassWriter
Helper class to rewrite PHPUnit's TestCase class.

Namespace

Drupal\TestTools\PhpUnitCompatibility\PhpUnit8

Code

private static function alterAssert(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\\Assert', FALSE)) {
        return;
    }
    // Mutate Assert code to make it forward compatible with different PhpUnit
    // versions, by adding Symfony's PHPUnit-bridge PolyfillAssertTrait.
    $alteredFile = $autoloader->findFile('PHPUnit\\Framework\\Assert');
    $phpunit_dir = dirname($alteredFile, 3);
    $alteredCode = file_get_contents($alteredFile);
    $alteredCode = preg_replace('/abstract class Assert[^\\{]+\\{/', '$0 ' . \PHP_EOL . "    use \\Symfony\\Bridge\\PhpUnit\\Legacy\\PolyfillAssertTrait;" . \PHP_EOL, $alteredCode, 1);
    include static::flushAlteredCodeToFile('Assert.php', $alteredCode);
}

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