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.',
45 $this->addOption( 'regex', 'Only run parser tests that match the given regex.', false, true );
46 $this->addOption( 'file', 'File describing parser tests.', false, true );
47 $this->addOption( 'use-filebackend', 'Use filebackend', false, true );
48 $this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
49 $this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
50 $this->addOption( 'keep-uploads', 'Re-use the same upload directory for each test, don\'t delete it.', false, false );
51 $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
52 $this->addOption( 'reuse-db', 'Init DB only if tables are missing and keep after finish.', false, false );
55 public function finalSetup() {
58 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType;
59 global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
60 global $wgLocaltimezone, $wgLocalisationCacheConf;
61 global $wgDevelopmentWarnings;
63 // Inject test autoloader
64 require_once __DIR__
. '/../TestsAutoLoader.php';
66 // wfWarn should cause tests to fail
67 $wgDevelopmentWarnings = true;
69 $wgMainCacheType = CACHE_NONE
;
70 $wgMessageCacheType = CACHE_NONE
;
71 $wgParserCacheType = CACHE_NONE
;
72 $wgLanguageConverterCacheType = CACHE_NONE
;
74 $wgUseDatabaseMessages = false; # Set for future resets
76 // Assume UTC for testing purposes
77 $wgLocaltimezone = 'UTC';
79 $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
81 // Bug 44192 Do not attempt to send a real e-mail
82 Hooks
::clear( 'AlternateUserMailer' );
84 'AlternateUserMailer',
89 // xdebug's default of 100 is too low for MediaWiki
90 ini_set( 'xdebug.max_nesting_level', 1000 );
93 public function execute() {
96 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
98 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
99 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
100 restore_error_handler();
102 $this->forceFormatServerArgv();
104 # Make sure we have --configuration or PHPUnit might complain
105 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
106 //Hack to eliminate the need to use the Makefile (which sucks ATM)
107 array_splice( $_SERVER['argv'], 1, 0,
108 array( '--configuration', $IP . '/tests/phpunit/suite.xml' ) );
111 # --with-phpunitdir let us override the default PHPUnit version
112 # Can use with either or phpunit.phar in the directory or the
113 # full PHPUnit code base.
114 if ( $this->hasOption( 'with-phpunitdir' ) ) {
115 $phpunitDir = $this->getOption( 'with-phpunitdir' );
117 # prepends provided PHPUnit directory or phar
118 $this->output( "Will attempt loading PHPUnit from `$phpunitDir`\n" );
119 set_include_path( $phpunitDir . PATH_SEPARATOR
. get_include_path() );
121 # Cleanup $args array so the option and its value do not
123 $key = array_search( '--with-phpunitdir', $_SERVER['argv'] );
124 unset( $_SERVER['argv'][$key] ); // the option
125 unset( $_SERVER['argv'][$key +
1] ); // its value
126 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
129 if ( !wfIsWindows() ) {
130 # If we are not running on windows then we can enable phpunit colors
131 # Windows does not come anymore with ANSI.SYS loaded by default
132 # PHPUnit uses the suite.xml parameters to enable/disable colors
133 # which can be then forced to be enabled with --colors.
134 # The below code injects a parameter just like if the user called
135 # Probably fix bug 29226
136 $key = array_search( '--colors', $_SERVER['argv'] );
137 if ( $key === false ) {
138 array_splice( $_SERVER['argv'], 1, 0, '--colors' );
142 # Makes MediaWiki PHPUnit directory includable so the PHPUnit will
143 # be able to resolve relative files inclusion such as suites/*
144 # PHPUnit uses stream_resolve_include_path() internally
146 $key = array_search( '--include-path', $_SERVER['argv'] );
147 if ( $key === false ) {
148 array_splice( $_SERVER['argv'], 1, 0,
153 array_splice( $_SERVER['argv'], 1, 0, '--include-path' );
156 $key = array_search( '--debug-tests', $_SERVER['argv'] );
157 if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
158 unset( $_SERVER['argv'][$key] );
159 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
160 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
163 foreach ( self
::$additionalOptions as $option => $default ) {
164 $key = array_search( '--' . $option, $_SERVER['argv'] );
165 if ( $key !== false ) {
166 unset( $_SERVER['argv'][$key] );
167 if ( $this->mParams
[$option]['withArg'] ) {
168 self
::$additionalOptions[$option] = $_SERVER['argv'][$key +
1];
169 unset( $_SERVER['argv'][$key +
1] );
171 self
::$additionalOptions[$option] = true;
178 public function getDbType() {
179 return Maintenance
::DB_ADMIN
;
183 * Force the format of elements in $_SERVER['argv']
184 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
186 private function forceFormatServerArgv() {
188 foreach ( $_SERVER['argv'] as $key => $arg ) {
191 } elseif ( strstr( $arg, '=' ) ) {
192 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
199 $_SERVER['argv'] = $argv;
204 $maintClass = 'PHPUnitMaintClass';
205 require RUN_MAINTENANCE_IF_MAIN
;
207 // Prevent segfault when we have lots of unit tests (bug 62623)
208 if ( version_compare( PHP_VERSION
, '5.4.0', '<' ) ) {
209 register_shutdown_function( function () {
219 stream_resolve_include_path( 'phpunit.phar' ),
220 'PHPUnit/Runner/Version.php',
221 'PHPUnit/Autoload.php'
222 ) as $includePath ) {
223 @include_once
$includePath;
224 if ( class_exists( 'PHPUnit_TextUI_Command' ) ) {
231 die( "Couldn't find a usable PHPUnit.\n" );
234 $puVersion = PHPUnit_Runner_Version
::id();
235 if ( $puVersion !== '@package_version@' && version_compare( $puVersion, '3.7.0', '<' ) ) {
236 die( "PHPUnit 3.7.0 or later required; you have {$puVersion}.\n" );
239 PHPUnit_TextUI_Command
::main();