Merge "mediawiki.util: Optimise logic in addPortletLink"
[mediawiki.git] / tests / phpunit / structure / AutoLoaderTest.php
blob205ea3608284a2500c83756fdda34fc2ab6735ec
1 <?php
2 class AutoLoaderTest extends MediaWikiTestCase {
4 /**
5 * Assert that there were no classes loaded that are not registered with the AutoLoader.
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();
13 $this->assertEquals(
14 $results['expected'],
15 $results['actual']
19 protected static function checkAutoLoadConf() {
20 global $wgAutoloadLocalClasses, $wgAutoloadClasses, $IP;
21 $supportsParsekit = function_exists( 'parsekit_compile_file' );
23 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
24 $expected = $wgAutoloadLocalClasses + $wgAutoloadClasses;
25 $actual = array();
27 $files = array_unique( $expected );
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;
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];
46 foreach ( $classes as $class ) {
47 $actual[$class] = $file;
51 return array(
52 'expected' => $expected,
53 'actual' => $actual,