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;
73 global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
74 global $wgLocaltimezone, $wgLocalisationCacheConf;
75 global $wgDevelopmentWarnings;
77 // Inject test autoloader
78 require_once __DIR__
. '/../TestsAutoLoader.php';
80 // wfWarn should cause tests to fail
81 $wgDevelopmentWarnings = true;
83 // Make sure all caches and stashes are either disabled or use
84 // in-process cache only to prevent tests from using any preconfigured
85 // cache meant for the local wiki from outside the test run.
86 // See also MediaWikiTestCase::run() which mocks CACHE_DB and APC.
88 // Disabled in DefaultSettings, override local settings
90 $wgMainCacheType = CACHE_NONE
;
91 // Uses CACHE_ANYTHING in DefaultSettings, use hash instead of db
95 $wgLanguageConverterCacheType = 'hash';
96 // Uses db-replicated in DefaultSettings
97 $wgMainStash = 'hash';
99 $wgUseDatabaseMessages = false; # Set for future resets
101 // Assume UTC for testing purposes
102 $wgLocaltimezone = 'UTC';
104 $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
106 // Bug 44192 Do not attempt to send a real e-mail
107 Hooks
::clear( 'AlternateUserMailer' );
109 'AlternateUserMailer',
114 // xdebug's default of 100 is too low for MediaWiki
115 ini_set( 'xdebug.max_nesting_level', 1000 );
118 public function execute() {
121 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
123 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
124 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
125 restore_error_handler();
127 $this->forceFormatServerArgv();
129 # Make sure we have --configuration or PHPUnit might complain
130 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
131 // Hack to eliminate the need to use the Makefile (which sucks ATM)
132 array_splice( $_SERVER['argv'], 1, 0,
133 array( '--configuration', $IP . '/tests/phpunit/suite.xml' ) );
136 # --with-phpunitdir let us override the default PHPUnit version
137 # Can use with either or phpunit.phar in the directory or the
138 # full PHPUnit code base.
139 if ( $this->hasOption( 'with-phpunitdir' ) ) {
140 $phpunitDir = $this->getOption( 'with-phpunitdir' );
142 # prepends provided PHPUnit directory or phar
143 $this->output( "Will attempt loading PHPUnit from `$phpunitDir`\n" );
144 set_include_path( $phpunitDir . PATH_SEPARATOR
. get_include_path() );
146 # Cleanup $args array so the option and its value do not
148 $key = array_search( '--with-phpunitdir', $_SERVER['argv'] );
149 unset( $_SERVER['argv'][$key] ); // the option
150 unset( $_SERVER['argv'][$key +
1] ); // its value
151 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
154 if ( !wfIsWindows() ) {
155 # If we are not running on windows then we can enable phpunit colors
156 # Windows does not come anymore with ANSI.SYS loaded by default
157 # PHPUnit uses the suite.xml parameters to enable/disable colors
158 # which can be then forced to be enabled with --colors.
159 # The below code injects a parameter just like if the user called
160 # Probably fix bug 29226
161 $key = array_search( '--colors', $_SERVER['argv'] );
162 if ( $key === false ) {
163 array_splice( $_SERVER['argv'], 1, 0, '--colors' );
167 # Makes MediaWiki PHPUnit directory includable so the PHPUnit will
168 # be able to resolve relative files inclusion such as suites/*
169 # PHPUnit uses stream_resolve_include_path() internally
171 $key = array_search( '--include-path', $_SERVER['argv'] );
172 if ( $key === false ) {
173 array_splice( $_SERVER['argv'], 1, 0,
178 array_splice( $_SERVER['argv'], 1, 0, '--include-path' );
181 $key = array_search( '--debug-tests', $_SERVER['argv'] );
182 if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
183 unset( $_SERVER['argv'][$key] );
184 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
185 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
188 foreach ( self
::$additionalOptions as $option => $default ) {
189 $key = array_search( '--' . $option, $_SERVER['argv'] );
190 if ( $key !== false ) {
191 unset( $_SERVER['argv'][$key] );
192 if ( $this->mParams
[$option]['withArg'] ) {
193 self
::$additionalOptions[$option] = $_SERVER['argv'][$key +
1];
194 unset( $_SERVER['argv'][$key +
1] );
196 self
::$additionalOptions[$option] = true;
203 public function getDbType() {
204 return Maintenance
::DB_ADMIN
;
208 * Force the format of elements in $_SERVER['argv']
209 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
211 private function forceFormatServerArgv() {
213 foreach ( $_SERVER['argv'] as $key => $arg ) {
216 } elseif ( strstr( $arg, '=' ) ) {
217 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
224 $_SERVER['argv'] = $argv;
229 $maintClass = 'PHPUnitMaintClass';
230 require RUN_MAINTENANCE_IF_MAIN
;
232 // Prevent segfault when we have lots of unit tests (bug 62623)
233 if ( version_compare( PHP_VERSION
, '5.4.0', '<' ) ) {
234 register_shutdown_function( function () {
243 if ( class_exists( 'PHPUnit_TextUI_Command' ) ) {
244 echo "PHPUnit already present\n";
248 stream_resolve_include_path( 'phpunit.phar' ),
249 'PHPUnit/Runner/Version.php',
250 'PHPUnit/Autoload.php'
251 ) as $includePath ) {
252 // @codingStandardsIgnoreStart
253 @include_once
$includePath;
254 // @codingStandardsIgnoreEnd
255 if ( class_exists( 'PHPUnit_TextUI_Command' ) ) {
257 echo "Using PHPUnit from $includePath\n";
264 echo "Couldn't find a usable PHPUnit.\n";
268 $puVersion = PHPUnit_Runner_Version
::id();
269 if ( $puVersion !== '@package_version@' && version_compare( $puVersion, '3.7.0', '<' ) ) {
270 echo "PHPUnit 3.7.0 or later required; you have {$puVersion}.\n";
274 PHPUnit_TextUI_Command
::main();