Merge "Revert "Add type hint against LinkTarget""
[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 = [
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 $wgMainStash;
74 global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
75 global $wgLocaltimezone, $wgLocalisationCacheConf;
76 global $wgDevelopmentWarnings;
77 global $wgSessionProviders;
78 global $wgJobTypeConf;
79
80 // Inject test autoloader
81 require_once __DIR__ . '/../TestsAutoLoader.php';
82
83 // wfWarn should cause tests to fail
84 $wgDevelopmentWarnings = true;
85
86 // Make sure all caches and stashes are either disabled or use
87 // in-process cache only to prevent tests from using any preconfigured
88 // cache meant for the local wiki from outside the test run.
89 // See also MediaWikiTestCase::run() which mocks CACHE_DB and APC.
90
91 // Disabled in DefaultSettings, override local settings
92 $wgMainWANCache =
93 $wgMainCacheType = CACHE_NONE;
94 // Uses CACHE_ANYTHING in DefaultSettings, use hash instead of db
95 $wgMessageCacheType =
96 $wgParserCacheType =
97 $wgSessionCacheType =
98 $wgLanguageConverterCacheType = 'hash';
99 // Uses db-replicated in DefaultSettings
100 $wgMainStash = 'hash';
101 // Use memory job queue
102 $wgJobTypeConf = [
103 'default' => [ 'class' => 'JobQueueMemory', 'order' => 'fifo' ],
104 ];
105
106 $wgUseDatabaseMessages = false; # Set for future resets
107
108 // Assume UTC for testing purposes
109 $wgLocaltimezone = 'UTC';
110
111 $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
112
113 // Generic MediaWiki\Session\SessionManager configuration for tests
114 // We use CookieSessionProvider because things might be expecting
115 // cookies to show up in a FauxRequest somewhere.
116 $wgSessionProviders = [
117 [
118 'class' => 'MediaWiki\\Session\\CookieSessionProvider',
119 'args' => [ [
120 'priority' => 30,
121 'callUserSetCookiesHook' => true,
122 ] ],
123 ],
124 ];
125
126 // Bug 44192 Do not attempt to send a real e-mail
127 Hooks::clear( 'AlternateUserMailer' );
128 Hooks::register(
129 'AlternateUserMailer',
130 function () {
131 return false;
132 }
133 );
134 // xdebug's default of 100 is too low for MediaWiki
135 ini_set( 'xdebug.max_nesting_level', 1000 );
136
137 // Bug T116683 serialize_precision of 100
138 // may break testing against floating point values
139 // treated with PHP's serialize()
140 ini_set( 'serialize_precision', 17 );
141 }
142
143 public function execute() {
144 global $IP;
145
146 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
147 // stays in tact.
148 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
149 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
150 restore_error_handler();
151
152 $this->forceFormatServerArgv();
153
154 # Make sure we have --configuration or PHPUnit might complain
155 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
156 // Hack to eliminate the need to use the Makefile (which sucks ATM)
157 array_splice( $_SERVER['argv'], 1, 0,
158 [ '--configuration', $IP . '/tests/phpunit/suite.xml' ] );
159 }
160
161 # --with-phpunitdir let us override the default PHPUnit version
162 # Can use with either or phpunit.phar in the directory or the
163 # full PHPUnit code base.
164 if ( $this->hasOption( 'with-phpunitdir' ) ) {
165 $phpunitDir = $this->getOption( 'with-phpunitdir' );
166
167 # prepends provided PHPUnit directory or phar
168 $this->output( "Will attempt loading PHPUnit from `$phpunitDir`\n" );
169 set_include_path( $phpunitDir . PATH_SEPARATOR . get_include_path() );
170
171 # Cleanup $args array so the option and its value do not
172 # pollute PHPUnit
173 $key = array_search( '--with-phpunitdir', $_SERVER['argv'] );
174 unset( $_SERVER['argv'][$key] ); // the option
175 unset( $_SERVER['argv'][$key + 1] ); // its value
176 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
177 }
178
179 # Makes MediaWiki PHPUnit directory includable so the PHPUnit will
180 # be able to resolve relative files inclusion such as suites/*
181 # PHPUnit uses stream_resolve_include_path() internally
182 # See bug 32022
183 $key = array_search( '--include-path', $_SERVER['argv'] );
184 if ( $key === false ) {
185 array_splice( $_SERVER['argv'], 1, 0,
186 __DIR__
187 . PATH_SEPARATOR
188 . get_include_path()
189 );
190 array_splice( $_SERVER['argv'], 1, 0, '--include-path' );
191 }
192
193 $key = array_search( '--debug-tests', $_SERVER['argv'] );
194 if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
195 unset( $_SERVER['argv'][$key] );
196 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
197 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
198 }
199
200 foreach ( self::$additionalOptions as $option => $default ) {
201 $key = array_search( '--' . $option, $_SERVER['argv'] );
202 if ( $key !== false ) {
203 unset( $_SERVER['argv'][$key] );
204 if ( $this->mParams[$option]['withArg'] ) {
205 self::$additionalOptions[$option] = $_SERVER['argv'][$key + 1];
206 unset( $_SERVER['argv'][$key + 1] );
207 } else {
208 self::$additionalOptions[$option] = true;
209 }
210 }
211 }
212
213 }
214
215 public function getDbType() {
216 return Maintenance::DB_ADMIN;
217 }
218
219 /**
220 * Force the format of elements in $_SERVER['argv']
221 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
222 */
223 private function forceFormatServerArgv() {
224 $argv = [];
225 foreach ( $_SERVER['argv'] as $key => $arg ) {
226 if ( $key === 0 ) {
227 $argv[0] = $arg;
228 } elseif ( strstr( $arg, '=' ) ) {
229 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
230 $argv[] = $argPart;
231 }
232 } else {
233 $argv[] = $arg;
234 }
235 }
236 $_SERVER['argv'] = $argv;
237 }
238
239 }
240
241 $maintClass = 'PHPUnitMaintClass';
242 require RUN_MAINTENANCE_IF_MAIN;
243
244 $ok = false;
245
246 if ( class_exists( 'PHPUnit_TextUI_Command' ) ) {
247 echo "PHPUnit already present\n";
248 $ok = true;
249 } else {
250 foreach ( [
251 stream_resolve_include_path( 'phpunit.phar' ),
252 stream_resolve_include_path( 'phpunit-old.phar' ),
253 'PHPUnit/Runner/Version.php',
254 'PHPUnit/Autoload.php'
255 ] as $includePath ) {
256
257 if ( $includePath === false ) {
258 // stream_resolve_include_path can return false
259 continue;
260 }
261
262 \MediaWiki\suppressWarnings();
263 include_once $includePath;
264 \MediaWiki\restoreWarnings();
265 if ( class_exists( 'PHPUnit_TextUI_Command' ) ) {
266 $ok = true;
267 echo "Using PHPUnit from $includePath\n";
268 break;
269 }
270 }
271 }
272
273 if ( !$ok ) {
274 echo "Couldn't find a usable PHPUnit.\n";
275 exit( 1 );
276 }
277
278 $puVersion = PHPUnit_Runner_Version::id();
279 if ( $puVersion !== '@package_version@' && version_compare( $puVersion, '3.7.0', '<' ) ) {
280 echo "PHPUnit 3.7.0 or later required; you have {$puVersion}.\n";
281 exit( 1 );
282 }
283
284 echo defined( 'HHVM_VERSION' ) ?
285 'Using HHVM ' . HHVM_VERSION . ' (' . PHP_VERSION . ")\n" :
286 'Using PHP ' . PHP_VERSION . "\n";
287
288 PHPUnit_TextUI_Command::main();