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 = array(
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 'Directory to include PHPUnit from, for example when using a git '
35 . 'fetchout from upstream. Path will be prepended to PHP `include_path`.',
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;
79 // Inject test autoloader
80 require_once __DIR__
. '/../TestsAutoLoader.php';
82 // wfWarn should cause tests to fail
83 $wgDevelopmentWarnings = true;
85 // Make sure all caches and stashes are either disabled or use
86 // in-process cache only to prevent tests from using any preconfigured
87 // cache meant for the local wiki from outside the test run.
88 // See also MediaWikiTestCase::run() which mocks CACHE_DB and APC.
90 // Disabled in DefaultSettings, override local settings
92 $wgMainCacheType = CACHE_NONE
;
93 // Uses CACHE_ANYTHING in DefaultSettings, use hash instead of db
97 $wgLanguageConverterCacheType = 'hash';
98 // Uses db-replicated in DefaultSettings
99 $wgMainStash = 'hash';
101 $wgUseDatabaseMessages = false; # Set for future resets
103 // Assume UTC for testing purposes
104 $wgLocaltimezone = 'UTC';
106 $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
108 // Generic MediaWiki\Session\SessionManager configuration for tests
109 // We use CookieSessionProvider because things might be expecting
110 // cookies to show up in a FauxRequest somewhere.
111 $wgSessionProviders = array(
113 'class' => 'MediaWiki\\Session\\CookieSessionProvider',
114 'args' => array( array(
116 'callUserSetCookiesHook' => true,
121 // Bug 44192 Do not attempt to send a real e-mail
122 Hooks
::clear( 'AlternateUserMailer' );
124 'AlternateUserMailer',
129 // xdebug's default of 100 is too low for MediaWiki
130 ini_set( 'xdebug.max_nesting_level', 1000 );
132 // Bug T116683 serialize_precision of 100
133 // may break testing against floating point values
134 // treated with PHP's serialize()
135 ini_set( 'serialize_precision', 17 );
138 public function execute() {
141 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
143 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
144 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
145 restore_error_handler();
147 $this->forceFormatServerArgv();
149 # Make sure we have --configuration or PHPUnit might complain
150 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
151 // Hack to eliminate the need to use the Makefile (which sucks ATM)
152 array_splice( $_SERVER['argv'], 1, 0,
153 array( '--configuration', $IP . '/tests/phpunit/suite.xml' ) );
156 # --with-phpunitdir let us override the default PHPUnit version
157 # Can use with either or phpunit.phar in the directory or the
158 # full PHPUnit code base.
159 if ( $this->hasOption( 'with-phpunitdir' ) ) {
160 $phpunitDir = $this->getOption( 'with-phpunitdir' );
162 # prepends provided PHPUnit directory or phar
163 $this->output( "Will attempt loading PHPUnit from `$phpunitDir`\n" );
164 set_include_path( $phpunitDir . PATH_SEPARATOR
. get_include_path() );
166 # Cleanup $args array so the option and its value do not
168 $key = array_search( '--with-phpunitdir', $_SERVER['argv'] );
169 unset( $_SERVER['argv'][$key] ); // the option
170 unset( $_SERVER['argv'][$key +
1] ); // its value
171 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
174 if ( !wfIsWindows() ) {
175 # If we are not running on windows then we can enable phpunit colors
176 # Windows does not come anymore with ANSI.SYS loaded by default
177 # PHPUnit uses the suite.xml parameters to enable/disable colors
178 # which can be then forced to be enabled with --colors.
179 # The below code injects a parameter just like if the user called
180 # Probably fix bug 29226
181 $key = array_search( '--colors', $_SERVER['argv'] );
182 if ( $key === false ) {
183 array_splice( $_SERVER['argv'], 1, 0, '--colors' );
187 # Makes MediaWiki PHPUnit directory includable so the PHPUnit will
188 # be able to resolve relative files inclusion such as suites/*
189 # PHPUnit uses stream_resolve_include_path() internally
191 $key = array_search( '--include-path', $_SERVER['argv'] );
192 if ( $key === false ) {
193 array_splice( $_SERVER['argv'], 1, 0,
198 array_splice( $_SERVER['argv'], 1, 0, '--include-path' );
201 $key = array_search( '--debug-tests', $_SERVER['argv'] );
202 if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
203 unset( $_SERVER['argv'][$key] );
204 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
205 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
208 foreach ( self
::$additionalOptions as $option => $default ) {
209 $key = array_search( '--' . $option, $_SERVER['argv'] );
210 if ( $key !== false ) {
211 unset( $_SERVER['argv'][$key] );
212 if ( $this->mParams
[$option]['withArg'] ) {
213 self
::$additionalOptions[$option] = $_SERVER['argv'][$key +
1];
214 unset( $_SERVER['argv'][$key +
1] );
216 self
::$additionalOptions[$option] = true;
223 public function getDbType() {
224 return Maintenance
::DB_ADMIN
;
228 * Force the format of elements in $_SERVER['argv']
229 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
231 private function forceFormatServerArgv() {
233 foreach ( $_SERVER['argv'] as $key => $arg ) {
236 } elseif ( strstr( $arg, '=' ) ) {
237 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
244 $_SERVER['argv'] = $argv;
249 $maintClass = 'PHPUnitMaintClass';
250 require RUN_MAINTENANCE_IF_MAIN
;
252 // Prevent segfault when we have lots of unit tests (bug 62623)
253 if ( version_compare( PHP_VERSION
, '5.4.0', '<' ) ) {
254 register_shutdown_function( function () {
262 if ( class_exists( 'PHPUnit_TextUI_Command' ) ) {
263 echo "PHPUnit already present\n";
267 stream_resolve_include_path( 'phpunit.phar' ),
268 stream_resolve_include_path( 'phpunit-old.phar' ),
269 'PHPUnit/Runner/Version.php',
270 'PHPUnit/Autoload.php'
271 ) as $includePath ) {
273 if ( $includePath === false ) {
274 // stream_resolve_include_path can return false
278 \MediaWiki\
suppressWarnings();
279 include_once $includePath;
280 \MediaWiki\restoreWarnings
();
281 if ( class_exists( 'PHPUnit_TextUI_Command' ) ) {
283 echo "Using PHPUnit from $includePath\n";
290 echo "Couldn't find a usable PHPUnit.\n";
294 $puVersion = PHPUnit_Runner_Version
::id();
295 if ( $puVersion !== '@package_version@' && version_compare( $puVersion, '3.7.0', '<' ) ) {
296 echo "PHPUnit 3.7.0 or later required; you have {$puVersion}.\n";
300 PHPUnit_TextUI_Command
::main();