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