3 class AutoLoaderTest
extends MediaWikiTestCase
{
4 protected function setUp() {
7 // Fancy dance to trigger a rebuild of AutoLoader::$autoloadLocalClassesLower
8 $this->mergeMwGlobalArrayValue( 'wgAutoloadLocalClasses', [
9 'TestAutoloadedLocalClass' =>
10 __DIR__
. '/../data/autoloader/TestAutoloadedLocalClass.php',
11 'TestAutoloadedCamlClass' =>
12 __DIR__
. '/../data/autoloader/TestAutoloadedCamlClass.php',
13 'TestAutoloadedSerializedClass' =>
14 __DIR__
. '/../data/autoloader/TestAutoloadedSerializedClass.php',
16 AutoLoader
::resetAutoloadLocalClassesLower();
18 $this->mergeMwGlobalArrayValue( 'wgAutoloadClasses', [
19 'TestAutoloadedClass' => __DIR__
. '/../data/autoloader/TestAutoloadedClass.php',
24 * Assert that there were no classes loaded that are not registered with the AutoLoader.
26 * For example foo.php having class Foo and class Bar but only registering Foo.
27 * This is important because we should not be relying on Foo being used before Bar.
29 public function testAutoLoadConfig() {
30 $results = self
::checkAutoLoadConf();
38 protected static function checkAutoLoadConf() {
39 global $wgAutoloadLocalClasses, $wgAutoloadClasses, $IP;
41 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
42 $expected = $wgAutoloadLocalClasses +
$wgAutoloadClasses;
45 $files = array_unique( $expected );
47 foreach ( $files as $class => $file ) {
48 // Only prefix $IP if it doesn't have it already.
49 // Generally local classes don't have it, and those from extensions and test suites do.
50 if ( substr( $file, 0, 1 ) != '/' && substr( $file, 1, 1 ) != ':' ) {
51 $filePath = "$IP/$file";
56 if ( !file_exists( $filePath ) ) {
57 $actual[$class] = "[file '$filePath' does not exist]";
61 MediaWiki\
suppressWarnings();
62 $contents = file_get_contents( $filePath );
63 MediaWiki\restoreWarnings
();
65 if ( $contents === false ) {
66 $actual[$class] = "[couldn't read file '$filePath']";
70 // We could use token_get_all() here, but this is faster
71 // Note: Keep in sync with ClassCollector
75 (?:final\s+)? (?:abstract\s+)? (?:class|interface|trait) \s+
76 (?P<class> [a-zA-Z0-9_]+)
78 class_alias \s* \( \s*
79 ([\'"]) (?P<original> [^\'"]+) \g{-2} \s* , \s*
80 ([\'"]) (?P<alias> [^\'"]+ ) \g{-2} \s*
83 class_alias \s* \( \s*
84 (?P<originalStatic> [a-zA-Z0-9_]+)::class \s* , \s*
85 ([\'"]) (?P<aliasString> [^\'"]+ ) \g{-2} \s*
88 /imx', $contents, $matches, PREG_SET_ORDER
);
94 ([a-zA-Z0-9_]+(\\\\[a-zA-Z0-9_]+)*)
96 /imx', $contents, $namespaceMatch );
97 $fileNamespace = $namespaceMatch ?
$namespaceMatch[1] . '\\' : '';
102 foreach ( $matches as $match ) {
103 if ( !empty( $match['class'] ) ) {
105 $class = $fileNamespace . $match['class'];
106 $actual[$class] = $file;
107 $classesInFile[$class] = true;
109 if ( !empty( $match['original'] ) ) {
110 // 'class_alias( "Foo", "Bar" );'
111 $aliasesInFile[$match['alias']] = $match['original'];
113 // 'class_alias( Foo::class, "Bar" );'
114 $aliasesInFile[$match['aliasString']] = $fileNamespace . $match['originalStatic'];
119 // Only accept aliases for classes in the same file, because for correct
120 // behavior, all aliases for a class must be set up when the class is loaded
121 // (see <https://bugs.php.net/bug.php?id=61422>).
122 foreach ( $aliasesInFile as $alias => $class ) {
123 if ( isset( $classesInFile[$class] ) ) {
124 $actual[$alias] = $file;
126 $actual[$alias] = "[original class not in $file]";
132 'expected' => $expected,
137 function testCoreClass() {
138 $this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
141 function testExtensionClass() {
142 $this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
145 function testWrongCaseClass() {
146 $this->setMwGlobals( 'wgAutoloadAttemptLowercase', true );
148 $this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
151 function testWrongCaseSerializedClass() {
152 $this->setMwGlobals( 'wgAutoloadAttemptLowercase', true );
154 $dummyCereal = 'O:29:"testautoloadedserializedclass":0:{}';
155 $uncerealized = unserialize( $dummyCereal );
156 $this->assertFalse( $uncerealized instanceof __PHP_Incomplete_Class
,
157 "unserialize() can load classes case-insensitively." );
160 function testAutoloadOrder() {
161 $path = realpath( __DIR__
. '/../../..' );
162 $oldAutoload = file_get_contents( $path . '/autoload.php' );
163 $generator = new AutoloadGenerator( $path, 'local' );
164 $generator->initMediaWikiDefault();
165 $newAutoload = $generator->getAutoload( 'maintenance/generateLocalAutoload.php' );
167 $this->assertEquals( $oldAutoload, $newAutoload, 'autoload.php does not match' .
168 ' output of generateLocalAutoload.php script.' );