phpunit: Deprecate tests/phpunit/phpunit.php script
`composer phpunit:entrypoint` should be used instead. Copy all the setup to a new bootstrap file, with some adjustments for the bootstrap not being in the global scope when PHPUnit loads it. Leave the old script and bootstrap in place (e.g., for CI), but make them print deprecation warnings. Update the composer scripts to use the standard `phpunit` entrypoint. The bootstrap code was copied almost verbatim from the phpunit.php entrypoint to minimize changes. Any improvements can be done in other patches. Bug: T90875 Change-Id: I95a80c44f8d88c3c498efe1ae64008f0ff1eea55
This commit is contained in:
parent
8bd59ff17e
commit
d2a30096f1
7 changed files with 213 additions and 24 deletions
|
|
@ -319,6 +319,9 @@ because of Phabricator reports.
|
|||
the handler are deprecated and will soon be removed. Deprecated ways to
|
||||
specify a hook handler include callables wrapped in an array. Handlers
|
||||
defined using a "HookHandlers" entry in extension.json are not affected.
|
||||
* The tests/phpunit/phpunit.php entrypoint has been deprecated. PHPUnit
|
||||
tests should be run with composer, for example with the
|
||||
`composer phpunit:entrypoint` command.
|
||||
* The jquery.cookie ResourceLoader module has been merged into the existing
|
||||
mediawiki.cookie module; jquery.cookie remains but is deprecated.
|
||||
* The Pingback class has been moved into the MediaWiki\Installer namespace.
|
||||
|
|
|
|||
|
|
@ -159,8 +159,8 @@
|
|||
"@lint",
|
||||
"@phpcs"
|
||||
],
|
||||
"phpunit": "php tests/phpunit/phpunit.php",
|
||||
"phpunit:unit": "phpunit --colors=always --testsuite=core:unit,extensions:unit,skins:unit",
|
||||
"phpunit": "phpunit",
|
||||
"phpunit:unit": "@phpunit --colors=always --testsuite=core:unit,extensions:unit,skins:unit",
|
||||
"phpunit:integration": "@phpunit --colors=always --testsuite=core:integration,extensions:integration,skins:integration",
|
||||
"phpunit:coverage": "@phpunit --testsuite=core:unit --exclude-group Dump,Broken",
|
||||
"phpunit:coverage-edit": "ComposerPhpunitXmlCoverageEdit::onEvent",
|
||||
|
|
|
|||
|
|
@ -152,31 +152,35 @@ class TestSetup {
|
|||
* @param string $fileName the file to include
|
||||
*/
|
||||
public static function requireOnceInGlobalScope( string $fileName ): void {
|
||||
// Make a copy of the original $GLOBALS array so that later modifications to the
|
||||
// $GLOBALS array are not seen by the code below. This only affects PHP < 8.1,
|
||||
// see https://wiki.php.net/rfc/restrict_globals_usage
|
||||
// We use a map so that the lookup is faster.
|
||||
$originalGlobalsMap = [];
|
||||
foreach ( $GLOBALS as $key => $_ ) {
|
||||
$originalGlobalsMap[$key] = true;
|
||||
// phpcs:ignore MediaWiki.VariableAnalysis.UnusedGlobalVariables.UnusedGlobal$key,MediaWiki.NamingConventions.ValidGlobalName.allowedPrefix
|
||||
global $$key;
|
||||
}
|
||||
|
||||
require_once $fileName;
|
||||
|
||||
$localVars = [
|
||||
$ignore = [
|
||||
'fileName' => true,
|
||||
'originalGlobalsMap' => true,
|
||||
'key' => true,
|
||||
'_' => true,
|
||||
'localVars' => true,
|
||||
'ignore' => true,
|
||||
'wgAutoloadClasses' => true,
|
||||
];
|
||||
|
||||
// Import $GLOBALS into local scope for the file.
|
||||
// Modifications to these from te required file automatically affect the real global.
|
||||
foreach ( $GLOBALS as $key => $_ ) {
|
||||
$ignore[$key] = true;
|
||||
// phpcs:ignore MediaWiki.VariableAnalysis.UnusedGlobalVariables,MediaWiki.NamingConventions.ValidGlobalName.allowedPrefix
|
||||
global $$key;
|
||||
}
|
||||
|
||||
// Setup.php creates this variable, but we cannot wait for the below code to make it global,
|
||||
// because Setup.php (and MW_SETUP_CALLBACK -> TestsAutoLoader.php) needs this to be a
|
||||
// global during its execution (not just after).
|
||||
// phpcs:ignore MediaWiki.VariableAnalysis.UnusedGlobalVariables
|
||||
global $wgAutoloadClasses;
|
||||
|
||||
require_once $fileName;
|
||||
|
||||
// Create any new variables as actual globals.
|
||||
foreach ( get_defined_vars() as $varName => $value ) {
|
||||
if ( array_key_exists( $varName, $localVars ) ) {
|
||||
continue;
|
||||
}
|
||||
if ( array_key_exists( $varName, $originalGlobalsMap ) ) {
|
||||
// Skip our own internal variables, and variables that were already global.
|
||||
if ( array_key_exists( $varName, $ignore ) ) {
|
||||
continue;
|
||||
}
|
||||
$GLOBALS[$varName] = $value;
|
||||
|
|
|
|||
172
tests/phpunit/bootstrap.integration.php
Normal file
172
tests/phpunit/bootstrap.integration.php
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\MediaWikiServices;
|
||||
|
||||
/**
|
||||
* Bootstrapping for MediaWiki PHPUnit tests that allows running integration tests.
|
||||
* This file is included by phpunit and is NOT in the global scope.
|
||||
*
|
||||
* @file
|
||||
*/
|
||||
|
||||
// phpcs:ignore MediaWiki.Files.ClassMatchesFilename.NotMatch
|
||||
class PHPUnitBootstrap {
|
||||
public function setup() {
|
||||
// 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 );
|
||||
|
||||
// Send PHP warnings and errors to stderr instead of stdout.
|
||||
// This aids in diagnosing problems, while keeping messages
|
||||
// out of redirected output.
|
||||
if ( ini_get( 'display_errors' ) ) {
|
||||
ini_set( 'display_errors', 'stderr' );
|
||||
}
|
||||
|
||||
$this->prepareEnvironment();
|
||||
require_once __DIR__ . '/../common/TestSetup.php';
|
||||
TestSetup::snapshotGlobals();
|
||||
}
|
||||
|
||||
public function prepareEnvironment() {
|
||||
global $wgCommandLineMode;
|
||||
|
||||
# Disable the memory limit as it's not needed for tests.
|
||||
ini_set( 'memory_limit', -1 );
|
||||
|
||||
# Set max execution time to 0 (no limit). PHP.net says that
|
||||
# "When running PHP from the command line the default setting is 0."
|
||||
# But sometimes this doesn't seem to be the case.
|
||||
ini_set( 'max_execution_time', 0 );
|
||||
|
||||
$wgCommandLineMode = true;
|
||||
|
||||
# Turn off output buffering if it's on
|
||||
while ( ob_get_level() > 0 ) {
|
||||
ob_end_flush();
|
||||
}
|
||||
}
|
||||
|
||||
public function finalSetup() {
|
||||
global $wgDBadminuser, $wgDBadminpassword;
|
||||
global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf;
|
||||
|
||||
# Prepare environment again, things might have changed in the settings files
|
||||
$this->prepareEnvironment();
|
||||
|
||||
if ( isset( $wgDBadminuser ) ) {
|
||||
$wgDBuser = $wgDBadminuser;
|
||||
$wgDBpassword = $wgDBadminpassword;
|
||||
|
||||
if ( $wgDBservers ) {
|
||||
/**
|
||||
* @var array $wgDBservers
|
||||
*/
|
||||
foreach ( $wgDBservers as $i => $server ) {
|
||||
$wgDBservers[$i]['user'] = $wgDBuser;
|
||||
$wgDBservers[$i]['password'] = $wgDBpassword;
|
||||
}
|
||||
}
|
||||
if ( isset( $wgLBFactoryConf['serverTemplate'] ) ) {
|
||||
$wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
|
||||
$wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
|
||||
}
|
||||
$service = MediaWikiServices::getInstance()->peekService( 'DBLoadBalancerFactory' );
|
||||
if ( $service ) {
|
||||
$service->destroy();
|
||||
}
|
||||
}
|
||||
|
||||
TestSetup::requireOnceInGlobalScope( __DIR__ . '/../common/TestsAutoLoader.php' );
|
||||
|
||||
TestSetup::applyInitialConfig();
|
||||
|
||||
ExtensionRegistry::getInstance()->setLoadTestClassesAndNamespaces( true );
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
// Start an output buffer to avoid headers being sent by constructors,
|
||||
// data providers, etc. (T206476)
|
||||
ob_start();
|
||||
|
||||
fwrite( STDERR, 'Using PHP ' . PHP_VERSION . "\n" );
|
||||
}
|
||||
}
|
||||
|
||||
if ( defined( 'MEDIAWIKI' ) ) {
|
||||
exit( 'Wrong entry point?' );
|
||||
}
|
||||
|
||||
if ( PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ) {
|
||||
exit( 'This script must be run from the command line' );
|
||||
}
|
||||
|
||||
define( 'MW_ENTRY_POINT', 'cli' );
|
||||
|
||||
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
|
||||
define( 'MEDIAWIKI', true );
|
||||
|
||||
// phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.allowedPrefix
|
||||
global $IP;
|
||||
$IP = getenv( 'MW_INSTALL_PATH' );
|
||||
|
||||
// phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.allowedPrefix
|
||||
global $_PHPUnitBootstrapper;
|
||||
$_PHPUnitBootstrapper = new PHPUnitBootstrap();
|
||||
$_PHPUnitBootstrapper->setup();
|
||||
|
||||
require_once "$IP/includes/BootstrapHelperFunctions.php";
|
||||
|
||||
$IP = wfDetectInstallPath(); // ensure MW_INSTALL_PATH is defined
|
||||
wfDetectLocalSettingsFile( $IP );
|
||||
|
||||
function wfPHPUnitSetup() {
|
||||
// phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.allowedPrefix
|
||||
global $_PHPUnitBootstrapper;
|
||||
$_PHPUnitBootstrapper->finalSetup();
|
||||
}
|
||||
|
||||
define( 'MW_SETUP_CALLBACK', 'wfPHPUnitSetup' );
|
||||
|
||||
TestSetup::requireOnceInGlobalScope( "$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();
|
||||
|
||||
// Check that composer dependencies are up-to-date
|
||||
if ( !getenv( 'MW_SKIP_EXTERNAL_DEPENDENCIES' ) ) {
|
||||
$composerLockUpToDate = new CheckComposerLockUpToDate();
|
||||
$composerLockUpToDate->loadParamsAndArgs( 'phpunit', [ 'quiet' => true ] );
|
||||
$composerLockUpToDate->execute();
|
||||
}
|
||||
|
||||
$_PHPUnitBootstrapper->execute();
|
||||
|
||||
// The TestRunner class will run each test suite and may call
|
||||
// exit() with an exit status code. As such, we cannot run code "after the last test"
|
||||
// by adding statements to PHPUnitMaintClass::execute.
|
||||
// Instead, we work around it by registering a shutdown callback from the bootstrap
|
||||
// file, which runs before PHPUnit starts.
|
||||
// @todo Once we use PHPUnit 8 or higher, use the 'AfterLastTestHook' feature.
|
||||
// https://phpunit.readthedocs.io/en/8.0/extending-phpunit.html#available-hook-interfaces
|
||||
register_shutdown_function( static function () {
|
||||
// This will:
|
||||
// - clear the temporary job queue.
|
||||
// - allow extensions to delete any temporary tables they created.
|
||||
// - restore ability to connect to the real database.
|
||||
MediaWikiIntegrationTestCase::teardownTestDB();
|
||||
} );
|
||||
|
||||
MediaWikiCliOptions::initialize();
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
<?php
|
||||
/**
|
||||
* Bootstrapping for MediaWiki PHPUnit tests when called via the maintenance class tests runner.
|
||||
* This file is included by phpunit and is NOT in the global scope.
|
||||
* DEPRECATED: This file only exists for BC with tests/phpunit/phpunit.php and will be removed together with it.
|
||||
*
|
||||
* @file
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -98,10 +98,21 @@ class PHPUnitMaintClass {
|
|||
// or when T227900 is resolved.
|
||||
$args[] = '--configuration=' . __DIR__ . '/suite.xml';
|
||||
}
|
||||
$args[] = '--bootstrap=' . __DIR__ . '/bootstrap.maintenance.php';
|
||||
$command->run( $args, true );
|
||||
}
|
||||
}
|
||||
|
||||
$deprecationMsg = <<<EOT
|
||||
*******************************************************************************
|
||||
DEPRECATED: The tests/phpunit/phpunit.php entry point has been deprecated. Use
|
||||
`composer phpunit:entrypoint` instead.
|
||||
*******************************************************************************
|
||||
|
||||
EOT;
|
||||
|
||||
fwrite( STDERR, $deprecationMsg );
|
||||
|
||||
if ( defined( 'MEDIAWIKI' ) ) {
|
||||
exit( 'Wrong entry point?' );
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit bootstrap="./bootstrap.maintenance.php"
|
||||
<phpunit bootstrap="./bootstrap.integration.php"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
|
||||
colors="true"
|
||||
|
|
|
|||
Loading…
Reference in a new issue