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,
29 public function __construct() {
30 parent
::__construct();
33 'Class name of the PHPUnit entry point to use',
39 'Log testing activity to the PHPUnitCommand log channel.',
43 $this->addOption( 'file', 'File describing parser tests.', false, true );
44 $this->addOption( 'use-filebackend', 'Use filebackend', false, true );
45 $this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
46 $this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
47 $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
49 'reuse-db', 'Init DB only if tables are missing and keep after finish.',
55 public function finalSetup() {
58 // Inject test autoloader
59 self
::requireTestsAutoloader();
61 TestSetup
::applyInitialConfig();
64 public function execute() {
67 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
69 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
70 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
71 restore_error_handler();
73 $this->forceFormatServerArgv();
75 # Make sure we have --configuration or PHPUnit might complain
76 if ( !in_array( '--configuration', $_SERVER['argv'] ) ) {
77 // Hack to eliminate the need to use the Makefile (which sucks ATM)
78 array_splice( $_SERVER['argv'], 1, 0,
79 [ '--configuration', $IP . '/tests/phpunit/suite.xml' ] );
82 $phpUnitClass = 'PHPUnit_TextUI_Command';
84 if ( $this->hasOption( 'with-phpunitclass' ) ) {
85 $phpUnitClass = $this->getOption( 'with-phpunitclass' );
87 # Cleanup $args array so the option and its value do not
89 $key = array_search( '--with-phpunitclass', $_SERVER['argv'] );
90 unset( $_SERVER['argv'][$key] ); // the option
91 unset( $_SERVER['argv'][$key +
1] ); // its value
92 $_SERVER['argv'] = array_values( $_SERVER['argv'] );
95 $key = array_search( '--debug-tests', $_SERVER['argv'] );
96 if ( $key !== false && array_search( '--printer', $_SERVER['argv'] ) === false ) {
97 unset( $_SERVER['argv'][$key] );
98 array_splice( $_SERVER['argv'], 1, 0, 'MediaWikiPHPUnitTestListener' );
99 array_splice( $_SERVER['argv'], 1, 0, '--printer' );
102 foreach ( self
::$additionalOptions as $option => $default ) {
103 $key = array_search( '--' . $option, $_SERVER['argv'] );
104 if ( $key !== false ) {
105 unset( $_SERVER['argv'][$key] );
106 if ( $this->mParams
[$option]['withArg'] ) {
107 self
::$additionalOptions[$option] = $_SERVER['argv'][$key +
1];
108 unset( $_SERVER['argv'][$key +
1] );
110 self
::$additionalOptions[$option] = true;
115 if ( !class_exists( 'PHPUnit_Framework_TestCase' ) ) {
116 echo "PHPUnit not found. Please install it and other dev dependencies by
117 running `composer install` in MediaWiki root directory.\n";
120 if ( !class_exists( $phpUnitClass ) ) {
121 echo "PHPUnit entry point '" . $phpUnitClass . "' not found. Please make sure you installed
122 the containing component and check the spelling of the class name.\n";
126 echo defined( 'HHVM_VERSION' ) ?
127 'Using HHVM ' . HHVM_VERSION
. ' (' . PHP_VERSION
. ")\n" :
128 'Using PHP ' . PHP_VERSION
. "\n";
130 // Prepare global services for unit tests.
131 MediaWikiTestCase
::prepareServices( new GlobalVarConfig() );
133 $phpUnitClass::main();
136 public function getDbType() {
137 return Maintenance
::DB_ADMIN
;
141 * Force the format of elements in $_SERVER['argv']
142 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
144 private function forceFormatServerArgv() {
146 foreach ( $_SERVER['argv'] as $key => $arg ) {
149 } elseif ( strstr( $arg, '=' ) ) {
150 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
157 $_SERVER['argv'] = $argv;
162 $maintClass = 'PHPUnitMaintClass';
163 require RUN_MAINTENANCE_IF_MAIN
;