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;
41 $supportsParsekit = function_exists( 'parsekit_compile_file' );
43 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
44 $expected = $wgAutoloadLocalClasses +
$wgAutoloadClasses;
47 $files = array_unique( $expected );
49 foreach ( $files as $file ) {
50 // Only prefix $IP if it doesn't have it already.
51 // Generally local classes don't have it, and those from extensions and test suites do.
52 if ( substr( $file, 0, 1 ) != '/' && substr( $file, 1, 1 ) != ':' ) {
53 $filePath = "$IP/$file";
57 if ( $supportsParsekit ) {
58 $parseInfo = parsekit_compile_file( "$filePath" );
59 $classes = array_keys( $parseInfo['class_table'] );
61 $contents = file_get_contents( "$filePath" );
63 preg_match_all( '/\n\s*(?:final)?\s*(?:abstract)?\s*(?:class|interface)\s+([a-zA-Z0-9_]+)/', $contents, $m, PREG_PATTERN_ORDER
);
66 foreach ( $classes as $class ) {
67 $actual[$class] = $file;
72 'expected' => $expected,
77 function testCoreClass() {
78 $this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
81 function testExtensionClass() {
82 $this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
85 function testWrongCaseClass() {
86 $this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
89 function testWrongCaseSerializedClass() {
90 $dummyCereal = 'O:29:"testautoloadedserializedclass":0:{}';
91 $uncerealized = unserialize( $dummyCereal );
92 $this->assertFalse( $uncerealized instanceof __PHP_Incomplete_Class
,
93 "unserialize() can load classes case-insensitively." );