forked from mglaman/phpstan-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrupal-phpunit-hack.php
More file actions
33 lines (30 loc) · 1.21 KB
/
drupal-phpunit-hack.php
File metadata and controls
33 lines (30 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php declare(strict_types=1);
/**
* @file
*
* Implements PHPUnit compatibility hack provided by ClassWriter
*
* This ensures we can run our PHPUnit tests against 8.9 and 9, and more.
*
* @see \Drupal\TestTools\PhpUnitCompatibility\PhpUnit8\ClassWriter::mutateTestBase()
*/
$autoloader = null;
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
$autoloader = require __DIR__ . '/vendor/autoload.php';
}
if (!$autoloader instanceof \Composer\Autoload\ClassLoader) {
return;
}
// Inspired by Symfony's simple-phpunit remove typehints from TestCase.
$alteredFile = $autoloader->findFile('PHPUnit\Framework\TestCase');
$phpunit_dir = dirname($alteredFile, 3);
// Mutate TestCase code to make it compatible with Drupal 8 and 9 tests.
$alteredCode = file_get_contents($alteredFile);
$alteredCode = preg_replace('/^ ((?:protected|public)(?: static)? function \w+\(\)): void/m', ' $1', $alteredCode);
$alteredCode = str_replace("__DIR__ . '/../Util/", "'$phpunit_dir/src/Util/", $alteredCode);
// Only write when necessary.
$filename = __DIR__ . '/tests/fixtures/TestCase.php';
if (!file_exists($filename) || md5_file($filename) !== md5($alteredCode)) {
file_put_contents($filename, $alteredCode);
}
include $filename;