Merge "Changed FOR UPDATE handling in Postgresql"
[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 'TestAutoloadedAliasedClass' => 'alias:TestAutoloadedAliasedClassNew',
15 'TestAutoloadedAliasedClassDeprecated' => 'alias:TestAutoloadedAliasedClassNew?v=1.1',
16 'TestAutoloadedAliasedClassNew' => __DIR__ . '/../data/autoloader/TestAutoloadedAliasedClassNew.php',
17 );
18 $this->setMwGlobals( 'wgAutoloadLocalClasses', $this->testLocalClasses + $wgAutoloadLocalClasses );
19 AutoLoader::resetAutoloadLocalClassesLower();
20
21 $this->testExtensionClasses = array(
22 'TestAutoloadedClass' => __DIR__ . '/../data/autoloader/TestAutoloadedClass.php',
23 );
24 $this->setMwGlobals( 'wgAutoloadClasses', $this->testExtensionClasses + $wgAutoloadClasses );
25 }
26
27 /**
28 * Assert that there were no classes loaded that are not registered with the AutoLoader.
29 *
30 * For example foo.php having class Foo and class Bar but only registering Foo.
31 * This is important because we should not be relying on Foo being used before Bar.
32 */
33 public function testAutoLoadConfig() {
34 $results = self::checkAutoLoadConf();
35
36 $this->assertEquals(
37 $results['expected'],
38 $results['actual']
39 );
40 }
41
42 protected static function checkAutoLoadConf() {
43 global $wgAutoloadLocalClasses, $wgAutoloadClasses, $IP;
44 $supportsParsekit = function_exists( 'parsekit_compile_file' );
45
46 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
47 $expected = $wgAutoloadLocalClasses + $wgAutoloadClasses;
48 $actual = array();
49
50 // Check aliases
51 foreach ( $expected as $class => $file ) {
52 if ( substr( $file, 0, 6 ) !== 'alias:' ) {
53 // Not an alias, so should be an actual file
54 $files[] = $file;
55 } else {
56 $newClass = substr( $file, 6, strcspn( $file, '?', 6 ) );
57 if ( isset( $expected[$newClass] ) ) {
58 if ( substr( $expected[$newClass], 0, 6 ) !== 'alias:' ) {
59 // Alias pointing to an existing MediaWiki class
60 $actual[$class] = $file;
61 }
62 }
63 }
64 }
65
66 $files = array_unique( $files );
67
68 foreach ( $files as $file ) {
69 // Only prefix $IP if it doesn't have it already.
70 // Generally local classes don't have it, and those from extensions and test suites do.
71 if ( substr( $file, 0, 1 ) != '/' && substr( $file, 1, 1 ) != ':' ) {
72 $filePath = "$IP/$file";
73 } else {
74 $filePath = $file;
75 }
76 if ( $supportsParsekit ) {
77 $parseInfo = parsekit_compile_file( "$filePath" );
78 $classes = array_keys( $parseInfo['class_table'] );
79 } else {
80 $contents = file_get_contents( "$filePath" );
81 $m = array();
82 preg_match_all( '/\n\s*(?:final)?\s*(?:abstract)?\s*(?:class|interface)\s+([a-zA-Z0-9_]+)/', $contents, $m, PREG_PATTERN_ORDER );
83 $classes = $m[1];
84 }
85 foreach ( $classes as $class ) {
86 $actual[$class] = $file;
87 }
88 }
89
90 return array(
91 'expected' => $expected,
92 'actual' => $actual,
93 );
94 }
95
96 function testCoreClass() {
97 $this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
98 }
99
100 function testExtensionClass() {
101 $this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
102 }
103
104 function testWrongCaseClass() {
105 $this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
106 }
107
108 function testWrongCaseSerializedClass() {
109 $dummyCereal = 'O:29:"testautoloadedserializedclass":0:{}';
110 $uncerealized = unserialize( $dummyCereal );
111 $this->assertFalse( $uncerealized instanceof __PHP_Incomplete_Class,
112 "unserialize() can load classes case-insensitively." );
113 }
114
115 function testAliasedClass() {
116 $this->assertSame( 'TestAutoloadedAliasedClassNew',
117 get_class( new TestAutoloadedAliasedClass ) );
118 }
119
120 function testAliasedClassDeprecated() {
121 wfSuppressWarnings();
122 $this->assertSame( 'TestAutoloadedAliasedClassNew',
123 get_class( new TestAutoloadedAliasedClassDeprecated ) );
124 wfRestoreWarnings();
125 }
126 }