Merge "Make show/hide link in RC individually localizable"
[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' => __DIR__ . '/../data/autoloader/TestAutoloadedSerializedClass.php',
14 );
15 $this->setMwGlobals( 'wgAutoloadLocalClasses', $this->testLocalClasses + $wgAutoloadLocalClasses );
16 AutoLoader::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
42 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
43 $expected = $wgAutoloadLocalClasses + $wgAutoloadClasses;
44 $actual = array();
45
46 $files = array_unique( $expected );
47
48 foreach ( $files as $file ) {
49 // Only prefix $IP if it doesn't have it already.
50 // Generally local classes don't have it, and those from extensions and test suites do.
51 if ( substr( $file, 0, 1 ) != '/' && substr( $file, 1, 1 ) != ':' ) {
52 $filePath = "$IP/$file";
53 } else {
54 $filePath = $file;
55 }
56
57 $contents = file_get_contents( $filePath );
58
59 // We could use token_get_all() here, but this is faster
60 $matches = array();
61 preg_match_all( '/
62 ^ [\t ]* (?:
63 (?:final\s+)? (?:abstract\s+)? (?:class|interface) \s+
64 (?P<class> [a-zA-Z0-9_]+)
65 |
66 class_alias \s* \( \s*
67 ([\'"]) (?P<original> [^\'"]+) \g{-2} \s* , \s*
68 ([\'"]) (?P<alias> [^\'"]+ ) \g{-2} \s*
69 \) \s* ;
70 )
71 /imx', $contents, $matches, PREG_SET_ORDER );
72
73 $classesInFile = array();
74 $aliasesInFile = array();
75
76 foreach ( $matches as $match ) {
77 if ( !empty( $match['class'] ) ) {
78 $actual[$match['class']] = $file;
79 $classesInFile[$match['class']] = true;
80 } else {
81 $aliasesInFile[$match['alias']] = $match['original'];
82 }
83 }
84
85 // Only accept aliases for classes in the same file, because for correct
86 // behavior, all aliases for a class must be set up when the class is loaded
87 // (see <https://bugs.php.net/bug.php?id=61422>).
88 foreach ( $aliasesInFile as $alias => $class ) {
89 if ( isset( $classesInFile[$class] ) ) {
90 $actual[$alias] = $file;
91 } else {
92 $actual[$alias] = "[original class not in $file]";
93 }
94 }
95 }
96
97 return array(
98 'expected' => $expected,
99 'actual' => $actual,
100 );
101 }
102
103 function testCoreClass() {
104 $this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
105 }
106
107 function testExtensionClass() {
108 $this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
109 }
110
111 function testWrongCaseClass() {
112 $this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
113 }
114
115 function testWrongCaseSerializedClass() {
116 $dummyCereal = 'O:29:"testautoloadedserializedclass":0:{}';
117 $uncerealized = unserialize( $dummyCereal );
118 $this->assertFalse( $uncerealized instanceof __PHP_Incomplete_Class,
119 "unserialize() can load classes case-insensitively." );
120 }
121 }