You are here

private static function ClassWriter::alterAssert in Drupal 9

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);
}