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, $wgSessionPbkdf2Iterations;
79 global $wgJobTypeConf;
80 global $wgAuthManagerConfig, $wgAuth, $wgDisableAuthManager;
82 // Inject test autoloader
83 require_once __DIR__
. '/../TestsAutoLoader.php';
85 // wfWarn should cause tests to fail
86 $wgDevelopmentWarnings = true;
88 // Make sure all caches and stashes are either disabled or use
89 // in-process cache only to prevent tests from using any preconfigured
90 // cache meant for the local wiki from outside the test run.
91 // See also MediaWikiTestCase::run() which mocks CACHE_DB and APC.
93 // Disabled in DefaultSettings, override local settings
95 $wgMainCacheType = CACHE_NONE
;
96 // Uses CACHE_ANYTHING in DefaultSettings, use hash instead of db
100 $wgLanguageConverterCacheType = 'hash';
101 // Uses db-replicated in DefaultSettings
102 $wgMainStash = 'hash';
103 // Use memory job queue
105 'default' => [ 'class' => 'JobQueueMemory', 'order' => 'fifo' ],
108 $wgUseDatabaseMessages = false; # Set for future resets
110 // Assume UTC for testing purposes
111 $wgLocaltimezone = 'UTC';
113 $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
115 // Generic MediaWiki\Session\SessionManager configuration for tests
116 // We use CookieSessionProvider because things might be expecting
117 // cookies to show up in a FauxRequest somewhere.
118 $wgSessionProviders = [
120 'class' => MediaWiki\Session\CookieSessionProvider
::class,
123 'callUserSetCookiesHook' => true,
128 // Single-iteration PBKDF2 session secret derivation, for speed.
129 $wgSessionPbkdf2Iterations = 1;
131 // Generic AuthManager configuration for testing
132 $wgAuthManagerConfig = [
136 'class' => MediaWiki\Auth\TemporaryPasswordPrimaryAuthenticationProvider
::class,
138 'authoritative' => false,
142 'class' => MediaWiki\Auth\LocalPasswordPrimaryAuthenticationProvider
::class,
144 'authoritative' => true,
148 'secondaryauth' => [],
150 $wgAuth = $wgDisableAuthManager ?
new AuthPlugin
: new MediaWiki\Auth\
AuthManagerAuthPlugin();
152 // Bug 44192 Do not attempt to send a real e-mail
153 Hooks
::clear( 'AlternateUserMailer' );
155 'AlternateUserMailer',
160 // xdebug's default of 100 is too low for MediaWiki
161 ini_set( 'xdebug.max_nesting_level', 1000 );
163 // Bug T116683 serialize_precision of 100
164 // may break testing against floating point values
165 // treated with PHP's serialize()
166 ini_set( 'serialize_precision', 17 );
168 // TODO: we should call MediaWikiTestCase::prepareServices( new GlobalVarConfig() ) here.
169 // But PHPUnit may not be loaded yet, so we have to wait until just
170 // before PHPUnit_TextUI_Command::main() is executed at the end of this file.
173 public function execute() {
176 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
178 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
179 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
180 restore_error_handler();
182 $this->forceFormatServerArgv();
184 # Make sure we have --configuration or PHPUnit might complain
185 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
186 // Hack to eliminate the need to use the Makefile (which sucks ATM)
187 array_splice( $_SERVER['argv'], 1, 0,
188 [ '--configuration', $IP . '/tests/phpunit/suite.xml' ] );
191 if ( $this->hasOption( 'with-phpunitclass' ) ) {
192 global $wgPhpUnitClass;
193 $wgPhpUnitClass = $this->getOption( 'with-phpunitclass' );
195 # Cleanup $args array so the option and its value do not
197 $key = array_search( '--with-phpunitclass', $_SERVER['argv'] );
198 unset( $_SERVER['argv'][$key] ); // the option
199 unset( $_SERVER['argv'][$key +
1] ); // its value
200 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
203 $key = array_search( '--debug-tests', $_SERVER['argv'] );
204 if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
205 unset( $_SERVER['argv'][$key] );
206 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
207 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
210 foreach ( self
::$additionalOptions as $option => $default ) {
211 $key = array_search( '--' . $option, $_SERVER['argv'] );
212 if ( $key !== false ) {
213 unset( $_SERVER['argv'][$key] );
214 if ( $this->mParams
[$option]['withArg'] ) {
215 self
::$additionalOptions[$option] = $_SERVER['argv'][$key +
1];
216 unset( $_SERVER['argv'][$key +
1] );
218 self
::$additionalOptions[$option] = true;
225 public function getDbType() {
226 return Maintenance
::DB_ADMIN
;
230 * Force the format of elements in $_SERVER['argv']
231 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
233 private function forceFormatServerArgv() {
235 foreach ( $_SERVER['argv'] as $key => $arg ) {
238 } elseif ( strstr( $arg, '=' ) ) {
239 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
246 $_SERVER['argv'] = $argv;
251 $maintClass = 'PHPUnitMaintClass';
252 require RUN_MAINTENANCE_IF_MAIN
;
254 if ( !class_exists( 'PHPUnit_Framework_TestCase' ) ) {
255 echo "PHPUnit not found. Please install it and other dev dependencies by
256 running `composer install` in MediaWiki root directory.\n";
259 if ( !class_exists( $wgPhpUnitClass ) ) {
260 echo "PHPUnit entry point '" . $wgPhpUnitClass . "' not found. Please make sure you installed
261 the containing component and check the spelling of the class name.\n";
265 echo defined( 'HHVM_VERSION' ) ?
266 'Using HHVM ' . HHVM_VERSION
. ' (' . PHP_VERSION
. ")\n" :
267 'Using PHP ' . PHP_VERSION
. "\n";
269 // Prepare global services for unit tests.
270 // FIXME: this should be done in the finalSetup() method,
271 // but PHPUnit may not have been loaded at that point.
272 MediaWikiTestCase
::prepareServices( new GlobalVarConfig() );
274 $wgPhpUnitClass::main();