Separate MediaWiki unit and integration tests
[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 'MediaWikiUnitTestCase',
25 'MediaWikiTestCase',
26 'ResourceLoaderTestCase',
27 'PHPUnit_Framework_TestCase',
28 '\\?PHPUnit\\Framework\\TestCase',
29 'TestCase', // \PHPUnit\Framework\TestCase with appropriate use statement
30 'DumpTestCase',
31 'SpecialPageTestBase',
32 ] );
33 $testClassRegex = "/^class .* extends ($testClassRegex)/m";
34
35 $results = $this->recurseFiles( $rootPath );
36
37 $results = array_filter(
38 $results,
39 function ( $filename ) use ( $testClassRegex, $suitesPath ) {
40 // Remove testUnitTestFileNamesEndWithTest false positives
41 if ( strpos( $filename, $suitesPath ) === 0
42 || substr( $filename, -8 ) === 'Test.php'
43 ) {
44 return false;
45 }
46 $contents = file_get_contents( $filename );
47 return preg_match( $testClassRegex, $contents );
48 }
49 );
50 $strip = strlen( $rootPath ) + 1;
51 foreach ( $results as $k => $v ) {
52 $results[$k] = substr( $v, $strip );
53 }
54 $this->assertEquals(
55 [],
56 $results,
57 "Unit test file in $rootPath must end with Test."
58 );
59 }
60
61 private function recurseFiles( $dir ) {
62 return ( new File_Iterator_Facade() )->getFilesAsArray( $dir, [ '.php' ] );
63 }
64 }