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