Merge "Fix Language::parseFormattedNumber for lzh and zh-classical"
[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 $classesInFile = array();
78 $aliasesInFile = array();
79
80 foreach ( $matches as $match ) {
81 if ( !empty( $match['class'] ) ) {
82 $actual[$match['class']] = $file;
83 $classesInFile[$match['class']] = true;
84 } else {
85 $aliasesInFile[$match['alias']] = $match['original'];
86 }
87 }
88
89 // Only accept aliases for classes in the same file, because for correct
90 // behavior, all aliases for a class must be set up when the class is loaded
91 // (see <https://bugs.php.net/bug.php?id=61422>).
92 foreach ( $aliasesInFile as $alias => $class ) {
93 if ( isset( $classesInFile[$class] ) ) {
94 $actual[$alias] = $file;
95 } else {
96 $actual[$alias] = "[original class not in $file]";
97 }
98 }
99 }
100
101 return array(
102 'expected' => $expected,
103 'actual' => $actual,
104 );
105 }
106
107 function testCoreClass() {
108 $this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
109 }
110
111 function testExtensionClass() {
112 $this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
113 }
114
115 function testWrongCaseClass() {
116 $this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
117 }
118
119 function testWrongCaseSerializedClass() {
120 $dummyCereal = 'O:29:"testautoloadedserializedclass":0:{}';
121 $uncerealized = unserialize( $dummyCereal );
122 $this->assertFalse( $uncerealized instanceof __PHP_Incomplete_Class,
123 "unserialize() can load classes case-insensitively." );
124 }
125 }