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