Pass phpcs-strict on some test files (11/11)
[lhc/web/wiklou.git] / tests / phpunit / structure / AutoLoaderTest.php
index d8b90d5..12f147e 100644 (file)
@@ -10,9 +10,13 @@ class AutoLoaderTest extends MediaWikiTestCase {
                $this->testLocalClasses = array(
                        'TestAutoloadedLocalClass' => __DIR__ . '/../data/autoloader/TestAutoloadedLocalClass.php',
                        'TestAutoloadedCamlClass' => __DIR__ . '/../data/autoloader/TestAutoloadedCamlClass.php',
-                       'TestAutoloadedSerializedClass' => __DIR__ . '/../data/autoloader/TestAutoloadedSerializedClass.php',
+                       'TestAutoloadedSerializedClass' =>
+                               __DIR__ . '/../data/autoloader/TestAutoloadedSerializedClass.php',
+               );
+               $this->setMwGlobals(
+                       'wgAutoloadLocalClasses',
+                       $this->testLocalClasses + $wgAutoloadLocalClasses
                );
-               $this->setMwGlobals( 'wgAutoloadLocalClasses', $this->testLocalClasses + $wgAutoloadLocalClasses );
                AutoLoader::resetAutoloadLocalClassesLower();
 
                $this->testExtensionClasses = array(
@@ -38,7 +42,6 @@ class AutoLoaderTest extends MediaWikiTestCase {
 
        protected static function checkAutoLoadConf() {
                global $wgAutoloadLocalClasses, $wgAutoloadClasses, $IP;
-               $supportsParsekit = function_exists( 'parsekit_compile_file' );
 
                // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
                $expected = $wgAutoloadLocalClasses + $wgAutoloadClasses;
@@ -54,17 +57,44 @@ class AutoLoaderTest extends MediaWikiTestCase {
                        } else {
                                $filePath = $file;
                        }
-                       if ( $supportsParsekit ) {
-                               $parseInfo = parsekit_compile_file( "$filePath" );
-                               $classes = array_keys( $parseInfo['class_table'] );
-                       } else {
-                               $contents = file_get_contents( "$filePath" );
-                               $m = array();
-                               preg_match_all( '/\n\s*(?:final)?\s*(?:abstract)?\s*(?:class|interface)\s+([a-zA-Z0-9_]+)/', $contents, $m, PREG_PATTERN_ORDER );
-                               $classes = $m[1];
+
+                       $contents = file_get_contents( $filePath );
+
+                       // We could use token_get_all() here, but this is faster
+                       $matches = array();
+                       preg_match_all( '/
+                               ^ [\t ]* (?:
+                                       (?:final\s+)? (?:abstract\s+)? (?:class|interface) \s+
+                                       (?P<class> [a-zA-Z0-9_]+)
+                               |
+                                       class_alias \s* \( \s*
+                                               ([\'"]) (?P<original> [^\'"]+) \g{-2} \s* , \s*
+                                               ([\'"]) (?P<alias> [^\'"]+ ) \g{-2} \s*
+                                       \) \s* ;
+                               )
+                       /imx', $contents, $matches, PREG_SET_ORDER );
+
+                       $classesInFile = array();
+                       $aliasesInFile = array();
+
+                       foreach ( $matches as $match ) {
+                               if ( !empty( $match['class'] ) ) {
+                                       $actual[$match['class']] = $file;
+                                       $classesInFile[$match['class']] = true;
+                               } else {
+                                       $aliasesInFile[$match['alias']] = $match['original'];
+                               }
                        }
-                       foreach ( $classes as $class ) {
-                               $actual[$class] = $file;
+
+                       // Only accept aliases for classes in the same file, because for correct
+                       // behavior, all aliases for a class must be set up when the class is loaded
+                       // (see <https://bugs.php.net/bug.php?id=61422>).
+                       foreach ( $aliasesInFile as $alias => $class ) {
+                               if ( isset( $classesInFile[$class] ) ) {
+                                       $actual[$alias] = $file;
+                               } else {
+                                       $actual[$alias] = "[original class not in $file]";
+                               }
                        }
                }