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,
31 public function __construct() {
32 parent
::__construct();
35 'Class name of the PHPUnit entry point to use',
41 'Log testing activity to the PHPUnitCommand log channel.',
47 'Only run parser tests that match the given regex.',
51 $this->addOption( 'file', 'File describing parser tests.', false, true );
52 $this->addOption( 'use-filebackend', 'Use filebackend', false, true );
53 $this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
54 $this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
57 'Re-use the same upload directory for each test, don\'t delete it.',
61 $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
63 'reuse-db', 'Init DB only if tables are missing and keep after finish.',
69 public function finalSetup() {
72 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType, $wgMainWANCache;
74 global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
75 global $wgLocaltimezone, $wgLocalisationCacheConf;
76 global $wgDevelopmentWarnings;
77 global $wgSessionProviders, $wgSessionPbkdf2Iterations;
78 global $wgJobTypeConf;
79 global $wgAuthManagerConfig, $wgAuth;
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 // Single-iteration PBKDF2 session secret derivation, for speed.
128 $wgSessionPbkdf2Iterations = 1;
130 // Generic AuthManager configuration for testing
131 $wgAuthManagerConfig = [
135 'class' => MediaWiki\Auth\TemporaryPasswordPrimaryAuthenticationProvider
::class,
137 'authoritative' => false,
141 'class' => MediaWiki\Auth\LocalPasswordPrimaryAuthenticationProvider
::class,
143 'authoritative' => true,
147 'secondaryauth' => [],
149 $wgAuth = new MediaWiki\Auth\
AuthManagerAuthPlugin();
151 // Bug 44192 Do not attempt to send a real e-mail
152 Hooks
::clear( 'AlternateUserMailer' );
154 'AlternateUserMailer',
159 // xdebug's default of 100 is too low for MediaWiki
160 ini_set( 'xdebug.max_nesting_level', 1000 );
162 // Bug T116683 serialize_precision of 100
163 // may break testing against floating point values
164 // treated with PHP's serialize()
165 ini_set( 'serialize_precision', 17 );
167 // TODO: we should call MediaWikiTestCase::prepareServices( new GlobalVarConfig() ) here.
168 // But PHPUnit may not be loaded yet, so we have to wait until just
169 // before PHPUnit_TextUI_Command::main() is executed.
172 public function execute() {
175 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
177 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
178 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
179 restore_error_handler();
181 $this->forceFormatServerArgv();
183 # Make sure we have --configuration or PHPUnit might complain
184 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
185 // Hack to eliminate the need to use the Makefile (which sucks ATM)
186 array_splice( $_SERVER['argv'], 1, 0,
187 [ '--configuration', $IP . '/tests/phpunit/suite.xml' ] );
190 $phpUnitClass = 'PHPUnit_TextUI_Command';
192 if ( $this->hasOption( 'with-phpunitclass' ) ) {
193 $phpUnitClass = $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;
223 if ( !class_exists( 'PHPUnit_Framework_TestCase' ) ) {
224 echo "PHPUnit not found. Please install it and other dev dependencies by
225 running `composer install` in MediaWiki root directory.\n";
228 if ( !class_exists( $phpUnitClass ) ) {
229 echo "PHPUnit entry point '" . $phpUnitClass . "' not found. Please make sure you installed
230 the containing component and check the spelling of the class name.\n";
234 echo defined( 'HHVM_VERSION' ) ?
235 'Using HHVM ' . HHVM_VERSION
. ' (' . PHP_VERSION
. ")\n" :
236 'Using PHP ' . PHP_VERSION
. "\n";
238 // Prepare global services for unit tests.
239 MediaWikiTestCase
::prepareServices( new GlobalVarConfig() );
241 $phpUnitClass::main();
244 public function getDbType() {
245 return Maintenance
::DB_ADMIN
;
249 * Force the format of elements in $_SERVER['argv']
250 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
252 private function forceFormatServerArgv() {
254 foreach ( $_SERVER['argv'] as $key => $arg ) {
257 } elseif ( strstr( $arg, '=' ) ) {
258 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
265 $_SERVER['argv'] = $argv;
270 $maintClass = 'PHPUnitMaintClass';
271 require RUN_MAINTENANCE_IF_MAIN
;