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