Merge "Make interwiki transclusion use the WAN cache"
[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 'file' => false,
20 'use-filebackend' => false,
21 'use-bagostuff' => false,
22 'use-jobqueue' => false,
23 'use-normal-tables' => false,
24 'mwdebug' => false,
25 'reuse-db' => false,
26 'wiki' => false,
27 'profiler' => false,
28 ];
29
30 public function __construct() {
31 parent::__construct();
32 $this->setAllowUnregisteredOptions( true );
33 $this->addOption(
34 'with-phpunitclass',
35 'Class name of the PHPUnit entry point to use',
36 false,
37 true
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( 'file', 'File describing parser tests.', false, true );
46 $this->addOption( 'use-filebackend', 'Use filebackend', false, true );
47 $this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
48 $this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
49 $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
50 $this->addOption(
51 'reuse-db', 'Init DB only if tables are missing and keep after finish.',
52 false,
53 false
54 );
55 }
56
57 public function finalSetup() {
58 parent::finalSetup();
59
60 // Inject test autoloader
61 self::requireTestsAutoloader();
62
63 TestSetup::applyInitialConfig();
64 }
65
66 public function execute() {
67 global $IP;
68
69 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
70 // stays in tact.
71 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
72 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
73 restore_error_handler();
74
75 $this->forceFormatServerArgv();
76
77 # Make sure we have --configuration or PHPUnit might complain
78 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
79 // Hack to eliminate the need to use the Makefile (which sucks ATM)
80 array_splice( $_SERVER['argv'], 1, 0,
81 [ '--configuration', $IP . '/tests/phpunit/suite.xml' ] );
82 }
83
84 $phpUnitClass = PHPUnit_TextUI_Command::class;
85
86 if ( $this->hasOption( 'with-phpunitclass' ) ) {
87 $phpUnitClass = $this->getOption( 'with-phpunitclass' );
88
89 # Cleanup $args array so the option and its value do not
90 # pollute PHPUnit
91 $key = array_search( '--with-phpunitclass', $_SERVER['argv'] );
92 unset( $_SERVER['argv'][$key] ); // the option
93 unset( $_SERVER['argv'][$key + 1] ); // its value
94 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
95 }
96
97 $key = array_search( '--debug-tests', $_SERVER['argv'] );
98 if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
99 unset( $_SERVER['argv'][$key] );
100 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
101 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
102 }
103
104 foreach ( self::$additionalOptions as $option => $default ) {
105 $key = array_search( '--' . $option, $_SERVER['argv'] );
106 if ( $key !== false ) {
107 unset( $_SERVER['argv'][$key] );
108 if ( $this->mParams[$option]['withArg'] ) {
109 self::$additionalOptions[$option] = $_SERVER['argv'][$key + 1];
110 unset( $_SERVER['argv'][$key + 1] );
111 } else {
112 self::$additionalOptions[$option] = true;
113 }
114 }
115 }
116
117 if ( !class_exists( 'PHPUnit\\Framework\\TestCase' ) ) {
118 echo "PHPUnit not found. Please install it and other dev dependencies by
119 running `composer install` in MediaWiki root directory.\n";
120 exit( 1 );
121 }
122 if ( !class_exists( $phpUnitClass ) ) {
123 echo "PHPUnit entry point '" . $phpUnitClass . "' not found. Please make sure you installed
124 the containing component and check the spelling of the class name.\n";
125 exit( 1 );
126 }
127
128 echo defined( 'HHVM_VERSION' ) ?
129 'Using HHVM ' . HHVM_VERSION . ' (' . PHP_VERSION . ")\n" :
130 'Using PHP ' . PHP_VERSION . "\n";
131
132 // Prepare global services for unit tests.
133 MediaWikiTestCase::prepareServices( new GlobalVarConfig() );
134
135 $phpUnitClass::main();
136 }
137
138 public function getDbType() {
139 return Maintenance::DB_ADMIN;
140 }
141
142 protected function addOption( $name, $description, $required = false,
143 $withArg = false, $shortName = false, $multiOccurrence = false
144 ) {
145 // ignore --quiet which does not really make sense for unit tests
146 if ( $name !== 'quiet' ) {
147 parent::addOption( $name, $description, $required, $withArg, $shortName, $multiOccurrence );
148 }
149 }
150
151 /**
152 * Force the format of elements in $_SERVER['argv']
153 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
154 */
155 private function forceFormatServerArgv() {
156 $argv = [];
157 foreach ( $_SERVER['argv'] as $key => $arg ) {
158 if ( $key === 0 ) {
159 $argv[0] = $arg;
160 } elseif ( strstr( $arg, '=' ) ) {
161 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
162 $argv[] = $argPart;
163 }
164 } else {
165 $argv[] = $arg;
166 }
167 }
168 $_SERVER['argv'] = $argv;
169 }
170
171 }
172
173 $maintClass = 'PHPUnitMaintClass';
174 require RUN_MAINTENANCE_IF_MAIN;