wiki.techinc.nl/tests/phpunit/suites/ExtensionsUnitTestSuite.php
Daimona Eaytoy a053d106bf phpunit: Determine what extensions to load in unit tests via config
When running unit tests, the bootstrap would previously load all
extensions and skins in the filesystem. This was OK for an initial
implementation, but is not acceptable if we want to eventually do that
for all PHPUnit entry points (once we'll have a single config and
bootstrap). Instead, it's desirable to only load the extensions
specified in LocalSettings.php. The problem is that it's pretty much
impossible to load LocalSettings.php without also loading the rest of
MediaWiki, with all the side effects this might have.

This patch introduces a helper script that loads all the config, then
prints what extensions and skins were loaded. The bootstrap file runs
this script via proc_open and then reads the list of extensions to
load. Because the script is run in a separate process, any side effects
only affect the spawned process, not the one where PHPUnit is running.

Currently, there doesn't seem to be a better way to obtain the list of
extensions loaded in LocalSettings.php without all the other side
effects. YAML settings
(https://www.mediawiki.org/wiki/Manual:YAML_settings_file_format) would
probably help, but that's very far from becoming the only supported
config format (if it will ever be).

Also add two TestSuite implementations to replace the '*' wildcard in the
extensions:unit and skins:unit suites. These use the same list of loaded
extensions to determine where to look for tests.

And last but not least: my most sincere apologies to you if the hack
you're seeing here has ruined your day. If you think a better approach
exists, please tell me and I'll be so relieved!

Bug: T227900
Change-Id: Ib578644b8a4c0b64dca607afb9eb8204ca7fc660
2023-09-25 23:03:48 +00:00

36 lines
1 KiB
PHP

<?php
use PHPUnit\Framework\TestSuite;
use SebastianBergmann\FileIterator\Facade;
/**
* Test suite that runs extensions unit tests (the `extensions:unit` suite).
*/
class ExtensionsUnitTestSuite extends TestSuite {
public function __construct() {
parent::__construct();
if ( !defined( 'MW_PHPUNIT_EXTENSIONS_PATHS' ) ) {
throw new RuntimeException( 'The PHPUnit bootstrap was not loaded' );
}
$paths = [];
foreach ( MW_PHPUNIT_EXTENSIONS_PATHS as $path ) {
// Note that we don't load settings, so we expect to find extensions in their
// default location
// Standardize directory separators for Windows compatibility.
if ( str_contains( strtr( $path, '\\', '/' ), '/extensions/' ) ) {
$paths[] = "$path/tests/phpunit/unit";
}
}
foreach ( array_unique( $paths ) as $path ) {
$suffixes = [ 'Test.php' ];
$fileIterator = new Facade();
$matchingFiles = $fileIterator->getFilesAsArray( $path, $suffixes );
$this->addTestFiles( $matchingFiles );
}
}
public static function suite() {
return new self;
}
}