Merge "Fix missing variable in HTMLFormField exceptions"
[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 $wgPhpUnitClass = 'PHPUnit_TextUI_Command';
14
15 // Start up MediaWiki in command-line mode
16 require_once dirname( dirname( __DIR__ ) ) . "/maintenance/Maintenance.php";
17
18 class PHPUnitMaintClass extends Maintenance {
19
20 public static $additionalOptions = [
21 'regex' => false,
22 'file' => false,
23 'use-filebackend' => false,
24 'use-bagostuff' => false,
25 'use-jobqueue' => false,
26 'keep-uploads' => false,
27 'use-normal-tables' => false,
28 'reuse-db' => false,
29 'wiki' => false,
30 ];
31
32 public function __construct() {
33 parent::__construct();
34 $this->addOption(
35 'with-phpunitclass',
36 'Class name of the PHPUnit entry point to use',
37 false,
38 true
39 );
40 $this->addOption(
41 'debug-tests',
42 'Log testing activity to the PHPUnitCommand log channel.',
43 false, # not required
44 false # no arg needed
45 );
46 $this->addOption(
47 'regex',
48 'Only run parser tests that match the given regex.',
49 false,
50 true
51 );
52 $this->addOption( 'file', 'File describing parser tests.', false, true );
53 $this->addOption( 'use-filebackend', 'Use filebackend', false, true );
54 $this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
55 $this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
56 $this->addOption(
57 'keep-uploads',
58 'Re-use the same upload directory for each test, don\'t delete it.',
59 false,
60 false
61 );
62 $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
63 $this->addOption(
64 'reuse-db', 'Init DB only if tables are missing and keep after finish.',
65 false,
66 false
67 );
68 }
69
70 public function finalSetup() {
71 parent::finalSetup();
72
73 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType, $wgMainWANCache;
74 global $wgMainStash;
75 global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
76 global $wgLocaltimezone, $wgLocalisationCacheConf;
77 global $wgDevelopmentWarnings;
78 global $wgSessionProviders;
79 global $wgJobTypeConf;
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 // Bug 44192 Do not attempt to send a real e-mail
128 Hooks::clear( 'AlternateUserMailer' );
129 Hooks::register(
130 'AlternateUserMailer',
131 function () {
132 return false;
133 }
134 );
135 // xdebug's default of 100 is too low for MediaWiki
136 ini_set( 'xdebug.max_nesting_level', 1000 );
137
138 // Bug T116683 serialize_precision of 100
139 // may break testing against floating point values
140 // treated with PHP's serialize()
141 ini_set( 'serialize_precision', 17 );
142 }
143
144 public function execute() {
145 global $IP;
146
147 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
148 // stays in tact.
149 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
150 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
151 restore_error_handler();
152
153 $this->forceFormatServerArgv();
154
155 # Make sure we have --configuration or PHPUnit might complain
156 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
157 // Hack to eliminate the need to use the Makefile (which sucks ATM)
158 array_splice( $_SERVER['argv'], 1, 0,
159 [ '--configuration', $IP . '/tests/phpunit/suite.xml' ] );
160 }
161
162 if ( $this->hasOption( 'with-phpunitclass' ) ) {
163 global $wgPhpUnitClass;
164 $wgPhpUnitClass = $this->getOption( 'with-phpunitclass' );
165
166 # Cleanup $args array so the option and its value do not
167 # pollute PHPUnit
168 $key = array_search( '--with-phpunitclass', $_SERVER['argv'] );
169 unset( $_SERVER['argv'][$key] ); // the option
170 unset( $_SERVER['argv'][$key + 1] ); // its value
171 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
172 }
173
174 $key = array_search( '--debug-tests', $_SERVER['argv'] );
175 if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
176 unset( $_SERVER['argv'][$key] );
177 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
178 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
179 }
180
181 foreach ( self::$additionalOptions as $option => $default ) {
182 $key = array_search( '--' . $option, $_SERVER['argv'] );
183 if ( $key !== false ) {
184 unset( $_SERVER['argv'][$key] );
185 if ( $this->mParams[$option]['withArg'] ) {
186 self::$additionalOptions[$option] = $_SERVER['argv'][$key + 1];
187 unset( $_SERVER['argv'][$key + 1] );
188 } else {
189 self::$additionalOptions[$option] = true;
190 }
191 }
192 }
193
194 }
195
196 public function getDbType() {
197 return Maintenance::DB_ADMIN;
198 }
199
200 /**
201 * Force the format of elements in $_SERVER['argv']
202 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
203 */
204 private function forceFormatServerArgv() {
205 $argv = [];
206 foreach ( $_SERVER['argv'] as $key => $arg ) {
207 if ( $key === 0 ) {
208 $argv[0] = $arg;
209 } elseif ( strstr( $arg, '=' ) ) {
210 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
211 $argv[] = $argPart;
212 }
213 } else {
214 $argv[] = $arg;
215 }
216 }
217 $_SERVER['argv'] = $argv;
218 }
219
220 }
221
222 $maintClass = 'PHPUnitMaintClass';
223 require RUN_MAINTENANCE_IF_MAIN;
224
225 if ( !class_exists( 'PHPUnit_Framework_TestCase' ) ) {
226 echo "PHPUnit not found. Please install it and other dev dependencies by
227 running `composer install` in MediaWiki root directory.\n";
228 exit( 1 );
229 }
230 if ( !class_exists( $wgPhpUnitClass ) ) {
231 echo "PHPUnit entry point '" . $wgPhpUnitClass . "' not found. Please make sure you installed
232 the containing component and check the spelling of the class name.\n";
233 exit( 1 );
234 }
235
236 echo defined( 'HHVM_VERSION' ) ?
237 'Using HHVM ' . HHVM_VERSION . ' (' . PHP_VERSION . ")\n" :
238 'Using PHP ' . PHP_VERSION . "\n";
239
240 $wgPhpUnitClass::main();