Debian Codegraph @

skins/Vector/tests/phpunit/unit/FeatureManagement/FeatureManagerTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @since 1.35
20 */
21
22 namespace MediaWiki\Skins\Vector\Tests\Unit\FeatureManagement;
23
24 use MediaWiki\Context\RequestContext;
25 use MediaWiki\Skins\Vector\FeatureManagement\FeatureManager;
26 use MediaWiki\User\Options\UserOptionsLookup;
27
28 /**
29 * @group Vector
30 * @group FeatureManagement
31 * @coversDefaultClass \MediaWiki\Skins\Vector\FeatureManagement\FeatureManager
32 */
33 class FeatureManagerTest extends \MediaWikiUnitTestCase {
34
35 /**
36 * @covers ::registerSimpleRequirement
37 * @covers ::registerRequirement
38 */
39 public function testRegisterSimpleRequirementThrowsWhenRequirementIsRegisteredTwice() {
40 $this->expectException( \LogicException::class );
41
42 $featureManager = new FeatureManager(
43 $this->createMock( UserOptionsLookup::class ),
44 RequestContext::getMain()
45 );
46 $featureManager->registerSimpleRequirement( 'requirementA', true );
47 $featureManager->registerSimpleRequirement( 'requirementA', true );
48 }
49
50 public static function provideInvalidFeatureConfig() {
51 return [
52
53 // ::registerFeature( string, int[] ) will throw an exception.
54 [
55 \Wikimedia\Assert\ParameterAssertionException::class,
56 [ 1 ],
57 ],
58
59 // The "bar" requirement hasn't been registered.
60 [
61 \InvalidArgumentException::class,
62 [
63 'bar',
64 ],
65 ],
66 ];
67 }
68
69 /**
70 * @dataProvider provideInvalidFeatureConfig
71 * @covers ::registerFeature
72 */
73 public function testRegisterFeatureValidatesConfig( $expectedExceptionType, $config ) {
74 $this->expectException( $expectedExceptionType );
75
76 $featureManager = new FeatureManager(
77 $this->createMock( UserOptionsLookup::class ),
78 RequestContext::getMain()
79 );
80 $featureManager->registerSimpleRequirement( 'requirement', true );
81 $featureManager->registerFeature( 'feature', $config );
82 }
83
84 /**
85 * @covers ::isRequirementMet
86 */
87 public function testIsRequirementMet() {
88 $featureManager = new FeatureManager(
89 $this->createMock( UserOptionsLookup::class ),
90 RequestContext::getMain()
91 );
92 $featureManager->registerSimpleRequirement( 'enabled', true );
93 $featureManager->registerSimpleRequirement( 'disabled', false );
94
95 $this->assertTrue( $featureManager->isRequirementMet( 'enabled' ) );
96 $this->assertFalse( $featureManager->isRequirementMet( 'disabled' ) );
97 }
98
99 /**
100 * @covers ::isRequirementMet
101 */
102 public function testIsRequirementMetThrowsExceptionWhenRequirementIsntRegistered() {
103 $this->expectException( \InvalidArgumentException::class );
104
105 $featureManager = new FeatureManager(
106 $this->createMock( UserOptionsLookup::class ),
107 RequestContext::getMain()
108 );
109 $featureManager->isRequirementMet( 'foo' );
110 }
111
112 /**
113 * @covers ::registerFeature
114 */
115 public function testRegisterFeatureThrowsExceptionWhenFeatureIsRegisteredTwice() {
116 $this->expectException( \LogicException::class );
117
118 $featureManager = new FeatureManager(
119 $this->createMock( UserOptionsLookup::class ),
120 RequestContext::getMain()
121 );
122 $featureManager->registerFeature( 'featureA', [] );
123 $featureManager->registerFeature( 'featureA', [] );
124 }
125
126 /**
127 * @covers ::isFeatureEnabled
128 */
129 public function testIsFeatureEnabled() {
130 $featureManager = new FeatureManager(
131 $this->createMock( UserOptionsLookup::class ),
132 RequestContext::getMain()
133 );
134 $featureManager->registerSimpleRequirement( 'foo', false );
135 $featureManager->registerFeature( 'requiresFoo', 'foo' );
136
137 $this->assertFalse(
138 $featureManager->isFeatureEnabled( 'requiresFoo' ),
139 'A feature is disabled when the requirement that it requires is disabled.'
140 );
141
142 // ---
143
144 $featureManager->registerSimpleRequirement( 'bar', true );
145 $featureManager->registerSimpleRequirement( 'baz', true );
146
147 $featureManager->registerFeature( 'requiresFooBar', [ 'foo', 'bar' ] );
148 $featureManager->registerFeature( 'requiresBarBaz', [ 'bar', 'baz' ] );
149
150 $this->assertFalse(
151 $featureManager->isFeatureEnabled( 'requiresFooBar' ),
152 'A feature is disabled when at least one requirement that it requires is disabled.'
153 );
154
155 $this->assertTrue( $featureManager->isFeatureEnabled( 'requiresBarBaz' ) );
156 }
157
158 /**
159 * @covers ::isFeatureEnabled
160 */
161 public function testIsFeatureEnabledThrowsExceptionWhenFeatureIsntRegistered() {
162 $this->expectException( \InvalidArgumentException::class );
163
164 $featureManager = new FeatureManager(
165 $this->createMock( UserOptionsLookup::class ),
166 RequestContext::getMain()
167 );
168 $featureManager->isFeatureEnabled( 'foo' );
169 }
170 }