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