wiki.techinc.nl/tests/phpunit/phpunit.php

76 lines
2.2 KiB
PHP
Raw Normal View History

#!/usr/bin/env php
<?php
/**
* Bootstrapping for MediaWiki PHPUnit tests
*
* @file
*/
use PHPUnit\TextUI\Command;
require_once __DIR__ . '/bootstrap.integration.php';
require_once __DIR__ . '/IntegrationBootstrapWrapper.php';
Safer autoloading with respect to file-scope code Many files were in the autoloader despite having potentially harmful file-scope code. * Exclude all CommandLineInc maintenance scripts from the autoloader. * Introduce "NO_AUTOLOAD" tag which excludes the file containing it from the autoloader. Use it on CommandLineInc.php and a few suspicious-looking files without classes in case they are refactored to add classes in the future. * Add a test which parses all non-PSR4 class files and confirms that they do not contain dangerous file-scope code. It's slow (15s) but its results were enlightening. * Several maintenance scripts define constants in the file scope, intending to modify the behaviour of MediaWiki. Either move the define() to a later setup function, or protect with NO_AUTOLOAD. * Use require_once consistently with Maintenance.php and doMaintenance.php, per the original convention which is supposed to allow one maintenance script to use the class of another maintenance script. Using require breaks autoloading of these maintenance class files. * When Maintenance.php is included, check if MediaWiki has already started, and if so, return early. Revert the fix for T250003 which is incompatible with this safety measure. Hopefully it was superseded by splitting out the class file. * In runScript.php add a redundant PHP_SAPI check since it does some things in file-scope code before any other check will be run. * Change the if(false) class_alias(...) to something more hackish and more compatible with the new test. * Some site-related scripts found Maintenance.php in a non-standard way. Use the standard way. * fileOpPerfTest.php called error_reporting(). Probably debugging code left in; removed. * Moved mediawiki.compress.7z registration from the class file to the caller. Change-Id: I1b1be90343a5ab678df6f1b1bdd03319dcf6537f
2021-01-08 02:16:02 +00:00
/**
* Temporary class overriding the IntegrationBootstrapWrapper.
*/
class PHPUnitMaintClass extends IntegrationBootstrapWrapper {
public function execute() {
// Start an output buffer to avoid headers being sent by constructors,
// data providers, etc. (T206476)
ob_start();
wfDeprecatedMsg( 'tests/phpunit/phpunit.php is deprecated and will be removed. ' .
'Please use `composer phpunit` or `vendor/bin/phpunit`',
'1.39'
);
$command = new Command();
$args = $_SERVER['argv'];
$knownOpts = getopt( 'c:', [ 'configuration:', 'bootstrap:' ] ) ?: [];
if ( !isset( $knownOpts['c'] ) && !isset( $knownOpts['configuration'] ) ) {
// XXX HAX: Use our default file. This is a temporary hack, to be removed when this file goes away
// or when T227900 is resolved.
$args[] = '--configuration=' . dirname( __DIR__, 2 ) . '/phpunit.xml.dist';
}
$command->run( $args, true );
}
}
if ( PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ) {
exit( 'This script must be run from the command line' );
}
if ( strval( getenv( 'MW_INSTALL_PATH' ) ) === '' ) {
putenv( 'MW_INSTALL_PATH=' . realpath( __DIR__ . '/../..' ) );
}
if ( getenv( 'PHPUNIT_WIKI' ) ) {
$wikiName = getenv( 'PHPUNIT_WIKI' );
$bits = explode( '-', $wikiName, 2 );
define( 'MW_DB', $bits[0] );
define( 'MW_PREFIX', $bits[1] ?? '' );
define( 'MW_WIKI_NAME', $wikiName );
}
// Define the MediaWiki entrypoint
$IP = getenv( 'MW_INSTALL_PATH' );
global $wgIntegrationBootstrapWrapper;
$wgIntegrationBootstrapWrapper = new PHPUnitMaintClass();
$wgIntegrationBootstrapWrapper->setup();
require_once "$IP/includes/BootstrapHelperFunctions.php";
// ensure MW_INSTALL_PATH is defined
$IP = wfDetectInstallPath();
wfDetectLocalSettingsFile( $IP );
require_once "$IP/includes/Setup.php";
// Deregister handler from MWExceptionHandler::installHandle so that PHPUnit's own handler
// stays in tact. Needs to happen after including Setup.php, which calls MWExceptionHandler::installHandle().
restore_error_handler();
$wgIntegrationBootstrapWrapper->execute();