4 * PHPUnit bootstrap file.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
25 if ( PHP_SAPI
!== 'cli' ) {
26 die( 'This file is only meant to be executed indirectly by PHPUnit\'s bootstrap process!' );
30 * PHPUnit includes the bootstrap file inside a method body, while most MediaWiki startup files
31 * assume to be included in the global scope.
32 * This utility provides a way to include these files: it makes all globals available in the
33 * inclusion scope before including the file, then exports all new or changed globals.
35 * @param string $fileName the file to include
37 function wfRequireOnceInGlobalScope( $fileName ) {
38 // phpcs:disable MediaWiki.Usage.ForbiddenFunctions.extract
39 extract( $GLOBALS, EXTR_REFS | EXTR_SKIP
);
42 require_once $fileName;
44 foreach ( get_defined_vars() as $varName => $value ) {
45 $GLOBALS[$varName] = $value;
49 define( 'MEDIAWIKI', true );
50 define( 'MW_PHPUNIT_TEST', true );
52 // We don't use a settings file here but some code still assumes that one exists
53 define( 'MW_CONFIG_FILE', 'LocalSettings.php' );
55 $IP = realpath( __DIR__
. '/../../' );
57 // these variables must be defined before setup runs
59 // Set bootstrap globals to reuse in MediaWikiUnitTestCase
60 $bootstrapGlobals = [];
61 foreach ( $GLOBALS as $key => $value ) {
62 $bootstrapGlobals[ $key ] = $value;
64 $GLOBALS['wgPhpUnitBootstrapGlobals'] = $bootstrapGlobals;
65 // Faking for Setup.php
66 $GLOBALS['wgScopeTest'] = 'MediaWiki Setup.php scope test';
67 $GLOBALS['wgCommandLineMode'] = true;
68 $GLOBALS['wgAutoloadClasses'] = [];
70 require_once "$IP/tests/common/TestSetup.php";
72 wfRequireOnceInGlobalScope( "$IP/includes/AutoLoader.php" );
73 wfRequireOnceInGlobalScope( "$IP/tests/common/TestsAutoLoader.php" );
74 wfRequireOnceInGlobalScope( "$IP/includes/Defines.php" );
75 wfRequireOnceInGlobalScope( "$IP/includes/DefaultSettings.php" );
76 wfRequireOnceInGlobalScope( "$IP/includes/GlobalFunctions.php" );
78 // Load extensions/skins present in filesystem so that classes can be discovered.
79 $directoryToJsonMap = [
80 'extensions' => [ 'extension.json', 'extension-wip.json' ],
81 'skins' => [ 'skin.json', 'skin-wip.json' ]
83 foreach ( $directoryToJsonMap as $directory => $jsonFile ) {
84 foreach ( new DirectoryIterator( __DIR__
. '/../../' . $directory ) as $iterator ) {
85 foreach ( $jsonFile as $file ) {
86 $jsonPath = $iterator->getPathname() . '/' . $file;
87 if ( file_exists( $jsonPath ) ) {
88 $json = file_get_contents( $jsonPath );
89 $info = json_decode( $json, true );
90 $dir = dirname( $jsonPath );
91 ExtensionRegistry
::exportAutoloadClassesAndNamespaces( $dir, $info );