[docs] [dev] [PHP] [FeatureManager] revise docs and add DynamicConfigRequirement test

Address some feedback from I7a2cdc2dfdf20d78e4548f07cf53994563b234b3:

- Miscellaneous documentation improvements.
- Add a false case test to `DynamicConfigRequirement->isMet()`.

Bug: T244481
Change-Id: Ic5637f42da755f871c5a6d545e14effd3ac8c670
This commit is contained in:
Stephen Niedzielski 2020-03-25 14:36:44 -06:00 committed by Sam Smith
parent ac79997d87
commit 6b6bed86ed
4 changed files with 36 additions and 18 deletions

View File

@ -62,6 +62,7 @@ final class Constants {
// These are used in the Feature Management System.
/**
* Also known as `$wgFullyInitialised`. Set to true in core/includes/Setup.php.
* @var string
*/
public const CONFIG_KEY_FULLY_INITIALISED = 'FullyInitialised';

View File

@ -25,7 +25,7 @@ namespace Vector\FeatureManagement\Requirements;
use Vector\FeatureManagement\Requirement;
/**
* Some application state changes throughout the lifetime of the application, e.g.
* Some application state changes throughout the lifetime of the application, e.g. `wgSitename` or
* `wgFullyInitialised`, which signals whether the application boot process has finished and
* critical resources like database connections are available.
*
@ -36,14 +36,25 @@ use Vector\FeatureManagement\Requirement;
* $featureManager->registerComplexRequirement(
* new DynamicConfigRequirement(
* $config,
* 'FullyInitialised',
* 'Sitename',
* 'Foo'
* )
* );
* ```
*
* registers a requirement that will evaluate to true only when `mediawiki/includes/Setup.php` has
* finished executing (after all service wiring has executed).
* finished executing (after all service wiring has executed). I.e., every call to
* `Requirement->isMet()` reinterrogates the Config object for the current state and returns it.
* Contrast to
*
* ```lang=php
* $featureManager->registerRequirement(
* 'Foo',
* $config->get( 'Sitename' )
* );
* ```
*
* wherein state is evaluated only once at registration time and permanently cached.
*
* NOTE: This API hasn't settled. It may change at any time without warning. Please don't bind to
* it unless you absolutely need to
@ -72,8 +83,11 @@ final class DynamicConfigRequirement implements Requirement {
/**
* @param \Config $config
* @param string $configName
* @param string $requirementName The name of the requirement presented to the Feature Manager
* @param string $configName Any `Config` key. This name is used to query `$config` state. E.g.,
* `'DBname'`. See https://www.mediawiki.org/wiki/Manual:Configuration_settings
* @param string $requirementName The name of the requirement presented to FeatureManager.
* This name _usually_ matches the `$configName` parameter for simplicity but allows for
* abstraction as needed. See `Requirement->getName()`.
*/
public function __construct( \Config $config, string $configName, string $requirementName ) {
$this->config = $config;

View File

@ -32,13 +32,13 @@ return [
return $services->getService( 'ConfigFactory' )->makeConfig( Constants::SKIN_NAME );
},
Constants::SERVICE_FEATURE_MANAGER => function ( MediaWikiServices $services ) {
$requirement = new DynamicConfigRequirement(
$services->getMainConfig(),
Constants::CONFIG_KEY_FULLY_INITIALISED,
Constants::REQUIREMENT_FULLY_INITIALISED
);
$featureManager = new FeatureManager();
$requirement = new DynamicConfigRequirement(
$services->getMainConfig(),
Constants::CONFIG_KEY_FULLY_INITIALISED,
Constants::REQUIREMENT_FULLY_INITIALISED
);
$featureManager->registerComplexRequirement( $requirement );
return $featureManager;

View File

@ -31,19 +31,22 @@ use Vector\FeatureManagement\Requirements\DynamicConfigRequirement;
*/
class DynamicConfigRequirementTest extends \MediaWikiUnitTestCase {
public static function providesBooleanStates(): array {
return [ [ false ], [ true ] ];
}
/**
* @dataProvider providesBooleanStates
* @covers ::isMet
*/
public function testItFetchesAndReturnsConfigValue() {
$config = $this->createMock( \Config::class );
$config->expects( $this->once() )
->method( 'get' )
->with( 'Foo' )
->willReturn( true );
public function testItFetchesAndReturnsConfigValue( bool $configValue ) {
$config = new \HashConfig( [
'Foo' => $configValue,
] );
$requirement = new DynamicConfigRequirement( $config, 'Foo', 'Bar' );
$this->assertTrue( $requirement->isMet() );
$this->assertEquals( $requirement->isMet(), $configValue );
}
/**