Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / structure / StructureTest.php
1 <?php
2 /**
3 * The tests here verify the structure of the code. This is for outright bugs,
4 * not just style issues.
5 */
6
7 class StructureTest extends MediaWikiTestCase {
8 /**
9 * Verify all files that appear to be tests have file names ending in
10 * Test. If the file names do not end in Test, they will not be run.
11 * @group medium
12 */
13 public function testUnitTestFileNamesEndWithTest() {
14 // realpath() also normalizes directory separator on windows for prefix compares
15 $rootPath = realpath( __DIR__ . '/..' );
16 $suitesPath = realpath( __DIR__ . '/../suites/' );
17 $testClassRegex = implode( '|', [
18 'ApiFormatTestBase',
19 'ApiTestCase',
20 'ApiQueryTestBase',
21 'ApiQueryContinueTestBase',
22 'MediaWikiLangTestCase',
23 'MediaWikiMediaTestCase',
24 'MediaWikiTestCase',
25 'ResourceLoaderTestCase',
26 'PHPUnit_Framework_TestCase',
27 '\\?PHPUnit\\Framework\\TestCase',
28 'TestCase', // \PHPUnit\Framework\TestCase with appropriate use statement
29 'DumpTestCase',
30 'SpecialPageTestBase',
31 ] );
32 $testClassRegex = "/^class .* extends ($testClassRegex)/m";
33
34 $results = $this->recurseFiles( $rootPath );
35
36 $results = array_filter(
37 $results,
38 function ( $filename ) use ( $testClassRegex, $suitesPath ) {
39 // Remove testUnitTestFileNamesEndWithTest false positives
40 if ( strpos( $filename, $suitesPath ) === 0
41 || substr( $filename, -8 ) === 'Test.php'
42 ) {
43 return false;
44 }
45 $contents = file_get_contents( $filename );
46 return preg_match( $testClassRegex, $contents );
47 }
48 );
49 $strip = strlen( $rootPath ) + 1;
50 foreach ( $results as $k => $v ) {
51 $results[$k] = substr( $v, $strip );
52 }
53 $this->assertEquals(
54 [],
55 $results,
56 "Unit test file in $rootPath must end with Test."
57 );
58 }
59
60 private function recurseFiles( $dir ) {
61 return ( new File_Iterator_Facade() )->getFilesAsArray( $dir, [ '.php' ] );
62 }
63 }