Merge "Remove arbitrary newSequentialPerNodeIDs() $count limit"
[lhc/web/wiklou.git] / tests / phpunit / phpunit.php
1 #!/usr/bin/env php
2 <?php
3 /**
4 * Bootstrapping for MediaWiki PHPUnit tests
5 *
6 * @file
7 */
8
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 );
12
13 // Start up MediaWiki in command-line mode
14 require_once dirname( dirname( __DIR__ ) ) . "/maintenance/Maintenance.php";
15
16 class PHPUnitMaintClass extends Maintenance {
17
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,
28 );
29
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
38 );
39 $this->addOption(
40 'debug-tests',
41 'Log testing activity to the PHPUnitCommand log channel.',
42 false, # not required
43 false # no arg needed
44 );
45 $this->addOption(
46 'regex',
47 'Only run parser tests that match the given regex.',
48 false,
49 true
50 );
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 );
55 $this->addOption(
56 'keep-uploads',
57 'Re-use the same upload directory for each test, don\'t delete it.',
58 false,
59 false
60 );
61 $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
62 $this->addOption(
63 'reuse-db', 'Init DB only if tables are missing and keep after finish.',
64 false,
65 false
66 );
67 }
68
69 public function finalSetup() {
70 parent::finalSetup();
71
72 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType, $wgMainWANCache;
73 global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
74 global $wgLocaltimezone, $wgLocalisationCacheConf;
75 global $wgDevelopmentWarnings;
76
77 // Inject test autoloader
78 require_once __DIR__ . '/../TestsAutoLoader.php';
79
80 // wfWarn should cause tests to fail
81 $wgDevelopmentWarnings = true;
82
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.
87
88 // Disabled in DefaultSettings, override local settings
89 $wgMainWANCache =
90 $wgMainCacheType = CACHE_NONE;
91 // Uses CACHE_ANYTHING in DefaultSettings, use hash instead of db
92 $wgMessageCacheType =
93 $wgParserCacheType =
94 $wgSessionCacheType =
95 $wgLanguageConverterCacheType = 'hash';
96 // Uses db-replicated in DefaultSettings
97 $wgMainStash = 'hash';
98
99 $wgUseDatabaseMessages = false; # Set for future resets
100
101 // Assume UTC for testing purposes
102 $wgLocaltimezone = 'UTC';
103
104 $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
105
106 // Bug 44192 Do not attempt to send a real e-mail
107 Hooks::clear( 'AlternateUserMailer' );
108 Hooks::register(
109 'AlternateUserMailer',
110 function () {
111 return false;
112 }
113 );
114 // xdebug's default of 100 is too low for MediaWiki
115 ini_set( 'xdebug.max_nesting_level', 1000 );
116
117 // Bug T116683 serialize_precision of 100
118 // may break testing against floating point values
119 // treated with PHP's serialize()
120 ini_set( 'serialize_precision', 17 );
121 }
122
123 public function execute() {
124 global $IP;
125
126 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
127 // stays in tact.
128 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
129 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
130 restore_error_handler();
131
132 $this->forceFormatServerArgv();
133
134 # Make sure we have --configuration or PHPUnit might complain
135 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
136 // Hack to eliminate the need to use the Makefile (which sucks ATM)
137 array_splice( $_SERVER['argv'], 1, 0,
138 array( '--configuration', $IP . '/tests/phpunit/suite.xml' ) );
139 }
140
141 # --with-phpunitdir let us override the default PHPUnit version
142 # Can use with either or phpunit.phar in the directory or the
143 # full PHPUnit code base.
144 if ( $this->hasOption( 'with-phpunitdir' ) ) {
145 $phpunitDir = $this->getOption( 'with-phpunitdir' );
146
147 # prepends provided PHPUnit directory or phar
148 $this->output( "Will attempt loading PHPUnit from `$phpunitDir`\n" );
149 set_include_path( $phpunitDir . PATH_SEPARATOR . get_include_path() );
150
151 # Cleanup $args array so the option and its value do not
152 # pollute PHPUnit
153 $key = array_search( '--with-phpunitdir', $_SERVER['argv'] );
154 unset( $_SERVER['argv'][$key] ); // the option
155 unset( $_SERVER['argv'][$key + 1] ); // its value
156 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
157 }
158
159 if ( !wfIsWindows() ) {
160 # If we are not running on windows then we can enable phpunit colors
161 # Windows does not come anymore with ANSI.SYS loaded by default
162 # PHPUnit uses the suite.xml parameters to enable/disable colors
163 # which can be then forced to be enabled with --colors.
164 # The below code injects a parameter just like if the user called
165 # Probably fix bug 29226
166 $key = array_search( '--colors', $_SERVER['argv'] );
167 if ( $key === false ) {
168 array_splice( $_SERVER['argv'], 1, 0, '--colors' );
169 }
170 }
171
172 # Makes MediaWiki PHPUnit directory includable so the PHPUnit will
173 # be able to resolve relative files inclusion such as suites/*
174 # PHPUnit uses stream_resolve_include_path() internally
175 # See bug 32022
176 $key = array_search( '--include-path', $_SERVER['argv'] );
177 if ( $key === false ) {
178 array_splice( $_SERVER['argv'], 1, 0,
179 __DIR__
180 . PATH_SEPARATOR
181 . get_include_path()
182 );
183 array_splice( $_SERVER['argv'], 1, 0, '--include-path' );
184 }
185
186 $key = array_search( '--debug-tests', $_SERVER['argv'] );
187 if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
188 unset( $_SERVER['argv'][$key] );
189 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
190 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
191 }
192
193 foreach ( self::$additionalOptions as $option => $default ) {
194 $key = array_search( '--' . $option, $_SERVER['argv'] );
195 if ( $key !== false ) {
196 unset( $_SERVER['argv'][$key] );
197 if ( $this->mParams[$option]['withArg'] ) {
198 self::$additionalOptions[$option] = $_SERVER['argv'][$key + 1];
199 unset( $_SERVER['argv'][$key + 1] );
200 } else {
201 self::$additionalOptions[$option] = true;
202 }
203 }
204 }
205
206 }
207
208 public function getDbType() {
209 return Maintenance::DB_ADMIN;
210 }
211
212 /**
213 * Force the format of elements in $_SERVER['argv']
214 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
215 */
216 private function forceFormatServerArgv() {
217 $argv = array();
218 foreach ( $_SERVER['argv'] as $key => $arg ) {
219 if ( $key === 0 ) {
220 $argv[0] = $arg;
221 } elseif ( strstr( $arg, '=' ) ) {
222 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
223 $argv[] = $argPart;
224 }
225 } else {
226 $argv[] = $arg;
227 }
228 }
229 $_SERVER['argv'] = $argv;
230 }
231
232 }
233
234 $maintClass = 'PHPUnitMaintClass';
235 require RUN_MAINTENANCE_IF_MAIN;
236
237 // Prevent segfault when we have lots of unit tests (bug 62623)
238 if ( version_compare( PHP_VERSION, '5.4.0', '<' ) ) {
239 register_shutdown_function( function () {
240 gc_collect_cycles();
241 gc_disable();
242 } );
243 }
244
245
246 $ok = false;
247
248 if ( class_exists( 'PHPUnit_TextUI_Command' ) ) {
249 echo "PHPUnit already present\n";
250 $ok = true;
251 } else {
252 foreach ( array(
253 stream_resolve_include_path( 'phpunit.phar' ),
254 'PHPUnit/Runner/Version.php',
255 'PHPUnit/Autoload.php'
256 ) as $includePath ) {
257 // @codingStandardsIgnoreStart
258 @include_once $includePath;
259 // @codingStandardsIgnoreEnd
260 if ( class_exists( 'PHPUnit_TextUI_Command' ) ) {
261 $ok = true;
262 echo "Using PHPUnit from $includePath\n";
263 break;
264 }
265 }
266 }
267
268 if ( !$ok ) {
269 echo "Couldn't find a usable PHPUnit.\n";
270 exit( 1 );
271 }
272
273 $puVersion = PHPUnit_Runner_Version::id();
274 if ( $puVersion !== '@package_version@' && version_compare( $puVersion, '3.7.0', '<' ) ) {
275 echo "PHPUnit 3.7.0 or later required; you have {$puVersion}.\n";
276 exit( 1 );
277 }
278
279 PHPUnit_TextUI_Command::main();