Removed old HTMLCacheUpdateJob b/c code
[lhc/web/wiklou.git] / tests / phpunit / structure / AutoLoaderTest.php
1 <?php
2
3 class AutoLoaderTest extends MediaWikiTestCase {
4 protected function setUp() {
5 parent::setUp();
6
7 // Fancy dance to trigger a rebuild of AutoLoader::$autoloadLocalClassesLower
8 $this->mergeMwGlobalArrayValue( 'wgAutoloadLocalClasses', array(
9 'TestAutoloadedLocalClass' =>
10 __DIR__ . '/../data/autoloader/TestAutoloadedLocalClass.php',
11 'TestAutoloadedCamlClass' =>
12 __DIR__ . '/../data/autoloader/TestAutoloadedCamlClass.php',
13 'TestAutoloadedSerializedClass' =>
14 __DIR__ . '/../data/autoloader/TestAutoloadedSerializedClass.php',
15 ) );
16 AutoLoader::resetAutoloadLocalClassesLower();
17
18 $this->mergeMwGlobalArrayValue( 'wgAutoloadClasses', array(
19 'TestAutoloadedClass' => __DIR__ . '/../data/autoloader/TestAutoloadedClass.php',
20 ) );
21 }
22
23 /**
24 * Assert that there were no classes loaded that are not registered with the AutoLoader.
25 *
26 * For example foo.php having class Foo and class Bar but only registering Foo.
27 * This is important because we should not be relying on Foo being used before Bar.
28 */
29 public function testAutoLoadConfig() {
30 $results = self::checkAutoLoadConf();
31
32 $this->assertEquals(
33 $results['expected'],
34 $results['actual']
35 );
36 }
37
38 protected static function checkAutoLoadConf() {
39 global $wgAutoloadLocalClasses, $wgAutoloadClasses, $IP;
40
41 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
42 $expected = $wgAutoloadLocalClasses + $wgAutoloadClasses;
43 $actual = array();
44
45 $files = array_unique( $expected );
46
47 foreach ( $files as $file ) {
48 // Only prefix $IP if it doesn't have it already.
49 // Generally local classes don't have it, and those from extensions and test suites do.
50 if ( substr( $file, 0, 1 ) != '/' && substr( $file, 1, 1 ) != ':' ) {
51 $filePath = "$IP/$file";
52 } else {
53 $filePath = $file;
54 }
55
56 $contents = file_get_contents( $filePath );
57
58 // We could use token_get_all() here, but this is faster
59 $matches = array();
60 preg_match_all( '/
61 ^ [\t ]* (?:
62 (?:final\s+)? (?:abstract\s+)? (?:class|interface) \s+
63 (?P<class> [a-zA-Z0-9_]+)
64 |
65 class_alias \s* \( \s*
66 ([\'"]) (?P<original> [^\'"]+) \g{-2} \s* , \s*
67 ([\'"]) (?P<alias> [^\'"]+ ) \g{-2} \s*
68 \) \s* ;
69 )
70 /imx', $contents, $matches, PREG_SET_ORDER );
71
72 $namespaceMatch = array();
73 preg_match( '/
74 ^ [\t ]*
75 namespace \s+
76 ([a-zA-Z0-9_]+(\\\\[a-zA-Z0-9_]+)*)
77 \s* ;
78 /imx', $contents, $namespaceMatch );
79 $fileNamespace = $namespaceMatch ? $namespaceMatch[1] . '\\' : '';
80
81 $classesInFile = array();
82 $aliasesInFile = array();
83
84 foreach ( $matches as $match ) {
85 if ( !empty( $match['class'] ) ) {
86 $class = $fileNamespace . $match['class'];
87 $actual[$class] = $file;
88 $classesInFile[$class] = true;
89 } else {
90 $aliasesInFile[$match['alias']] = $match['original'];
91 }
92 }
93
94 // Only accept aliases for classes in the same file, because for correct
95 // behavior, all aliases for a class must be set up when the class is loaded
96 // (see <https://bugs.php.net/bug.php?id=61422>).
97 foreach ( $aliasesInFile as $alias => $class ) {
98 if ( isset( $classesInFile[$class] ) ) {
99 $actual[$alias] = $file;
100 } else {
101 $actual[$alias] = "[original class not in $file]";
102 }
103 }
104 }
105
106 return array(
107 'expected' => $expected,
108 'actual' => $actual,
109 );
110 }
111
112 function testCoreClass() {
113 $this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
114 }
115
116 function testExtensionClass() {
117 $this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
118 }
119
120 function testWrongCaseClass() {
121 $this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
122 }
123
124 function testWrongCaseSerializedClass() {
125 $dummyCereal = 'O:29:"testautoloadedserializedclass":0:{}';
126 $uncerealized = unserialize( $dummyCereal );
127 $this->assertFalse( $uncerealized instanceof __PHP_Incomplete_Class,
128 "unserialize() can load classes case-insensitively." );
129 }
130 }