MediaWiki REL1_28
AutoLoaderTest.php
Go to the documentation of this file.
1<?php
2
4 protected function setUp() {
5 parent::setUp();
6
7 // Fancy dance to trigger a rebuild of AutoLoader::$autoloadLocalClassesLower
8 $this->mergeMwGlobalArrayValue( 'wgAutoloadLocalClasses', [
9 'TestAutoloadedLocalClass' =>
10 __DIR__ . '/../data/autoloader/TestAutoloadedLocalClass.php',
11 'TestAutoloadedCamlClass' =>
12 __DIR__ . '/../data/autoloader/TestAutoloadedCamlClass.php',
13 'TestAutoloadedSerializedClass' =>
14 __DIR__ . '/../data/autoloader/TestAutoloadedSerializedClass.php',
15 ] );
17
18 $this->mergeMwGlobalArrayValue( 'wgAutoloadClasses', [
19 'TestAutoloadedClass' => __DIR__ . '/../data/autoloader/TestAutoloadedClass.php',
20 ] );
21 }
22
29 public function testAutoLoadConfig() {
30 $results = self::checkAutoLoadConf();
31
32 $this->assertEquals(
33 $results['expected'],
34 $results['actual']
35 );
36 }
37
38 protected static function checkAutoLoadConf() {
40
41 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
43 $actual = [];
44
45 $files = array_unique( $expected );
46
47 foreach ( $files as $class => $file ) {
48 // Only prefix $IP if it doesn't have it already.
49 // Generally local classes don't have it, and those from extensions and test suites do.
50 if ( substr( $file, 0, 1 ) != '/' && substr( $file, 1, 1 ) != ':' ) {
51 $filePath = "$IP/$file";
52 } else {
53 $filePath = $file;
54 }
55
56 if ( !file_exists( $filePath ) ) {
57 $actual[$class] = "[file '$filePath' does not exist]";
58 continue;
59 }
60
61 MediaWiki\suppressWarnings();
62 $contents = file_get_contents( $filePath );
63 MediaWiki\restoreWarnings();
64
65 if ( $contents === false ) {
66 $actual[$class] = "[couldn't read file '$filePath']";
67 continue;
68 }
69
70 // We could use token_get_all() here, but this is faster
71 $matches = [];
72 preg_match_all( '/
73 ^ [\t ]* (?:
74 (?:final\s+)? (?:abstract\s+)? (?:class|interface|trait) \s+
75 (?P<class> [a-zA-Z0-9_]+)
76 |
77 class_alias \s* \‍( \s*
78 ([\'"]) (?P<original> [^\'"]+) \g{-2} \s* , \s*
79 ([\'"]) (?P<alias> [^\'"]+ ) \g{-2} \s*
80 \‍) \s* ;
81 )
82 /imx', $contents, $matches, PREG_SET_ORDER );
83
84 $namespaceMatch = [];
85 preg_match( '/
86 ^ [\t ]*
87 namespace \s+
88 ([a-zA-Z0-9_]+(\\\\[a-zA-Z0-9_]+)*)
89 \s* ;
90 /imx', $contents, $namespaceMatch );
91 $fileNamespace = $namespaceMatch ? $namespaceMatch[1] . '\\' : '';
92
93 $classesInFile = [];
94 $aliasesInFile = [];
95
96 foreach ( $matches as $match ) {
97 if ( !empty( $match['class'] ) ) {
98 $class = $fileNamespace . $match['class'];
99 $actual[$class] = $file;
100 $classesInFile[$class] = true;
101 } else {
102 $aliasesInFile[$match['alias']] = $match['original'];
103 }
104 }
105
106 // Only accept aliases for classes in the same file, because for correct
107 // behavior, all aliases for a class must be set up when the class is loaded
108 // (see <https://bugs.php.net/bug.php?id=61422>).
109 foreach ( $aliasesInFile as $alias => $class ) {
110 if ( isset( $classesInFile[$class] ) ) {
111 $actual[$alias] = $file;
112 } else {
113 $actual[$alias] = "[original class not in $file]";
114 }
115 }
116 }
117
118 return [
119 'expected' => $expected,
120 'actual' => $actual,
121 ];
122 }
123
124 function testCoreClass() {
125 $this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
126 }
127
129 $this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
130 }
131
133 $this->setMwGlobals( 'wgAutoloadAttemptLowercase', true );
134
135 $this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
136 }
137
139 $this->setMwGlobals( 'wgAutoloadAttemptLowercase', true );
140
141 $dummyCereal = 'O:29:"testautoloadedserializedclass":0:{}';
142 $uncerealized = unserialize( $dummyCereal );
143 $this->assertFalse( $uncerealized instanceof __PHP_Incomplete_Class,
144 "unserialize() can load classes case-insensitively." );
145 }
146
147 function testAutoloadOrder() {
148 $path = realpath( __DIR__ . '/../../..' );
149 $oldAutoload = file_get_contents( $path . '/autoload.php' );
150 $generator = new AutoloadGenerator( $path, 'local' );
151 $generator->initMediaWikiDefault();
152 $newAutoload = $generator->getAutoload( 'maintenance/generateLocalAutoload.php' );
153
154 $this->assertEquals( $oldAutoload, $newAutoload, 'autoload.php does not match' .
155 ' output of generateLocalAutoload.php script.' );
156 }
157}
unserialize( $serialized)
$wgAutoloadClasses
Array mapping class names to filenames, for autoloading.
$IP
Definition WebStart.php:58
global $wgAutoloadLocalClasses
Definition autoload.php:4
testAutoLoadConfig()
Assert that there were no classes loaded that are not registered with the AutoLoader.
static checkAutoLoadConf()
static resetAutoloadLocalClassesLower()
Method to clear the protected class property $autoloadLocalClassesLower.
Accepts a list of files and directories to search for php files and generates $wgAutoloadLocalClasses...
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
setMwGlobals( $pairs, $value=null)
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
$files
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37