aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/structure/StructureTest.php
blob: 2554516325a4d80743b5f69c8ca9ecfb3e187228 (plain) (blame)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php

use SebastianBergmann\FileIterator\Facade;

/**
 * The tests here verify the structure of the code.  This is for outright bugs,
 * not just style issues.
 * @coversNothing
 */
class StructureTest extends \PHPUnit\Framework\TestCase {
	/**
	 * Verify all files that appear to be tests have file names ending in
	 * Test.  If the file names do not end in Test, they will not be run.
	 * @group medium
	 */
	public function testUnitTestFileNamesEndWithTest() {
		// realpath() also normalizes directory separator on windows for prefix compares
		$rootPath = realpath( __DIR__ . '/..' );
		$suitesPath = realpath( __DIR__ . '/../suites/' );
		$testClassRegex = '/^(final )?class .* extends [\S]*(TestCase|TestBase)\\b/m';

		$results = $this->recurseFiles( $rootPath );

		$results = array_filter(
			$results,
			static function ( $filename ) use ( $testClassRegex, $suitesPath ) {
				// Remove testUnitTestFileNamesEndWithTest false positives
				if ( str_starts_with( $filename, $suitesPath ) ||
					str_ends_with( $filename, 'Test.php' )
				) {
					return false;
				}
				$contents = file_get_contents( $filename );
				return preg_match( $testClassRegex, $contents );
			}
		);
		$strip = strlen( $rootPath ) + 1;
		foreach ( $results as $k => $v ) {
			$results[$k] = substr( $v, $strip );
		}

		// Normalize indexes to make failure output less confusing
		$results = array_values( $results );

		$this->assertEquals(
			[],
			$results,
			"Unit test file in $rootPath must end with Test."
		);
	}

	private function recurseFiles( $dir ) {
		return ( new Facade() )->getFilesAsArray( $dir, [ '.php' ] );
	}
}