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',
14 'TestAutoloadedAliasedClass' => 'alias:TestAutoloadedAliasedClassNew',
15 'TestAutoloadedAliasedClassDeprecated' => 'alias:TestAutoloadedAliasedClassNew?v=1.1',
16 'TestAutoloadedAliasedClassNew' => __DIR__
. '/../data/autoloader/TestAutoloadedAliasedClassNew.php',
18 $this->setMwGlobals( 'wgAutoloadLocalClasses', $this->testLocalClasses +
$wgAutoloadLocalClasses );
19 AutoLoader
::resetAutoloadLocalClassesLower();
21 $this->testExtensionClasses
= array(
22 'TestAutoloadedClass' => __DIR__
. '/../data/autoloader/TestAutoloadedClass.php',
24 $this->setMwGlobals( 'wgAutoloadClasses', $this->testExtensionClasses +
$wgAutoloadClasses );
28 * Assert that there were no classes loaded that are not registered with the AutoLoader.
30 * For example foo.php having class Foo and class Bar but only registering Foo.
31 * This is important because we should not be relying on Foo being used before Bar.
33 public function testAutoLoadConfig() {
34 $results = self
::checkAutoLoadConf();
42 protected static function checkAutoLoadConf() {
43 global $wgAutoloadLocalClasses, $wgAutoloadClasses, $IP;
44 $supportsParsekit = function_exists( 'parsekit_compile_file' );
46 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
47 $expected = $wgAutoloadLocalClasses +
$wgAutoloadClasses;
51 foreach ( $expected as $class => $file ) {
52 if ( substr( $file, 0, 6 ) !== 'alias:' ) {
53 // Not an alias, so should be an actual file
56 $newClass = substr( $file, 6, strcspn( $file, '?', 6 ) );
57 if ( isset( $expected[$newClass] ) ) {
58 if ( substr( $expected[$newClass], 0, 6 ) !== 'alias:' ) {
59 // Alias pointing to an existing MediaWiki class
60 $actual[$class] = $file;
66 $files = array_unique( $files );
68 foreach ( $files as $file ) {
69 // Only prefix $IP if it doesn't have it already.
70 // Generally local classes don't have it, and those from extensions and test suites do.
71 if ( substr( $file, 0, 1 ) != '/' && substr( $file, 1, 1 ) != ':' ) {
72 $filePath = "$IP/$file";
76 if ( $supportsParsekit ) {
77 $parseInfo = parsekit_compile_file( "$filePath" );
78 $classes = array_keys( $parseInfo['class_table'] );
80 $contents = file_get_contents( "$filePath" );
82 preg_match_all( '/\n\s*(?:final)?\s*(?:abstract)?\s*(?:class|interface)\s+([a-zA-Z0-9_]+)/', $contents, $m, PREG_PATTERN_ORDER
);
85 foreach ( $classes as $class ) {
86 $actual[$class] = $file;
91 'expected' => $expected,
96 function testCoreClass() {
97 $this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
100 function testExtensionClass() {
101 $this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
104 function testWrongCaseClass() {
105 $this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
108 function testWrongCaseSerializedClass() {
109 $dummyCereal = 'O:29:"testautoloadedserializedclass":0:{}';
110 $uncerealized = unserialize( $dummyCereal );
111 $this->assertFalse( $uncerealized instanceof __PHP_Incomplete_Class
,
112 "unserialize() can load classes case-insensitively." );
115 function testAliasedClass() {
116 $this->assertSame( 'TestAutoloadedAliasedClassNew',
117 get_class( new TestAutoloadedAliasedClass
) );
120 function testAliasedClassDeprecated() {
121 wfSuppressWarnings();
122 $this->assertSame( 'TestAutoloadedAliasedClassNew',
123 get_class( new TestAutoloadedAliasedClassDeprecated
) );