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
{
17 public function __construct() {
18 parent
::__construct();
19 $this->setAllowUnregisteredOptions( true );
22 'Log testing activity to the PHPUnitCommand log channel (deprecated, always on).',
26 $this->addOption( 'use-filebackend', 'Use filebackend', false, true );
27 $this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
28 $this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
29 $this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
31 'reuse-db', 'Init DB only if tables are missing and keep after finish.',
37 public function finalSetup() {
40 // Inject test autoloader
41 self
::requireTestsAutoloader();
43 TestSetup
::applyInitialConfig();
46 public function execute() {
47 // Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
49 // Has to in execute() instead of finalSetup(), because finalSetup() runs before
50 // doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
51 restore_error_handler();
53 $this->forceFormatServerArgv();
55 if ( !class_exists( 'PHPUnit\\Framework\\TestCase' ) ) {
56 echo "PHPUnit not found. Please install it and other dev dependencies by
57 running `composer install` in MediaWiki root directory.\n";
61 fwrite( STDERR
, defined( 'HHVM_VERSION' ) ?
62 'Using HHVM ' . HHVM_VERSION
. ' (' . PHP_VERSION
. ")\n" :
63 'Using PHP ' . PHP_VERSION
. "\n" );
65 // Tell PHPUnit to ignore options meant for MediaWiki
67 foreach ( $this->mParams
as $name => $param ) {
68 if ( empty( $param['withArg'] ) ) {
75 // Pass through certain options to MediaWikiTestCase
86 $cliArgs[$name] = $this->getOption( $name );
89 $command = new MediaWikiPHPUnitCommand( $ignore, $cliArgs );
90 $command->run( $_SERVER['argv'], true );
93 public function getDbType() {
94 return Maintenance
::DB_ADMIN
;
97 protected function addOption( $name, $description, $required = false,
98 $withArg = false, $shortName = false, $multiOccurrence = false
100 // ignore --quiet which does not really make sense for unit tests
101 if ( $name !== 'quiet' ) {
102 parent
::addOption( $name, $description, $required, $withArg, $shortName, $multiOccurrence );
107 * Force the format of elements in $_SERVER['argv']
108 * - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
110 private function forceFormatServerArgv() {
112 foreach ( $_SERVER['argv'] as $key => $arg ) {
115 } elseif ( strstr( $arg, '=' ) ) {
116 foreach ( explode( '=', $arg, 2 ) as $argPart ) {
123 $_SERVER['argv'] = $argv;
128 $maintClass = 'PHPUnitMaintClass';
129 require RUN_MAINTENANCE_IF_MAIN
;