Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / structure / AutoLoaderStructureTest.php
blobe45229768e85a4f968c7e652425b308254d33fd3
1 <?php
3 /**
4 * @coversNothing
5 */
6 class AutoLoaderStructureTest extends MediaWikiIntegrationTestCase {
7 /**
8 * Assert that there were no classes loaded that are not registered with the AutoLoader.
10 * For example foo.php having class Foo and class Bar but only registering Foo.
11 * This is important because we should not be relying on Foo being used before Bar.
13 public function testAutoLoadConfig() {
14 $results = self::checkAutoLoadConf();
16 $this->assertEquals(
17 $results['expected'],
18 $results['actual']
22 private static function parseFile( $contents ) {
23 // We could use token_get_all() here, but this is faster
24 // Note: Keep in sync with ClassCollector
25 $matches = [];
26 preg_match_all( '/
27 ^ [\t ]* (?:
28 (?:final\s+)? (?:abstract\s+)? (?:class|interface|trait) \s+
29 (?P<class> \w+)
31 class_alias \s* \( \s*
32 ([\'"]) (?P<original> [^\'"]+) \g{-2} \s* , \s*
33 ([\'"]) (?P<alias> [^\'"]+ ) \g{-2} \s*
34 \) \s* ;
36 class_alias \s* \( \s*
37 (?P<originalStatic> [\w\\\\]+)::class \s* , \s*
38 ([\'"]) (?P<aliasString> [^\'"]+ ) \g{-2} \s*
39 \) \s* ;
41 /imx', $contents, $matches, PREG_SET_ORDER );
43 $namespaceMatch = [];
44 preg_match( '/
45 ^ [\t ]*
46 namespace \s+
47 (\w+(\\\\\w+)*)
48 \s* ;
49 /imx', $contents, $namespaceMatch );
50 $fileNamespace = $namespaceMatch ? $namespaceMatch[1] . '\\' : '';
52 $classesInFile = [];
53 $aliasesInFile = [];
55 foreach ( $matches as $match ) {
56 if ( !empty( $match['class'] ) ) {
57 // 'class Foo {}'
58 $class = $fileNamespace . $match['class'];
59 $classesInFile[$class] = true;
60 } elseif ( !empty( $match['original'] ) ) {
61 // 'class_alias( "Foo", "Bar" );'
62 $aliasesInFile[self::removeSlashes( $match['alias'] )] = $match['original'];
63 } else {
64 // 'class_alias( Foo::class, "Bar" );'
65 $aliasesInFile[self::removeSlashes( $match['aliasString'] )] =
66 $fileNamespace . $match['originalStatic'];
70 return [ $classesInFile, $aliasesInFile ];
73 private static function removeSlashes( $str ) {
74 return str_replace( '\\\\', '\\', $str );
77 private static function fixSlashes( $str ) {
78 return str_replace( '\\', '/', $str );
81 protected static function checkAutoLoadConf() {
82 $IP = MW_INSTALL_PATH;
84 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
85 $expected = AutoLoader::getClassFiles();
86 $actual = [];
88 foreach ( $expected as $class => $file ) {
89 // Only prefix $IP if it doesn't have it already.
90 // Generally local classes don't have it, and those from extensions and test suites do.
91 if ( $file[0] !== '/' && $file[1] !== ':' ) {
92 $filePath = self::fixSlashes( "$IP/$file" );
93 } else {
94 $filePath = self::fixSlashes( $file );
97 if ( !is_file( $filePath ) ) {
98 $actual[$class] = "[file '$filePath' does not exist]";
99 continue;
102 $contents = @file_get_contents( $filePath );
103 if ( $contents === false ) {
104 $actual[$class] = "[couldn't read file '$filePath']";
105 continue;
108 [ $classesInFile, $aliasesInFile ] = self::parseFile( $contents );
110 foreach ( $classesInFile as $className => $ignore ) {
111 $actual[$className] = $file;
114 // Only accept aliases for classes in the same file, because for correct
115 // behavior, all aliases for a class must be set up when the class is loaded
116 // (see <https://bugs.php.net/bug.php?id=61422>).
117 foreach ( $aliasesInFile as $alias => $class ) {
118 if ( isset( $classesInFile[$class] ) ) {
119 $actual[$alias] = $file;
120 } else {
121 $actual[$alias] = "[original class not in $file]";
126 return [
127 'expected' => $expected,
128 'actual' => $actual,
132 public function testAutoloadOrder() {
133 $path = __DIR__ . '/../../..';
134 $oldAutoload = file_get_contents( $path . '/autoload.php' );
135 $generator = new AutoloadGenerator( $path, 'local' );
136 $generator->setPsr4Namespaces( AutoLoader::CORE_NAMESPACES );
137 $generator->initMediaWikiDefault();
138 $newAutoload = $generator->getAutoload( 'maintenance/generateLocalAutoload.php' );
140 $this->assertEquals( $oldAutoload, $newAutoload, 'autoload.php does not match' .
141 ' output of generateLocalAutoload.php script.' );
145 * Verify that all the directories specified for PSR-4 autoloading
146 * actually exist, to prevent situations like T259448
148 public function testAutoloadNamespaces() {
149 $missing = [];
150 $psr4Namespaces = AutoLoader::getNamespaceDirectories();
152 foreach ( $psr4Namespaces as $ns => $path ) {
153 if ( !is_dir( $path ) ) {
154 $missing[] = "Directory $path for namespace $ns does not exist";
158 $this->assertSame( [], $missing );