function ClassWriter::flushAlteredCodeToFile

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

Flushes altered class code to file when necessary.

Parameters

string $file_name: The file name.

string $altered_code: The altered code.

Return value

string The full path of the file to be included.

2 calls to ClassWriter::flushAlteredCodeToFile()
ClassWriter::alterAssert in core/tests/Drupal/TestTools/PhpUnitCompatibility/ClassWriter.php
Alters the Assert class.
ClassWriter::alterTestCase in core/tests/Drupal/TestTools/PhpUnitCompatibility/ClassWriter.php
Alters the TestCase class.

File

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

Class

ClassWriter
Helper class to rewrite PHPUnit's TestCase class.

Namespace

Drupal\TestTools\PhpUnitCompatibility

Code

private static function flushAlteredCodeToFile(string $file_name, string $altered_code) : string {
    $directory = __DIR__ . '/../../../../../sites/simpletest';
    $full_path = $directory . '/' . $file_name;
    // Only write when necessary.
    if (!file_exists($full_path) || md5_file($full_path) !== md5($altered_code)) {
        // Create directory when necessary.
        if (!is_dir($directory) && !@mkdir($directory, 0777, TRUE) && !is_dir($directory)) {
            throw new \RuntimeException('Unable to create directory: ' . $directory);
        }
        file_put_contents($full_path, $altered_code);
    }
    return $full_path;
}

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