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