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' => __DIR__
. '/../data/autoloader/TestAutoloadedSerializedClass.php',
15 $this->setMwGlobals( 'wgAutoloadLocalClasses', $this->testLocalClasses +
$wgAutoloadLocalClasses );
16 AutoLoader
::resetAutoloadLocalClassesLower();
18 $this->testExtensionClasses
= array(
19 'TestAutoloadedClass' => __DIR__
. '/../data/autoloader/TestAutoloadedClass.php',
21 $this->setMwGlobals( 'wgAutoloadClasses', $this->testExtensionClasses +
$wgAutoloadClasses );
25 * Assert that there were no classes loaded that are not registered with the AutoLoader.
27 * For example foo.php having class Foo and class Bar but only registering Foo.
28 * This is important because we should not be relying on Foo being used before Bar.
30 public function testAutoLoadConfig() {
31 $results = self
::checkAutoLoadConf();
39 protected static function checkAutoLoadConf() {
40 global $wgAutoloadLocalClasses, $wgAutoloadClasses, $IP;
42 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
43 $expected = $wgAutoloadLocalClasses +
$wgAutoloadClasses;
46 $files = array_unique( $expected );
48 foreach ( $files as $file ) {
49 // Only prefix $IP if it doesn't have it already.
50 // Generally local classes don't have it, and those from extensions and test suites do.
51 if ( substr( $file, 0, 1 ) != '/' && substr( $file, 1, 1 ) != ':' ) {
52 $filePath = "$IP/$file";
57 $contents = file_get_contents( $filePath );
59 // We could use token_get_all() here, but this is faster
63 (?:final\s+)? (?:abstract\s+)? (?:class|interface) \s+
64 (?P<class> [a-zA-Z0-9_]+)
66 class_alias \s* \( \s*
67 ([\'"]) (?P<original> [^\'"]+) \g{-2} \s* , \s*
68 ([\'"]) (?P<alias> [^\'"]+ ) \g{-2} \s*
71 /imx', $contents, $matches, PREG_SET_ORDER
);
73 $classesInFile = array();
74 $aliasesInFile = array();
76 foreach ( $matches as $match ) {
77 if ( !empty( $match['class'] ) ) {
78 $actual[$match['class']] = $file;
79 $classesInFile[$match['class']] = true;
81 $aliasesInFile[$match['alias']] = $match['original'];
85 // Only accept aliases for classes in the same file, because for correct
86 // behavior, all aliases for a class must be set up when the class is loaded
87 // (see <https://bugs.php.net/bug.php?id=61422>).
88 foreach ( $aliasesInFile as $alias => $class ) {
89 if ( isset( $classesInFile[$class] ) ) {
90 $actual[$alias] = $file;
92 $actual[$alias] = "[original class not in $file]";
98 'expected' => $expected,
103 function testCoreClass() {
104 $this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
107 function testExtensionClass() {
108 $this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
111 function testWrongCaseClass() {
112 $this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
115 function testWrongCaseSerializedClass() {
116 $dummyCereal = 'O:29:"testautoloadedserializedclass":0:{}';
117 $uncerealized = unserialize( $dummyCereal );
118 $this->assertFalse( $uncerealized instanceof __PHP_Incomplete_Class
,
119 "unserialize() can load classes case-insensitively." );