Merge "Add countUnreadNotifications to WatchedItemStore"
[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 ];
29
30 public function __construct() {
31 parent::__construct();
32 $this->addOption(
33 'debug-tests',
34 'Log testing activity to the PHPUnitCommand log channel.',
35 false, # not required
36 false # no arg needed
37 );
38 $this->addOption(
39 'regex',
40 'Only run parser tests that match the given regex.',
41 false,
42 true
43 );
44 $this->addOption( 'file', 'File describing parser tests.', false, true );
45 $this->addOption( 'use-filebackend', 'Use filebackend', false, true );
46 $this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
47 $this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
48 $this->addOption(
49 'keep-uploads',
50 'Re-use the same upload directory for each test, don\'t delete it.',
51 false,
52 false
53 );
54 $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
55 $this->addOption(
56 'reuse-db', 'Init DB only if tables are missing and keep after finish.',
57 false,
58 false
59 );
60 }
61
62 public function finalSetup() {
63 parent::finalSetup();
64
65 global $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType, $wgMainWANCache;
66 global $wgMainStash;
67 global $wgLanguageConverterCacheType, $wgUseDatabaseMessages;
68 global $wgLocaltimezone, $wgLocalisationCacheConf;
69 global $wgDevelopmentWarnings;
70 global $wgSessionProviders;
71 global $wgJobTypeConf;
72
73 // Inject test autoloader
74 require_once __DIR__ . '/../TestsAutoLoader.php';
75
76 // wfWarn should cause tests to fail
77 $wgDevelopmentWarnings = true;
78
79 // Make sure all caches and stashes are either disabled or use
80 // in-process cache only to prevent tests from using any preconfigured
81 // cache meant for the local wiki from outside the test run.
82 // See also MediaWikiTestCase::run() which mocks CACHE_DB and APC.
83
84 // Disabled in DefaultSettings, override local settings
85 $wgMainWANCache =
86 $wgMainCacheType = CACHE_NONE;
87 // Uses CACHE_ANYTHING in DefaultSettings, use hash instead of db
88 $wgMessageCacheType =
89 $wgParserCacheType =
90 $wgSessionCacheType =
91 $wgLanguageConverterCacheType = 'hash';
92 // Uses db-replicated in DefaultSettings
93 $wgMainStash = 'hash';
94 // Use memory job queue
95 $wgJobTypeConf = [
96 'default' => [ 'class' => 'JobQueueMemory', 'order' => 'fifo' ],
97 ];
98
99 $wgUseDatabaseMessages = false; # Set for future resets
100
101 // Assume UTC for testing purposes
102 $wgLocaltimezone = 'UTC';
103
104 $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
105
106 // Generic MediaWiki\Session\SessionManager configuration for tests
107 // We use CookieSessionProvider because things might be expecting
108 // cookies to show up in a FauxRequest somewhere.
109 $wgSessionProviders = [
110 [
111 'class' => 'MediaWiki\\Session\\CookieSessionProvider',
112 'args' => [ [
113 'priority' => 30,
114 'callUserSetCookiesHook' => true,
115 ] ],
116 ],
117 ];
118
119 // Bug 44192 Do not attempt to send a real e-mail
120 Hooks::clear( 'AlternateUserMailer' );
121 Hooks::register(
122 'AlternateUserMailer',
123 function () {
124 return false;
125 }
126 );
127 // xdebug's default of 100 is too low for MediaWiki
128 ini_set( 'xdebug.max_nesting_level', 1000 );
129
130 // Bug T116683 serialize_precision of 100
131 // may break testing against floating point values
132 // treated with PHP's serialize()
133 ini_set( 'serialize_precision', 17 );
134 }
135
136 public function execute() {
137 global $IP;
138
139 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
140 // stays in tact.
141 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
142 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
143 restore_error_handler();
144
145 $this->forceFormatServerArgv();
146
147 # Make sure we have --configuration or PHPUnit might complain
148 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
149 // Hack to eliminate the need to use the Makefile (which sucks ATM)
150 array_splice( $_SERVER['argv'], 1, 0,
151 [ '--configuration', $IP . '/tests/phpunit/suite.xml' ] );
152 }
153
154 $key = array_search( '--debug-tests', $_SERVER['argv'] );
155 if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
156 unset( $_SERVER['argv'][$key] );
157 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
158 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
159 }
160
161 foreach ( self::$additionalOptions as $option => $default ) {
162 $key = array_search( '--' . $option, $_SERVER['argv'] );
163 if ( $key !== false ) {
164 unset( $_SERVER['argv'][$key] );
165 if ( $this->mParams[$option]['withArg'] ) {
166 self::$additionalOptions[$option] = $_SERVER['argv'][$key + 1];
167 unset( $_SERVER['argv'][$key + 1] );
168 } else {
169 self::$additionalOptions[$option] = true;
170 }
171 }
172 }
173
174 }
175
176 public function getDbType() {
177 return Maintenance::DB_ADMIN;
178 }
179
180 /**
181 * Force the format of elements in $_SERVER['argv']
182 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
183 */
184 private function forceFormatServerArgv() {
185 $argv = [];
186 foreach ( $_SERVER['argv'] as $key => $arg ) {
187 if ( $key === 0 ) {
188 $argv[0] = $arg;
189 } elseif ( strstr( $arg, '=' ) ) {
190 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
191 $argv[] = $argPart;
192 }
193 } else {
194 $argv[] = $arg;
195 }
196 }
197 $_SERVER['argv'] = $argv;
198 }
199
200 }
201
202 $maintClass = 'PHPUnitMaintClass';
203 require RUN_MAINTENANCE_IF_MAIN;
204
205 if ( !class_exists( 'PHPUnit_TextUI_Command' ) ) {
206 echo "PHPUnit not found. Please install it and other dev dependencies by
207 running `composer install` in MediaWiki root directory.\n";
208 exit( 1 );
209 }
210
211 echo defined( 'HHVM_VERSION' ) ?
212 'Using HHVM ' . HHVM_VERSION . ' (' . PHP_VERSION . ")\n" :
213 'Using PHP ' . PHP_VERSION . "\n";
214
215 PHPUnit_TextUI_Command::main();