Minor optimization to the AutoLoader
[lhc/web/wiklou.git] / tests / phpunit / structure / AutoLoaderTest.php
1 <?php
2 class AutoLoaderTest extends MediaWikiTestCase {
3
4 protected function setUp() {
5 global $wgAutoloadLocalClasses, $wgAutoloadClasses;
6
7 parent::setUp();
8
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 );
15 $this->setMwGlobals( 'wgAutoloadLocalClasses', $this->testLocalClasses + $wgAutoloadLocalClasses );
16 InstrumentedAutoLoader::resetAutoloadLocalClassesLower();
17
18 $this->testExtensionClasses = array(
19 'TestAutoloadedClass' => __DIR__ . '/../data/autoloader/TestAutoloadedClass.php',
20 );
21 $this->setMwGlobals( 'wgAutoloadClasses', $this->testExtensionClasses + $wgAutoloadClasses );
22 }
23
24 /**
25 * Assert that there were no classes loaded that are not registered with the AutoLoader.
26 *
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.
29 */
30 public function testAutoLoadConfig() {
31 $results = self::checkAutoLoadConf();
32
33 $this->assertEquals(
34 $results['expected'],
35 $results['actual']
36 );
37 }
38
39 protected static function checkAutoLoadConf() {
40 global $wgAutoloadLocalClasses, $wgAutoloadClasses, $IP;
41 $supportsParsekit = function_exists( 'parsekit_compile_file' );
42
43 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
44 $expected = $wgAutoloadLocalClasses + $wgAutoloadClasses;
45 $actual = array();
46
47 $files = array_unique( $expected );
48
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";
54 } else {
55 $filePath = $file;
56 }
57 if ( $supportsParsekit ) {
58 $parseInfo = parsekit_compile_file( "$filePath" );
59 $classes = array_keys( $parseInfo['class_table'] );
60 } else {
61 $contents = file_get_contents( "$filePath" );
62 $m = array();
63 preg_match_all( '/\n\s*(?:final)?\s*(?:abstract)?\s*(?:class|interface)\s+([a-zA-Z0-9_]+)/', $contents, $m, PREG_PATTERN_ORDER );
64 $classes = $m[1];
65 }
66 foreach ( $classes as $class ) {
67 $actual[$class] = $file;
68 }
69 }
70
71 return array(
72 'expected' => $expected,
73 'actual' => $actual,
74 );
75 }
76
77 function testCoreClass() {
78 $this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
79 }
80
81 function testExtensionClass() {
82 $this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
83 }
84
85 function testWrongCaseClass() {
86 $this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
87 }
88
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.");
94 }
95 }
96
97 /**
98 * Cheater to poke protected members
99 */
100 class InstrumentedAutoLoader extends AutoLoader {
101 static function resetAutoloadLocalClassesLower() {
102 self::$autoloadLocalClassesLower = null;
103 }
104 }