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 // Start up MediaWiki in command-line mode
14 require_once dirname( dirname( __DIR__
) ) . "/maintenance/Maintenance.php";
16 class PHPUnitMaintClass
extends Maintenance
{
18 public static $additionalOptions = [
21 'use-filebackend' => false,
22 'use-bagostuff' => false,
23 'use-jobqueue' => false,
24 'keep-uploads' => false,
25 'use-normal-tables' => false,
30 public function __construct() {
31 parent
::__construct();
34 'Log testing activity to the PHPUnitCommand log channel.',
40 'Only run parser tests that match the given regex.',
44 $this->addOption( 'file', 'File describing parser tests.', false, true );
45 $this->addOption( 'use-filebackend', 'Use filebackend', false, true );
46 $this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
47 $this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
50 'Re-use the same upload directory for each test, don\'t delete it.',
54 $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
56 'reuse-db', 'Init DB only if tables are missing and keep after finish.',
62 public function finalSetup() {
65 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType, $wgMainWANCache;
67 global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
68 global $wgLocaltimezone, $wgLocalisationCacheConf;
69 global $wgDevelopmentWarnings;
70 global $wgSessionProviders;
71 global $wgJobTypeConf;
73 // Inject test autoloader
74 require_once __DIR__
. '/../TestsAutoLoader.php';
76 // wfWarn should cause tests to fail
77 $wgDevelopmentWarnings = true;
79 // Make sure all caches and stashes are either disabled or use
80 // in-process cache only to prevent tests from using any preconfigured
81 // cache meant for the local wiki from outside the test run.
82 // See also MediaWikiTestCase::run() which mocks CACHE_DB and APC.
84 // Disabled in DefaultSettings, override local settings
86 $wgMainCacheType = CACHE_NONE
;
87 // Uses CACHE_ANYTHING in DefaultSettings, use hash instead of db
91 $wgLanguageConverterCacheType = 'hash';
92 // Uses db-replicated in DefaultSettings
93 $wgMainStash = 'hash';
94 // Use memory job queue
96 'default' => [ 'class' => 'JobQueueMemory', 'order' => 'fifo' ],
99 $wgUseDatabaseMessages = false; # Set for future resets
101 // Assume UTC for testing purposes
102 $wgLocaltimezone = 'UTC';
104 $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
106 // Generic MediaWiki\Session\SessionManager configuration for tests
107 // We use CookieSessionProvider because things might be expecting
108 // cookies to show up in a FauxRequest somewhere.
109 $wgSessionProviders = [
111 'class' => 'MediaWiki\\Session\\CookieSessionProvider',
114 'callUserSetCookiesHook' => true,
119 // Bug 44192 Do not attempt to send a real e-mail
120 Hooks
::clear( 'AlternateUserMailer' );
122 'AlternateUserMailer',
127 // xdebug's default of 100 is too low for MediaWiki
128 ini_set( 'xdebug.max_nesting_level', 1000 );
130 // Bug T116683 serialize_precision of 100
131 // may break testing against floating point values
132 // treated with PHP's serialize()
133 ini_set( 'serialize_precision', 17 );
136 public function execute() {
139 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
141 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
142 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
143 restore_error_handler();
145 $this->forceFormatServerArgv();
147 # Make sure we have --configuration or PHPUnit might complain
148 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
149 // Hack to eliminate the need to use the Makefile (which sucks ATM)
150 array_splice( $_SERVER['argv'], 1, 0,
151 [ '--configuration', $IP . '/tests/phpunit/suite.xml' ] );
154 $key = array_search( '--debug-tests', $_SERVER['argv'] );
155 if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
156 unset( $_SERVER['argv'][$key] );
157 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
158 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
161 foreach ( self
::$additionalOptions as $option => $default ) {
162 $key = array_search( '--' . $option, $_SERVER['argv'] );
163 if ( $key !== false ) {
164 unset( $_SERVER['argv'][$key] );
165 if ( $this->mParams
[$option]['withArg'] ) {
166 self
::$additionalOptions[$option] = $_SERVER['argv'][$key +
1];
167 unset( $_SERVER['argv'][$key +
1] );
169 self
::$additionalOptions[$option] = true;
176 public function getDbType() {
177 return Maintenance
::DB_ADMIN
;
181 * Force the format of elements in $_SERVER['argv']
182 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
184 private function forceFormatServerArgv() {
186 foreach ( $_SERVER['argv'] as $key => $arg ) {
189 } elseif ( strstr( $arg, '=' ) ) {
190 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
197 $_SERVER['argv'] = $argv;
202 $maintClass = 'PHPUnitMaintClass';
203 require RUN_MAINTENANCE_IF_MAIN
;
205 if ( !class_exists( 'PHPUnit_TextUI_Command' ) ) {
206 echo "PHPUnit not found. Please install it and other dev dependencies by
207 running `composer install` in MediaWiki root directory.\n";
211 echo defined( 'HHVM_VERSION' ) ?
212 'Using HHVM ' . HHVM_VERSION
. ' (' . PHP_VERSION
. ")\n" :
213 'Using PHP ' . PHP_VERSION
. "\n";
215 PHPUnit_TextUI_Command
::main();