3 class AutoLoaderTest
extends MediaWikiTestCase
{
4 protected function setUp() {
5 global $wgAutoloadLocalClasses, $wgAutoloadClasses;
9 // Fancy dance to trigger a rebuild of AutoLoader::$autoloadLocalClassesLower
10 $this->testLocalClasses
= array(
11 'TestAutoloadedLocalClass' => __DIR__
. '/../data/autoloader/TestAutoloadedLocalClass.php',
12 'TestAutoloadedCamlClass' => __DIR__
. '/../data/autoloader/TestAutoloadedCamlClass.php',
13 'TestAutoloadedSerializedClass' =>
14 __DIR__
. '/../data/autoloader/TestAutoloadedSerializedClass.php',
17 'wgAutoloadLocalClasses',
18 $this->testLocalClasses +
$wgAutoloadLocalClasses
20 AutoLoader
::resetAutoloadLocalClassesLower();
22 $this->testExtensionClasses
= array(
23 'TestAutoloadedClass' => __DIR__
. '/../data/autoloader/TestAutoloadedClass.php',
25 $this->setMwGlobals( 'wgAutoloadClasses', $this->testExtensionClasses +
$wgAutoloadClasses );
29 * Assert that there were no classes loaded that are not registered with the AutoLoader.
31 * For example foo.php having class Foo and class Bar but only registering Foo.
32 * This is important because we should not be relying on Foo being used before Bar.
34 public function testAutoLoadConfig() {
35 $results = self
::checkAutoLoadConf();
43 protected static function checkAutoLoadConf() {
44 global $wgAutoloadLocalClasses, $wgAutoloadClasses, $IP;
46 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
47 $expected = $wgAutoloadLocalClasses +
$wgAutoloadClasses;
50 $files = array_unique( $expected );
52 foreach ( $files as $file ) {
53 // Only prefix $IP if it doesn't have it already.
54 // Generally local classes don't have it, and those from extensions and test suites do.
55 if ( substr( $file, 0, 1 ) != '/' && substr( $file, 1, 1 ) != ':' ) {
56 $filePath = "$IP/$file";
61 $contents = file_get_contents( $filePath );
63 // We could use token_get_all() here, but this is faster
67 (?:final\s+)? (?:abstract\s+)? (?:class|interface) \s+
68 (?P<class> [a-zA-Z0-9_]+)
70 class_alias \s* \( \s*
71 ([\'"]) (?P<original> [^\'"]+) \g{-2} \s* , \s*
72 ([\'"]) (?P<alias> [^\'"]+ ) \g{-2} \s*
75 /imx', $contents, $matches, PREG_SET_ORDER
);
77 $namespaceMatch = array();
81 ([a-zA-Z0-9_]+(\\\\[a-zA-Z0-9_]+)*)
83 /imx', $contents, $namespaceMatch );
84 $fileNamespace = $namespaceMatch ?
$namespaceMatch[1] . '\\' : '';
86 $classesInFile = array();
87 $aliasesInFile = array();
89 foreach ( $matches as $match ) {
90 if ( !empty( $match['class'] ) ) {
91 $class = $fileNamespace . $match['class'];
92 $actual[$class] = $file;
93 $classesInFile[$class] = true;
95 $aliasesInFile[$match['alias']] = $match['original'];
99 // Only accept aliases for classes in the same file, because for correct
100 // behavior, all aliases for a class must be set up when the class is loaded
101 // (see <https://bugs.php.net/bug.php?id=61422>).
102 foreach ( $aliasesInFile as $alias => $class ) {
103 if ( isset( $classesInFile[$class] ) ) {
104 $actual[$alias] = $file;
106 $actual[$alias] = "[original class not in $file]";
112 'expected' => $expected,
117 function testCoreClass() {
118 $this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
121 function testExtensionClass() {
122 $this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
125 function testWrongCaseClass() {
126 $this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
129 function testWrongCaseSerializedClass() {
130 $dummyCereal = 'O:29:"testautoloadedserializedclass":0:{}';
131 $uncerealized = unserialize( $dummyCereal );
132 $this->assertFalse( $uncerealized instanceof __PHP_Incomplete_Class
,
133 "unserialize() can load classes case-insensitively." );