VectorGOLEM/tests/phpunit/unit/FeatureManagement/FeatureManagerTest.php
Sam Smith 9177c22365 features: Add minimalist feature manager
With complex additions to Vector's codebase like the Desktop Improvement
Program upcoming, it's important that we have a shared, intuitive
language to talk about features and their requirements. Centralising
the registration of features and creating an API satisfies does exactly
this.

This change introduces a greatly-reduced version of Piotr Miazga's
(polishdeveloper, pmiazga) original proposed API and associated
scaffolding classes for feature management in Vector, which itself was
based upon his work in MobileFrontend/MinervaNeue. This is done to
establish a foundation upon which we can build the more sophisticated
parts of Piotr's proposal in a piecemeal basis, thereby minimising risk.

Distinct from Piotr's proposed API is the ability to register sets and
features that are always enabled or disabled.

Additionally:

- A Vector.FeatureManager service is registered but not used

- A list of proposed immediate next steps is included

Bug: T244481
Change-Id: Ie53c41d479eaf15559d5bb00f269774760360bde
2020-02-26 11:49:29 +00:00

157 lines
4.4 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @since 1.35
*/
namespace Vector\FeatureManagement\Tests;
use Vector\FeatureManagement\FeatureManager;
/**
* @group Vector
* @group FeatureManagement
* @coversDefaultClass \Vector\FeatureManagement\FeatureManager
*/
class FeatureManagerTest extends \MediaWikiUnitTestCase {
/**
* @covers ::registerSet
*/
public function testRegisterSetThrowsWhenSetIsRegisteredTwice() {
$this->expectException( \LogicException::class );
$featureManager = new FeatureManager();
$featureManager->registerSet( 'setA', true );
$featureManager->registerSet( 'setA', true );
}
/**
* @covers ::registerSet
*/
public function testRegisterSetValidatesIsEnabled() {
$this->expectException( \Wikimedia\Assert\ParameterAssertionException::class );
$featureManager = new FeatureManager();
$featureManager->registerSet( 'setA', 'foo' );
}
public static function provideInvalidFeatureConfig() {
return [
// ::registerFeature( string, int[] ) will throw an exception.
[
\Wikimedia\Assert\ParameterAssertionException::class,
[ 1 ],
],
// The "bar" set hasn't been registered.
[
\InvalidArgumentException::class,
[
'bar',
],
],
];
}
/**
* @dataProvider provideInvalidFeatureConfig
* @covers ::registerFeature
*/
public function testRegisterFeatureValidatesConfig( $expectedExceptionType, $config ) {
$this->expectException( $expectedExceptionType );
$featureManager = new FeatureManager();
$featureManager->registerSet( 'set', true );
$featureManager->registerFeature( 'feature', $config );
}
/**
* @covers ::isSetEnabled
*/
public function testIsSetEnabled() {
$featureManager = new FeatureManager();
$featureManager->registerSet( 'enabled', true );
$featureManager->registerSet( 'disabled', false );
$this->assertTrue( $featureManager->isSetEnabled( 'enabled' ) );
$this->assertFalse( $featureManager->isSetEnabled( 'disabled' ) );
}
/**
* @covers ::isSetEnabled
*/
public function testIsSetEnabledThrowsExceptionWhenSetIsntRegistered() {
$this->expectException( \InvalidArgumentException::class );
$featureManager = new FeatureManager();
$featureManager->isSetEnabled( 'foo' );
}
/**
* @covers ::registerFeature
*/
public function testRegisterFeatureThrowsExceptionWhenFeatureIsRegisteredTwice() {
$this->expectException( \LogicException::class );
$featureManager = new FeatureManager();
$featureManager->registerFeature( 'featureA', [] );
$featureManager->registerFeature( 'featureA', [] );
}
/**
* @covers ::isFeatureEnabled
*/
public function testIsFeatureEnabled() {
$featureManager = new FeatureManager();
$featureManager->registerSet( 'foo', false );
$featureManager->registerFeature( 'requiresFoo', 'foo' );
$this->assertFalse(
$featureManager->isFeatureEnabled( 'requiresFoo' ),
'A feature is disabled when the set that it requires is disabled.'
);
// ---
$featureManager->registerSet( 'bar', true );
$featureManager->registerSet( 'baz', true );
$featureManager->registerFeature( 'requiresFooBar', [ 'foo', 'bar' ] );
$featureManager->registerFeature( 'requiresBarBaz', [ 'bar', 'baz' ] );
$this->assertFalse(
$featureManager->isFeatureEnabled( 'requiresFooBar' ),
'A feature is disabled when at least one set that it requires is disabled.'
);
$this->assertTrue( $featureManager->isFeatureEnabled( 'requiresBarBaz' ) );
}
/**
* @covers ::isFeatureEnabled
*/
public function testIsFeatureEnabledThrowsExceptionWhenFeatureIsntRegistered() {
$this->expectException( \InvalidArgumentException::class );
$featureManager = new FeatureManager();
$featureManager->isFeatureEnabled( 'foo' );
}
}