2020-02-20 16:37:33 +00:00
|
|
|
<?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 {
|
|
|
|
|
|
|
|
/**
|
2020-02-28 15:42:05 +00:00
|
|
|
* @covers ::registerRequirement
|
2020-02-20 16:37:33 +00:00
|
|
|
*/
|
2020-02-28 15:42:05 +00:00
|
|
|
public function testRegisterRequirementThrowsWhenRequirementIsRegisteredTwice() {
|
2020-02-20 16:37:33 +00:00
|
|
|
$this->expectException( \LogicException::class );
|
|
|
|
|
|
|
|
$featureManager = new FeatureManager();
|
2020-02-28 15:42:05 +00:00
|
|
|
$featureManager->registerRequirement( 'requirementA', true );
|
|
|
|
$featureManager->registerRequirement( 'requirementA', true );
|
2020-02-20 16:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-28 15:42:05 +00:00
|
|
|
* @covers ::registerRequirement
|
2020-02-20 16:37:33 +00:00
|
|
|
*/
|
2020-02-28 15:42:05 +00:00
|
|
|
public function testRegisterRequirementValidatesIsEnabled() {
|
2020-02-20 16:37:33 +00:00
|
|
|
$this->expectException( \Wikimedia\Assert\ParameterAssertionException::class );
|
|
|
|
|
|
|
|
$featureManager = new FeatureManager();
|
2020-02-28 15:42:05 +00:00
|
|
|
$featureManager->registerRequirement( 'requirementA', 'foo' );
|
2020-02-20 16:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function provideInvalidFeatureConfig() {
|
|
|
|
return [
|
|
|
|
|
|
|
|
// ::registerFeature( string, int[] ) will throw an exception.
|
|
|
|
[
|
|
|
|
\Wikimedia\Assert\ParameterAssertionException::class,
|
|
|
|
[ 1 ],
|
|
|
|
],
|
|
|
|
|
2020-02-28 15:42:05 +00:00
|
|
|
// The "bar" requirement hasn't been registered.
|
2020-02-20 16:37:33 +00:00
|
|
|
[
|
|
|
|
\InvalidArgumentException::class,
|
|
|
|
[
|
|
|
|
'bar',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideInvalidFeatureConfig
|
|
|
|
* @covers ::registerFeature
|
|
|
|
*/
|
|
|
|
public function testRegisterFeatureValidatesConfig( $expectedExceptionType, $config ) {
|
|
|
|
$this->expectException( $expectedExceptionType );
|
|
|
|
|
|
|
|
$featureManager = new FeatureManager();
|
2020-02-28 15:42:05 +00:00
|
|
|
$featureManager->registerRequirement( 'requirement', true );
|
2020-02-20 16:37:33 +00:00
|
|
|
$featureManager->registerFeature( 'feature', $config );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-28 15:42:05 +00:00
|
|
|
* @covers ::isRequirementMet
|
2020-02-20 16:37:33 +00:00
|
|
|
*/
|
2020-02-28 15:42:05 +00:00
|
|
|
public function testIsRequirementMet() {
|
2020-02-20 16:37:33 +00:00
|
|
|
$featureManager = new FeatureManager();
|
2020-02-28 15:42:05 +00:00
|
|
|
$featureManager->registerRequirement( 'enabled', true );
|
|
|
|
$featureManager->registerRequirement( 'disabled', false );
|
2020-02-20 16:37:33 +00:00
|
|
|
|
2020-02-28 15:42:05 +00:00
|
|
|
$this->assertTrue( $featureManager->isRequirementMet( 'enabled' ) );
|
|
|
|
$this->assertFalse( $featureManager->isRequirementMet( 'disabled' ) );
|
2020-02-20 16:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-28 15:42:05 +00:00
|
|
|
* @covers ::isRequirementMet
|
2020-02-20 16:37:33 +00:00
|
|
|
*/
|
2020-02-28 15:42:05 +00:00
|
|
|
public function testIsRequirementMetThrowsExceptionWhenRequirementIsntRegistered() {
|
2020-02-20 16:37:33 +00:00
|
|
|
$this->expectException( \InvalidArgumentException::class );
|
|
|
|
|
|
|
|
$featureManager = new FeatureManager();
|
2020-02-28 15:42:05 +00:00
|
|
|
$featureManager->isRequirementMet( 'foo' );
|
2020-02-20 16:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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();
|
2020-02-28 15:42:05 +00:00
|
|
|
$featureManager->registerRequirement( 'foo', false );
|
2020-02-20 16:37:33 +00:00
|
|
|
$featureManager->registerFeature( 'requiresFoo', 'foo' );
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$featureManager->isFeatureEnabled( 'requiresFoo' ),
|
2020-02-28 15:42:05 +00:00
|
|
|
'A feature is disabled when the requirement that it requires is disabled.'
|
2020-02-20 16:37:33 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2020-02-28 15:42:05 +00:00
|
|
|
$featureManager->registerRequirement( 'bar', true );
|
|
|
|
$featureManager->registerRequirement( 'baz', true );
|
2020-02-20 16:37:33 +00:00
|
|
|
|
|
|
|
$featureManager->registerFeature( 'requiresFooBar', [ 'foo', 'bar' ] );
|
|
|
|
$featureManager->registerFeature( 'requiresBarBaz', [ 'bar', 'baz' ] );
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$featureManager->isFeatureEnabled( 'requiresFooBar' ),
|
2020-02-28 15:42:05 +00:00
|
|
|
'A feature is disabled when at least one requirement that it requires is disabled.'
|
2020-02-20 16:37:33 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue( $featureManager->isFeatureEnabled( 'requiresBarBaz' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::isFeatureEnabled
|
|
|
|
*/
|
|
|
|
public function testIsFeatureEnabledThrowsExceptionWhenFeatureIsntRegistered() {
|
|
|
|
$this->expectException( \InvalidArgumentException::class );
|
|
|
|
|
|
|
|
$featureManager = new FeatureManager();
|
|
|
|
$featureManager->isFeatureEnabled( 'foo' );
|
|
|
|
}
|
|
|
|
}
|