wiki.techinc.nl/tests/phpunit/phpunit.php
Florian d7c72bcbe5 Fix broken running tests locally in IDE
The class PHPMaintClass is meant to be loaded when running phpunit.php
which is only done when running tests with it. When running tests by
using the bootstrap file only, e.g. in an IDE, this class will not be
available. Relying on it in other classes will therefore break them.

Moving the required parts to another outside class and add it to the
test autoloader. My feeling says, that adding the PHPMaintClass to the
autoloader says "NOOOO", that's why I added a new one.

There also seems to be some CI builds failing because of that:
https://integration.wikimedia.org/ci/hob/quibble-vendor-mysql-php72-docker/30642/console

Bug: T151101
Change-Id: I33e27009657a951173694fc847973560a1ce967b
2019-11-21 16:28:12 +00:00

116 lines
3.3 KiB
PHP
Executable file

#!/usr/bin/env php
<?php
/**
* Bootstrapping for MediaWiki PHPUnit tests
*
* @file
*/
// Set a flag which can be used to detect when other scripts have been entered
// through this entry point or not.
define( 'MW_PHPUNIT_TEST', true );
// Start up MediaWiki in command-line mode
require_once dirname( dirname( __DIR__ ) ) . "/maintenance/Maintenance.php";
class PHPUnitMaintClass extends Maintenance {
public function __construct() {
parent::__construct();
$this->setAllowUnregisteredOptions( true );
$this->addOption(
'debug-tests',
'Log testing activity to the PHPUnitCommand log channel (deprecated, always on).',
false, # not required
false # no arg needed
);
$this->addOption( 'use-filebackend', 'Use filebackend', false, true );
$this->addOption( 'use-bagostuff', 'Use bagostuff', false, true );
$this->addOption( 'use-jobqueue', 'Use jobqueue', false, true );
$this->addOption( 'use-normal-tables', 'Use normal DB tables.', false, false );
$this->addOption(
'reuse-db', 'Init DB only if tables are missing and keep after finish.',
false,
false
);
}
public function setup() {
parent::setup();
require_once __DIR__ . '/../common/TestSetup.php';
TestSetup::snapshotGlobals();
}
public function finalSetup() {
parent::finalSetup();
// Inject test autoloader
self::requireTestsAutoloader();
TestSetup::applyInitialConfig();
}
public function execute() {
// Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
// stays in tact.
// Has to in execute() instead of finalSetup(), because finalSetup() runs before
// doMaintenance.php includes Setup.php, which calls MWExceptionHandler::installHandle().
restore_error_handler();
$this->forceFormatServerArgv();
if ( !class_exists( 'PHPUnit\\Framework\\TestCase' ) ) {
echo "PHPUnit not found. Please install it and other dev dependencies by
running `composer install` in MediaWiki root directory.\n";
exit( 1 );
}
fwrite( STDERR, 'Using PHP ' . PHP_VERSION . "\n" );
foreach ( MediaWikiCliOptions::$additionalOptions as $option => $default ) {
MediaWikiCliOptions::$additionalOptions[$option] = $this->getOption( $option );
}
$command = new MediaWikiPHPUnitCommand();
$command->run( $_SERVER['argv'], true );
}
public function getDbType() {
return Maintenance::DB_ADMIN;
}
protected function addOption( $name, $description, $required = false,
$withArg = false, $shortName = false, $multiOccurrence = false
) {
// ignore --quiet which does not really make sense for unit tests
if ( $name !== 'quiet' ) {
parent::addOption( $name, $description, $required, $withArg, $shortName, $multiOccurrence );
}
}
/**
* Force the format of elements in $_SERVER['argv']
* - Split args such as "wiki=enwiki" into two separate arg elements "wiki" and "enwiki"
*/
private function forceFormatServerArgv() {
$argv = [];
foreach ( $_SERVER['argv'] as $key => $arg ) {
if ( $key === 0 ) {
$argv[0] = $arg;
} else {
$parts = explode( '=', $arg, 2 );
$arg = preg_replace( '/^--/', '', $parts[0] );
// Avoid confusing PHPUnit with MediaWiki-specific parameters
if ( isset( $this->mParams[$arg] ) ) {
continue;
}
$argv = array_merge( $argv, $parts );
}
}
$_SERVER['argv'] = $argv;
}
}
$maintClass = 'PHPUnitMaintClass';
require RUN_MAINTENANCE_IF_MAIN;