MediaWiki REL1_30
AutoLoaderTest.php
Go to the documentation of this file.
1<?php
2
4 protected function setUp() {
5 parent::setUp();
6
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',
15 ] );
17
18 $this->mergeMwGlobalArrayValue( 'wgAutoloadClasses', [
19 'TestAutoloadedClass' => __DIR__ . '/../data/autoloader/TestAutoloadedClass.php',
20 ] );
21 }
22
29 public function testAutoLoadConfig() {
30 $results = self::checkAutoLoadConf();
31
32 $this->assertEquals(
33 $results['expected'],
34 $results['actual']
35 );
36 }
37
38 protected static function checkAutoLoadConf() {
40
41 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
43 $actual = [];
44
45 $files = array_unique( $expected );
46
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";
52 } else {
53 $filePath = $file;
54 }
55
56 if ( !file_exists( $filePath ) ) {
57 $actual[$class] = "[file '$filePath' does not exist]";
58 continue;
59 }
60
61 MediaWiki\suppressWarnings();
62 $contents = file_get_contents( $filePath );
63 MediaWiki\restoreWarnings();
64
65 if ( $contents === false ) {
66 $actual[$class] = "[couldn't read file '$filePath']";
67 continue;
68 }
69
70 // We could use token_get_all() here, but this is faster
71 // Note: Keep in sync with ClassCollector
72 $matches = [];
73 preg_match_all( '/
74 ^ [\t ]* (?:
75 (?:final\s+)? (?:abstract\s+)? (?:class|interface|trait) \s+
76 (?P<class> [a-zA-Z0-9_]+)
77 |
78 class_alias \s* \‍( \s*
79 ([\'"]) (?P<original> [^\'"]+) \g{-2} \s* , \s*
80 ([\'"]) (?P<alias> [^\'"]+ ) \g{-2} \s*
81 \‍) \s* ;
82 |
83 class_alias \s* \‍( \s*
84 (?P<originalStatic> [a-zA-Z0-9_]+)::class \s* , \s*
85 ([\'"]) (?P<aliasString> [^\'"]+ ) \g{-2} \s*
86 \‍) \s* ;
87 )
88 /imx', $contents, $matches, PREG_SET_ORDER );
89
90 $namespaceMatch = [];
91 preg_match( '/
92 ^ [\t ]*
93 namespace \s+
94 ([a-zA-Z0-9_]+(\\\\[a-zA-Z0-9_]+)*)
95 \s* ;
96 /imx', $contents, $namespaceMatch );
97 $fileNamespace = $namespaceMatch ? $namespaceMatch[1] . '\\' : '';
98
99 $classesInFile = [];
100 $aliasesInFile = [];
101
102 foreach ( $matches as $match ) {
103 if ( !empty( $match['class'] ) ) {
104 // 'class Foo {}'
105 $class = $fileNamespace . $match['class'];
106 $actual[$class] = $file;
107 $classesInFile[$class] = true;
108 } else {
109 if ( !empty( $match['original'] ) ) {
110 // 'class_alias( "Foo", "Bar" );'
111 $aliasesInFile[$match['alias']] = $match['original'];
112 } else {
113 // 'class_alias( Foo::class, "Bar" );'
114 $aliasesInFile[$match['aliasString']] = $fileNamespace . $match['originalStatic'];
115 }
116 }
117 }
118
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;
125 } else {
126 $actual[$alias] = "[original class not in $file]";
127 }
128 }
129 }
130
131 return [
132 'expected' => $expected,
133 'actual' => $actual,
134 ];
135 }
136
137 function testCoreClass() {
138 $this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
139 }
140
142 $this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
143 }
144
146 $this->setMwGlobals( 'wgAutoloadAttemptLowercase', true );
147
148 $this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
149 }
150
152 $this->setMwGlobals( 'wgAutoloadAttemptLowercase', true );
153
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." );
158 }
159
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' );
166
167 $this->assertEquals( $oldAutoload, $newAutoload, 'autoload.php does not match' .
168 ' output of generateLocalAutoload.php script.' );
169 }
170}
unserialize( $serialized)
$wgAutoloadClasses
Array mapping class names to filenames, for autoloading.
global $wgAutoloadLocalClasses
Definition autoload.php:4
testAutoLoadConfig()
Assert that there were no classes loaded that are not registered with the AutoLoader.
static checkAutoLoadConf()
static resetAutoloadLocalClassesLower()
Method to clear the protected class property $autoloadLocalClassesLower.
Accepts a list of files and directories to search for php files and generates $wgAutoloadLocalClasses...
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
$IP
Definition update.php:3