3 use SebastianBergmann\FileIterator\Facade
;
6 * The tests here verify the structure of the code. This is for outright bugs,
7 * not just style issues.
10 class StructureTest
extends \PHPUnit\Framework\TestCase
{
12 * Verify all files that appear to be tests have file names ending in
13 * Test. If the file names do not end in Test, they will not be run.
16 public function testUnitTestFileNamesEndWithTest() {
17 // realpath() also normalizes directory separator on windows for prefix compares
18 $rootPath = realpath( __DIR__
. '/..' );
19 $suitesPath = realpath( __DIR__
. '/../suites/' );
20 $testClassRegex = '/^(final )?class .* extends [\S]*(TestCase|TestBase)\\b/m';
22 $results = $this->recurseFiles( $rootPath );
24 $results = array_filter(
26 static function ( $filename ) use ( $testClassRegex, $suitesPath ) {
27 // Remove testUnitTestFileNamesEndWithTest false positives
28 if ( str_starts_with( $filename, $suitesPath ) ||
29 str_ends_with( $filename, 'Test.php' )
33 $contents = file_get_contents( $filename );
34 return preg_match( $testClassRegex, $contents );
37 $strip = strlen( $rootPath ) +
1;
38 foreach ( $results as $k => $v ) {
39 $results[$k] = substr( $v, $strip );
42 // Normalize indexes to make failure output less confusing
43 $results = array_values( $results );
48 "Unit test file in $rootPath must end with Test."
52 private function recurseFiles( $dir ) {
53 return ( new Facade() )->getFilesAsArray( $dir, [ '.php' ] );