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