Allow --profile to be used in phpunit.php
[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 'profiler' => false,
29 ];
30
31 public function __construct() {
32 parent::__construct();
33 $this->addOption(
34 'with-phpunitclass',
35 'Class name of the PHPUnit entry point to use',
36 false,
37 true
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, $wgSessionPbkdf2Iterations;
78 global $wgJobTypeConf;
79 global $wgAuthManagerConfig, $wgAuth;
80
81 // Inject test autoloader
82 require_once __DIR__ . '/../TestsAutoLoader.php';
83
84 // wfWarn should cause tests to fail
85 $wgDevelopmentWarnings = true;
86
87 // Make sure all caches and stashes are either disabled or use
88 // in-process cache only to prevent tests from using any preconfigured
89 // cache meant for the local wiki from outside the test run.
90 // See also MediaWikiTestCase::run() which mocks CACHE_DB and APC.
91
92 // Disabled in DefaultSettings, override local settings
93 $wgMainWANCache =
94 $wgMainCacheType = CACHE_NONE;
95 // Uses CACHE_ANYTHING in DefaultSettings, use hash instead of db
96 $wgMessageCacheType =
97 $wgParserCacheType =
98 $wgSessionCacheType =
99 $wgLanguageConverterCacheType = 'hash';
100 // Uses db-replicated in DefaultSettings
101 $wgMainStash = 'hash';
102 // Use memory job queue
103 $wgJobTypeConf = [
104 'default' => [ 'class' => 'JobQueueMemory', 'order' => 'fifo' ],
105 ];
106
107 $wgUseDatabaseMessages = false; # Set for future resets
108
109 // Assume UTC for testing purposes
110 $wgLocaltimezone = 'UTC';
111
112 $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
113
114 // Generic MediaWiki\Session\SessionManager configuration for tests
115 // We use CookieSessionProvider because things might be expecting
116 // cookies to show up in a FauxRequest somewhere.
117 $wgSessionProviders = [
118 [
119 'class' => MediaWiki\Session\CookieSessionProvider::class,
120 'args' => [ [
121 'priority' => 30,
122 'callUserSetCookiesHook' => true,
123 ] ],
124 ],
125 ];
126
127 // Single-iteration PBKDF2 session secret derivation, for speed.
128 $wgSessionPbkdf2Iterations = 1;
129
130 // Generic AuthManager configuration for testing
131 $wgAuthManagerConfig = [
132 'preauth' => [],
133 'primaryauth' => [
134 [
135 'class' => MediaWiki\Auth\TemporaryPasswordPrimaryAuthenticationProvider::class,
136 'args' => [ [
137 'authoritative' => false,
138 ] ],
139 ],
140 [
141 'class' => MediaWiki\Auth\LocalPasswordPrimaryAuthenticationProvider::class,
142 'args' => [ [
143 'authoritative' => true,
144 ] ],
145 ],
146 ],
147 'secondaryauth' => [],
148 ];
149 $wgAuth = new MediaWiki\Auth\AuthManagerAuthPlugin();
150
151 // Bug 44192 Do not attempt to send a real e-mail
152 Hooks::clear( 'AlternateUserMailer' );
153 Hooks::register(
154 'AlternateUserMailer',
155 function () {
156 return false;
157 }
158 );
159 // xdebug's default of 100 is too low for MediaWiki
160 ini_set( 'xdebug.max_nesting_level', 1000 );
161
162 // Bug T116683 serialize_precision of 100
163 // may break testing against floating point values
164 // treated with PHP's serialize()
165 ini_set( 'serialize_precision', 17 );
166
167 // TODO: we should call MediaWikiTestCase::prepareServices( new GlobalVarConfig() ) here.
168 // But PHPUnit may not be loaded yet, so we have to wait until just
169 // before PHPUnit_TextUI_Command::main() is executed.
170 }
171
172 public function execute() {
173 global $IP;
174
175 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
176 // stays in tact.
177 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
178 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
179 restore_error_handler();
180
181 $this->forceFormatServerArgv();
182
183 # Make sure we have --configuration or PHPUnit might complain
184 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
185 // Hack to eliminate the need to use the Makefile (which sucks ATM)
186 array_splice( $_SERVER['argv'], 1, 0,
187 [ '--configuration', $IP . '/tests/phpunit/suite.xml' ] );
188 }
189
190 $phpUnitClass = 'PHPUnit_TextUI_Command';
191
192 if ( $this->hasOption( 'with-phpunitclass' ) ) {
193 $phpUnitClass = $this->getOption( 'with-phpunitclass' );
194
195 # Cleanup $args array so the option and its value do not
196 # pollute PHPUnit
197 $key = array_search( '--with-phpunitclass', $_SERVER['argv'] );
198 unset( $_SERVER['argv'][$key] ); // the option
199 unset( $_SERVER['argv'][$key + 1] ); // its value
200 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
201 }
202
203 $key = array_search( '--debug-tests', $_SERVER['argv'] );
204 if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
205 unset( $_SERVER['argv'][$key] );
206 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
207 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
208 }
209
210 foreach ( self::$additionalOptions as $option => $default ) {
211 $key = array_search( '--' . $option, $_SERVER['argv'] );
212 if ( $key !== false ) {
213 unset( $_SERVER['argv'][$key] );
214 if ( $this->mParams[$option]['withArg'] ) {
215 self::$additionalOptions[$option] = $_SERVER['argv'][$key + 1];
216 unset( $_SERVER['argv'][$key + 1] );
217 } else {
218 self::$additionalOptions[$option] = true;
219 }
220 }
221 }
222
223 if ( !class_exists( 'PHPUnit_Framework_TestCase' ) ) {
224 echo "PHPUnit not found. Please install it and other dev dependencies by
225 running `composer install` in MediaWiki root directory.\n";
226 exit( 1 );
227 }
228 if ( !class_exists( $phpUnitClass ) ) {
229 echo "PHPUnit entry point '" . $phpUnitClass . "' not found. Please make sure you installed
230 the containing component and check the spelling of the class name.\n";
231 exit( 1 );
232 }
233
234 echo defined( 'HHVM_VERSION' ) ?
235 'Using HHVM ' . HHVM_VERSION . ' (' . PHP_VERSION . ")\n" :
236 'Using PHP ' . PHP_VERSION . "\n";
237
238 // Prepare global services for unit tests.
239 MediaWikiTestCase::prepareServices( new GlobalVarConfig() );
240
241 $phpUnitClass::main();
242 }
243
244 public function getDbType() {
245 return Maintenance::DB_ADMIN;
246 }
247
248 /**
249 * Force the format of elements in $_SERVER['argv']
250 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
251 */
252 private function forceFormatServerArgv() {
253 $argv = [];
254 foreach ( $_SERVER['argv'] as $key => $arg ) {
255 if ( $key === 0 ) {
256 $argv[0] = $arg;
257 } elseif ( strstr( $arg, '=' ) ) {
258 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
259 $argv[] = $argPart;
260 }
261 } else {
262 $argv[] = $arg;
263 }
264 }
265 $_SERVER['argv'] = $argv;
266 }
267
268 }
269
270 $maintClass = 'PHPUnitMaintClass';
271 require RUN_MAINTENANCE_IF_MAIN;