Merge "Fix doc error in new incr test"
[lhc/web/wiklou.git] / tests / phpunit / structure / AutoLoaderTest.php
1 <?php
2 class AutoLoaderTest extends MediaWikiTestCase {
3
4 /**
5 * Assert that there were no classes loaded that are not registered with the AutoLoader.
6 *
7 * For example foo.php having class Foo and class Bar but only registering Foo.
8 * This is important because we should not be relying on Foo being used before Bar.
9 */
10 public function testAutoLoadConfig() {
11 $results = self::checkAutoLoadConf();
12
13 $this->assertEquals(
14 $results['expected'],
15 $results['actual']
16 );
17 }
18
19 protected static function checkAutoLoadConf() {
20 global $wgAutoloadLocalClasses, $wgAutoloadClasses, $IP;
21 $supportsParsekit = function_exists( 'parsekit_compile_file' );
22
23 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
24 $expected = $wgAutoloadLocalClasses + $wgAutoloadClasses;
25 $actual = array();
26
27 $files = array_unique( $expected );
28
29 foreach ( $files as $file ) {
30 // Only prefix $IP if it doesn't have it already.
31 // Generally local classes don't have it, and those from extensions and test suites do.
32 if ( substr( $file, 0, 1 ) != '/' && substr( $file, 1, 1 ) != ':' ) {
33 $filePath = "$IP/$file";
34 } else {
35 $filePath = $file;
36 }
37 if ( $supportsParsekit ) {
38 $parseInfo = parsekit_compile_file( "$filePath" );
39 $classes = array_keys( $parseInfo['class_table'] );
40 } else {
41 $contents = file_get_contents( "$filePath" );
42 $m = array();
43 preg_match_all( '/\n\s*(?:final)?\s*(?:abstract)?\s*(?:class|interface)\s+([a-zA-Z0-9_]+)/', $contents, $m, PREG_PATTERN_ORDER );
44 $classes = $m[1];
45 }
46 foreach ( $classes as $class ) {
47 $actual[$class] = $file;
48 }
49 }
50
51 return array(
52 'expected' => $expected,
53 'actual' => $actual,
54 );
55 }
56 }