expectWarning() and friends have been deprecated in PHPUnit 9.6, and removed in PHPUnit 10. Unfortunately, there is no simple replacement because PHPUnit no longer converts them to exceptions in the first place. In fact, Sebastian Bergmann explicitly stated that he does not consider the use case of > a library developer to verify a code block warns its consumer when > certain action is performed worth supporting. So, add an ad-hoc replacement for all the deprecated methods. This is quite ugly, but it's simple enough given the low number of usages. On the bright side, this new method does not halt the test when the warning is triggered. This seems to align with the developers' expectation, seen in a few existing tests, that any code following the notice will be executed. Bug: T342110 Change-Id: I214abfed4280834840c115777ed78eb0a5570da9
158 lines
3.9 KiB
PHP
158 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Tests\Unit\Settings;
|
|
|
|
use ExtensionRegistry;
|
|
use MediaWiki\Config\HashConfig;
|
|
use MediaWiki\MainConfigNames;
|
|
use MediaWiki\Settings\Config\ArrayConfigBuilder;
|
|
use MediaWiki\Settings\Config\PhpIniSink;
|
|
use MediaWiki\Settings\SettingsBuilder;
|
|
use MediaWiki\Settings\WikiFarmSettingsLoader;
|
|
use MediaWikiUnitTestCase;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
/**
|
|
* @covers \MediaWiki\Settings\WikiFarmSettingsLoader
|
|
*/
|
|
class WikiFarmSettingsLoaderTest extends MediaWikiUnitTestCase {
|
|
|
|
private $originalServerVars = null;
|
|
|
|
/**
|
|
* @before
|
|
*/
|
|
public function serverVarsSetUp() {
|
|
$this->originalServerVars = $_SERVER;
|
|
}
|
|
|
|
/**
|
|
* @after
|
|
*/
|
|
public function serverVarsTearDown() {
|
|
$_SERVER = $this->originalServerVars;
|
|
}
|
|
|
|
/**
|
|
* @param array $params
|
|
* @return SettingsBuilder
|
|
*/
|
|
private function newSettingsBuilder( $params = [] ): SettingsBuilder {
|
|
return new SettingsBuilder(
|
|
__DIR__,
|
|
$params['extensionRegistry'] ?? $this->createMock( ExtensionRegistry::class ),
|
|
$params['configBuilder'] ?? new ArrayConfigBuilder(),
|
|
$params['phpIniSink'] ?? $this->createMock( PhpIniSink::class ),
|
|
$params['cache'] ?? null
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return SettingsBuilder|MockObject
|
|
*/
|
|
private function fakeSettingsBuilder(): SettingsBuilder {
|
|
$config = [
|
|
MainConfigNames::WikiFarmSettingsDirectory => 'test',
|
|
MainConfigNames::WikiFarmSettingsExtension => 'yaml',
|
|
];
|
|
$mock = $this->createMock( SettingsBuilder::class );
|
|
$mock->method( 'getConfig' )->willReturn( new HashConfig( $config ) );
|
|
$mock->method( 'fileExists' )->willReturn( true );
|
|
|
|
return $mock;
|
|
}
|
|
|
|
public static function provideWikiFarmSettings() {
|
|
yield [
|
|
[
|
|
'WikiFarmSettingsDirectory' => __DIR__ . '/fixtures/sites',
|
|
'WikiFarmSettingsExtension' => 'yaml',
|
|
],
|
|
'alpha',
|
|
[ 'SiteName' => 'Alpha Wiki' ]
|
|
];
|
|
yield [
|
|
[
|
|
'WikiFarmSettingsDirectory' => __DIR__ . '/fixtures/sites',
|
|
'WikiFarmSettingsExtension' => 'json',
|
|
],
|
|
'beta',
|
|
[ 'SiteName' => 'Beta Wiki' ]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array $config
|
|
* @param string $detect
|
|
* @param array $expected
|
|
*
|
|
* @dataProvider provideWikiFarmSettings
|
|
*/
|
|
public function testLoadWikiFarmSettings( $config, $detect, $expected ) {
|
|
$settings = $this->newSettingsBuilder();
|
|
|
|
$settings->putConfigValues( $config );
|
|
|
|
$_SERVER['MW_WIKI_NAME'] = $detect;
|
|
|
|
$loader = $this->newLoader( $settings );
|
|
$loader->loadWikiFarmSettings();
|
|
|
|
$config = $settings->getConfig();
|
|
|
|
foreach ( $expected as $name => $value ) {
|
|
$this->assertSame( $value, $config->get( $name ) );
|
|
}
|
|
}
|
|
|
|
public function testDetectSettingsFileFromConstant() {
|
|
$settings = $this->fakeSettingsBuilder();
|
|
|
|
$settings->expects( $this->once() )
|
|
->method( 'loadFile' )
|
|
->with( 'test/AcmeWiki123.yaml' );
|
|
|
|
$loader = $this->newLoader( $settings, 'AcmeWiki123' );
|
|
$loader->loadWikiFarmSettings();
|
|
}
|
|
|
|
public function testDetectSettingsFileFromLegacyVar() {
|
|
$settings = $this->fakeSettingsBuilder();
|
|
|
|
$_SERVER['WIKI_NAME'] = 'test/FooBarWiki.yaml';
|
|
|
|
$this->expectPHPError(
|
|
E_USER_NOTICE,
|
|
function () use ( $settings ) {
|
|
// TODO: Assert that the file is actually loaded after the notice is triggered
|
|
$loader = $this->newLoader( $settings );
|
|
$loader->loadWikiFarmSettings();
|
|
},
|
|
'The WIKI_NAME server variable has been deprecated'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param SettingsBuilder $settings
|
|
* @param ?string $wikiNameConstantValue
|
|
*
|
|
* @return WikiFarmSettingsLoader
|
|
*/
|
|
private function newLoader( SettingsBuilder $settings, ?string $wikiNameConstantValue = null ) {
|
|
$loader = new class( $settings, $wikiNameConstantValue ) extends WikiFarmSettingsLoader {
|
|
private $wikiNameConstantValue;
|
|
|
|
public function __construct( $settings, $wikiNameConstantValue ) {
|
|
parent::__construct( $settings );
|
|
$this->wikiNameConstantValue = $wikiNameConstantValue;
|
|
}
|
|
|
|
protected function getWikiNameConstant() {
|
|
return $this->wikiNameConstantValue;
|
|
}
|
|
};
|
|
|
|
return $loader;
|
|
}
|
|
|
|
}
|