Merge "Add Special:MediaStatistics page for file type stats"
[mediawiki.git] / tests / phpunit / structure / AutoLoaderTest.php
blob2bdc9c9a22845f780d5d760170675fb1ab1bf8ba
1 <?php
3 class AutoLoaderTest extends MediaWikiTestCase {
4 protected function setUp() {
5 global $wgAutoloadLocalClasses, $wgAutoloadClasses;
7 parent::setUp();
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',
16 $this->setMwGlobals(
17 'wgAutoloadLocalClasses',
18 $this->testLocalClasses + $wgAutoloadLocalClasses
20 AutoLoader::resetAutoloadLocalClassesLower();
22 $this->testExtensionClasses = array(
23 'TestAutoloadedClass' => __DIR__ . '/../data/autoloader/TestAutoloadedClass.php',
25 $this->setMwGlobals( 'wgAutoloadClasses', $this->testExtensionClasses + $wgAutoloadClasses );
28 /**
29 * Assert that there were no classes loaded that are not registered with the AutoLoader.
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.
34 public function testAutoLoadConfig() {
35 $results = self::checkAutoLoadConf();
37 $this->assertEquals(
38 $results['expected'],
39 $results['actual']
43 protected static function checkAutoLoadConf() {
44 global $wgAutoloadLocalClasses, $wgAutoloadClasses, $IP;
46 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
47 $expected = $wgAutoloadLocalClasses + $wgAutoloadClasses;
48 $actual = array();
50 $files = array_unique( $expected );
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;
61 $contents = file_get_contents( $filePath );
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_]+)
70 class_alias \s* \( \s*
71 ([\'"]) (?P<original> [^\'"]+) \g{-2} \s* , \s*
72 ([\'"]) (?P<alias> [^\'"]+ ) \g{-2} \s*
73 \) \s* ;
75 /imx', $contents, $matches, PREG_SET_ORDER );
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] . '\\' : '';
86 $classesInFile = array();
87 $aliasesInFile = array();
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'];
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]";
111 return array(
112 'expected' => $expected,
113 'actual' => $actual,
117 function testCoreClass() {
118 $this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
121 function testExtensionClass() {
122 $this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
125 function testWrongCaseClass() {
126 $this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
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." );