4 * Bootstrapping for MediaWiki PHPUnit tests
9 // Set a flag which can be used to detect when other scripts have been entered
10 // through this entry point or not.
11 define( 'MW_PHPUNIT_TEST', true );
13 $wgPhpUnitClass = 'PHPUnit_TextUI_Command';
15 // Start up MediaWiki in command-line mode
16 require_once dirname( dirname( __DIR__
) ) . "/maintenance/Maintenance.php";
18 class PHPUnitMaintClass
extends Maintenance
{
20 public static $additionalOptions = [
23 'use-filebackend' => false,
24 'use-bagostuff' => false,
25 'use-jobqueue' => false,
26 'keep-uploads' => false,
27 'use-normal-tables' => false,
32 public function __construct() {
33 parent
::__construct();
36 'Class name of the PHPUnit entry point to use',
42 'Log testing activity to the PHPUnitCommand log channel.',
48 'Only run parser tests that match the given regex.',
52 $this->addOption( 'file', 'File describing parser tests.', false, true );
53 $this->addOption( 'use-filebackend', 'Use filebackend', false, true );
54 $this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
55 $this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
58 'Re-use the same upload directory for each test, don\'t delete it.',
62 $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
64 'reuse-db', 'Init DB only if tables are missing and keep after finish.',
70 public function finalSetup() {
73 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType, $wgMainWANCache;
75 global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
76 global $wgLocaltimezone, $wgLocalisationCacheConf;
77 global $wgDevelopmentWarnings;
78 global $wgSessionProviders;
79 global $wgJobTypeConf;
81 // Inject test autoloader
82 require_once __DIR__
. '/../TestsAutoLoader.php';
84 // wfWarn should cause tests to fail
85 $wgDevelopmentWarnings = true;
87 // Make sure all caches and stashes are either disabled or use
88 // in-process cache only to prevent tests from using any preconfigured
89 // cache meant for the local wiki from outside the test run.
90 // See also MediaWikiTestCase::run() which mocks CACHE_DB and APC.
92 // Disabled in DefaultSettings, override local settings
94 $wgMainCacheType = CACHE_NONE
;
95 // Uses CACHE_ANYTHING in DefaultSettings, use hash instead of db
99 $wgLanguageConverterCacheType = 'hash';
100 // Uses db-replicated in DefaultSettings
101 $wgMainStash = 'hash';
102 // Use memory job queue
104 'default' => [ 'class' => 'JobQueueMemory', 'order' => 'fifo' ],
107 $wgUseDatabaseMessages = false; # Set for future resets
109 // Assume UTC for testing purposes
110 $wgLocaltimezone = 'UTC';
112 $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
114 // Generic MediaWiki\Session\SessionManager configuration for tests
115 // We use CookieSessionProvider because things might be expecting
116 // cookies to show up in a FauxRequest somewhere.
117 $wgSessionProviders = [
119 'class' => MediaWiki\Session\CookieSessionProvider
::class,
122 'callUserSetCookiesHook' => true,
127 // Bug 44192 Do not attempt to send a real e-mail
128 Hooks
::clear( 'AlternateUserMailer' );
130 'AlternateUserMailer',
135 // xdebug's default of 100 is too low for MediaWiki
136 ini_set( 'xdebug.max_nesting_level', 1000 );
138 // Bug T116683 serialize_precision of 100
139 // may break testing against floating point values
140 // treated with PHP's serialize()
141 ini_set( 'serialize_precision', 17 );
143 // TODO: we should call MediaWikiTestCase::prepareServices( new GlobalVarConfig() ) here.
144 // But PHPUnit may not be loaded yet, so we have to wait until just
145 // before PHPUnit_TextUI_Command::main() is executed at the end of this file.
148 public function execute() {
151 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
153 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
154 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
155 restore_error_handler();
157 $this->forceFormatServerArgv();
159 # Make sure we have --configuration or PHPUnit might complain
160 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
161 // Hack to eliminate the need to use the Makefile (which sucks ATM)
162 array_splice( $_SERVER['argv'], 1, 0,
163 [ '--configuration', $IP . '/tests/phpunit/suite.xml' ] );
166 if ( $this->hasOption( 'with-phpunitclass' ) ) {
167 global $wgPhpUnitClass;
168 $wgPhpUnitClass = $this->getOption( 'with-phpunitclass' );
170 # Cleanup $args array so the option and its value do not
172 $key = array_search( '--with-phpunitclass', $_SERVER['argv'] );
173 unset( $_SERVER['argv'][$key] ); // the option
174 unset( $_SERVER['argv'][$key +
1] ); // its value
175 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
178 $key = array_search( '--debug-tests', $_SERVER['argv'] );
179 if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
180 unset( $_SERVER['argv'][$key] );
181 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
182 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
185 foreach ( self
::$additionalOptions as $option => $default ) {
186 $key = array_search( '--' . $option, $_SERVER['argv'] );
187 if ( $key !== false ) {
188 unset( $_SERVER['argv'][$key] );
189 if ( $this->mParams
[$option]['withArg'] ) {
190 self
::$additionalOptions[$option] = $_SERVER['argv'][$key +
1];
191 unset( $_SERVER['argv'][$key +
1] );
193 self
::$additionalOptions[$option] = true;
200 public function getDbType() {
201 return Maintenance
::DB_ADMIN
;
205 * Force the format of elements in $_SERVER['argv']
206 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
208 private function forceFormatServerArgv() {
210 foreach ( $_SERVER['argv'] as $key => $arg ) {
213 } elseif ( strstr( $arg, '=' ) ) {
214 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
221 $_SERVER['argv'] = $argv;
226 $maintClass = 'PHPUnitMaintClass';
227 require RUN_MAINTENANCE_IF_MAIN
;
229 if ( !class_exists( 'PHPUnit_Framework_TestCase' ) ) {
230 echo "PHPUnit not found. Please install it and other dev dependencies by
231 running `composer install` in MediaWiki root directory.\n";
234 if ( !class_exists( $wgPhpUnitClass ) ) {
235 echo "PHPUnit entry point '" . $wgPhpUnitClass . "' not found. Please make sure you installed
236 the containing component and check the spelling of the class name.\n";
240 echo defined( 'HHVM_VERSION' ) ?
241 'Using HHVM ' . HHVM_VERSION
. ' (' . PHP_VERSION
. ")\n" :
242 'Using PHP ' . PHP_VERSION
. "\n";
244 // Prepare global services for unit tests.
245 // FIXME: this should be done in the finalSetup() method,
246 // but PHPUnit may not have been loaded at that point.
247 MediaWikiTestCase
::prepareServices( new GlobalVarConfig() );
249 $wgPhpUnitClass::main();