4 * Bootstrapping for MediaWiki PHPUnit tests
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 );
13 // Start up MediaWiki in command-line mode
14 require_once dirname( dirname( __DIR__
) ) . "/maintenance/Maintenance.php";
16 class PHPUnitMaintClass
extends Maintenance
{
18 public static $additionalOptions = [
20 'use-filebackend' => false,
21 'use-bagostuff' => false,
22 'use-jobqueue' => false,
23 'use-normal-tables' => false,
30 public function __construct() {
31 parent
::__construct();
34 'Class name of the PHPUnit entry point to use',
40 'Log testing activity to the PHPUnitCommand log channel.',
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 );
50 'reuse-db', 'Init DB only if tables are missing and keep after finish.',
56 public function finalSetup() {
59 // Inject test autoloader
60 self
::requireTestsAutoloader();
62 TestSetup
::applyInitialConfig();
65 public function execute() {
68 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
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();
74 $this->forceFormatServerArgv();
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' ] );
83 $phpUnitClass = 'PHPUnit_TextUI_Command';
85 if ( $this->hasOption( 'with-phpunitclass' ) ) {
86 $phpUnitClass = $this->getOption( 'with-phpunitclass' );
88 # Cleanup $args array so the option and its value do not
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'] );
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' );
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] );
111 self
::$additionalOptions[$option] = true;
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";
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";
127 echo defined( 'HHVM_VERSION' ) ?
128 'Using HHVM ' . HHVM_VERSION
. ' (' . PHP_VERSION
. ")\n" :
129 'Using PHP ' . PHP_VERSION
. "\n";
131 // Prepare global services for unit tests.
132 MediaWikiTestCase
::prepareServices( new GlobalVarConfig() );
134 $phpUnitClass::main();
137 public function getDbType() {
138 return Maintenance
::DB_ADMIN
;
141 protected function addOption( $name, $description, $required = false,
142 $withArg = false, $shortName = false, $multiOccurrence = false
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 );
151 * Force the format of elements in $_SERVER['argv']
152 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
154 private function forceFormatServerArgv() {
156 foreach ( $_SERVER['argv'] as $key => $arg ) {
159 } elseif ( strstr( $arg, '=' ) ) {
160 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
167 $_SERVER['argv'] = $argv;
172 $maintClass = 'PHPUnitMaintClass';
173 require RUN_MAINTENANCE_IF_MAIN
;