TitleTest: fix minor todo about test naming
[mediawiki.git] / tests / phpunit / phpunit.php
blob11255043537ffa33aa9b5ea1a1cfcb3636eb0735
1 #!/usr/bin/env php
2 <?php
3 /**
4 * Bootstrapping for MediaWiki PHPUnit tests
6 * @file
7 */
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(
19 'regex' => false,
20 'file' => false,
21 'use-filebackend' => false,
22 'use-bagostuff' => false,
23 'use-jobqueue' => false,
24 'keep-uploads' => false,
25 'use-normal-tables' => false,
26 'reuse-db' => false,
27 'wiki' => false,
30 public function __construct() {
31 parent::__construct();
32 $this->addOption(
33 'with-phpunitdir',
34 'Directory to include PHPUnit from, for example when using a git '
35 . 'fetchout from upstream. Path will be prepended to PHP `include_path`.',
36 false, # not required
37 true # need arg
39 $this->addOption(
40 'debug-tests',
41 'Log testing activity to the PHPUnitCommand log channel.',
42 false, # not required
43 false # no arg needed
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() {
56 parent::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' );
83 Hooks::register(
84 'AlternateUserMailer',
85 function () {
86 return false;
89 // xdebug's default of 100 is too low for MediaWiki
90 ini_set( 'xdebug.max_nesting_level', 1000 );
93 public function execute() {
94 global $IP;
96 $this->forceFormatServerArgv();
98 # Make sure we have --configuration or PHPUnit might complain
99 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
100 //Hack to eliminate the need to use the Makefile (which sucks ATM)
101 array_splice( $_SERVER['argv'], 1, 0,
102 array( '--configuration', $IP . '/tests/phpunit/suite.xml' ) );
105 # --with-phpunitdir let us override the default PHPUnit version
106 # Can use with either or phpunit.phar in the directory or the
107 # full PHPUnit code base.
108 if ( $this->hasOption( 'with-phpunitdir' ) ) {
109 $phpunitDir = $this->getOption( 'with-phpunitdir' );
111 # prepends provided PHPUnit directory or phar
112 $this->output( "Will attempt loading PHPUnit from `$phpunitDir`\n" );
113 set_include_path( $phpunitDir . PATH_SEPARATOR . get_include_path() );
115 # Cleanup $args array so the option and its value do not
116 # pollute PHPUnit
117 $key = array_search( '--with-phpunitdir', $_SERVER['argv'] );
118 unset( $_SERVER['argv'][$key] ); // the option
119 unset( $_SERVER['argv'][$key + 1] ); // its value
120 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
123 if ( !wfIsWindows() ) {
124 # If we are not running on windows then we can enable phpunit colors
125 # Windows does not come anymore with ANSI.SYS loaded by default
126 # PHPUnit uses the suite.xml parameters to enable/disable colors
127 # which can be then forced to be enabled with --colors.
128 # The below code injects a parameter just like if the user called
129 # Probably fix bug 29226
130 $key = array_search( '--colors', $_SERVER['argv'] );
131 if ( $key === false ) {
132 array_splice( $_SERVER['argv'], 1, 0, '--colors' );
136 # Makes MediaWiki PHPUnit directory includable so the PHPUnit will
137 # be able to resolve relative files inclusion such as suites/*
138 # PHPUnit uses stream_resolve_include_path() internally
139 # See bug 32022
140 $key = array_search( '--include-path', $_SERVER['argv'] );
141 if ( $key === false ) {
142 array_splice( $_SERVER['argv'], 1, 0,
143 __DIR__
144 . PATH_SEPARATOR
145 . get_include_path()
147 array_splice( $_SERVER['argv'], 1, 0, '--include-path' );
150 $key = array_search( '--debug-tests', $_SERVER['argv'] );
151 if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
152 unset( $_SERVER['argv'][$key] );
153 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
154 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
157 foreach ( self::$additionalOptions as $option => $default ) {
158 $key = array_search( '--' . $option, $_SERVER['argv'] );
159 if ( $key !== false ) {
160 unset( $_SERVER['argv'][$key] );
161 if ( $this->mParams[$option]['withArg'] ) {
162 self::$additionalOptions[$option] = $_SERVER['argv'][$key + 1];
163 unset( $_SERVER['argv'][$key + 1] );
164 } else {
165 self::$additionalOptions[$option] = true;
172 public function getDbType() {
173 return Maintenance::DB_ADMIN;
177 * Force the format of elements in $_SERVER['argv']
178 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
180 private function forceFormatServerArgv() {
181 $argv = array();
182 foreach ( $_SERVER['argv'] as $key => $arg ) {
183 if ( $key === 0 ) {
184 $argv[0] = $arg;
185 } elseif ( strstr( $arg, '=' ) ) {
186 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
187 $argv[] = $argPart;
189 } else {
190 $argv[] = $arg;
193 $_SERVER['argv'] = $argv;
198 $maintClass = 'PHPUnitMaintClass';
199 require RUN_MAINTENANCE_IF_MAIN;
201 // Prevent segfault when we have lots of unit tests (bug 62623)
202 if ( version_compare( PHP_VERSION, '5.4.0', '<' ) ) {
203 register_shutdown_function( function () {
204 gc_collect_cycles();
205 gc_disable();
206 } );
210 $ok = false;
212 foreach ( array(
213 stream_resolve_include_path( 'phpunit.phar' ),
214 'PHPUnit/Runner/Version.php',
215 'PHPUnit/Autoload.php'
216 ) as $includePath ) {
217 @include_once $includePath;
218 if ( class_exists( 'PHPUnit_TextUI_Command' ) ) {
219 $ok = true;
220 break;
224 if ( !$ok ) {
225 die( "Couldn't find a usable PHPUnit.\n" );
228 $puVersion = PHPUnit_Runner_Version::id();
229 if ( $puVersion !== '@package_version@' && version_compare( $puVersion, '3.7.0', '<' ) ) {
230 die( "PHPUnit 3.7.0 or later required; you have {$puVersion}.\n" );
233 PHPUnit_TextUI_Command::main();