Skip to content

Commit c67e7bd

Browse files
committed
add isOff public method, make getState public
1 parent 10f505b commit c67e7bd

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,32 @@ if (FeatureFlag::isOn('search-v2')) {
7777
}
7878
```
7979

80+
### Check If A Feature Is Disabled
81+
#### Blade View
82+
```php
83+
@unlessfeature('search-v2')
84+
// no new features for you
85+
@endfeature
86+
```
87+
88+
#### In Your Code
89+
```php
90+
use Codinglabs\FeatureFlags\Facades\FeatureFlag;
91+
92+
if (FeatureFlag::isOff('search-v2')) {
93+
// no new features for you
94+
}
95+
```
96+
97+
### Get The Underlying Current State
98+
If you want to know what the underlying `FeatureState` value is:
99+
```php
100+
use Codinglabs\FeatureFlags\Facades\FeatureFlag;
101+
102+
// value from Codinglabs\FeatureFlags\Enums\FeatureState
103+
$featureState = FeatureFlag::getState('search-v2');
104+
```
105+
80106
### Updating Feature State
81107
To change the state of a feature you can call the following methods:
82108
```php

src/FeatureFlags.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private static function getFeatureModel(string $feature): ?Model
4343
return null;
4444
}
4545

46-
private static function getState(string $feature): FeatureState
46+
public static function getState(string $feature): FeatureState
4747
{
4848
$featureKey = self::getFeatureCacheKey($feature);
4949

@@ -89,6 +89,11 @@ function () use ($feature) {
8989
};
9090
}
9191

92+
public static function isOff(string $feature): bool
93+
{
94+
return ! self::isOn($feature);
95+
}
96+
9297
public static function reset(): void
9398
{
9499
self::$dynamicHandlers = [];

tests/FeaturesTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@
1818
FeatureFlag::reset();
1919
});
2020

21-
it('throws an exception if calling isOn for a feature that does not exist', function () {
21+
it('throws an exception if calling isOn on a feature that does not exist', function () {
2222
$this->expectException(MissingFeatureException::class);
2323

2424
FeatureFlag::isOn('some-feature');
2525
expect(cache()->store('array')->get('testing.some-feature'))->toBeNull();
2626
});
2727

28+
it('throws an exception if calling isOff on a feature that does not exist', function () {
29+
$this->expectException(MissingFeatureException::class);
30+
31+
FeatureFlag::isOff('some-feature');
32+
expect(cache()->store('array')->get('testing.some-feature'))->toBeNull();
33+
});
34+
2835
it('generates the correct cache key', function () {
2936
config(['feature-flags.cache_prefix' => 'some-prefix']);
3037

@@ -37,6 +44,7 @@
3744
});
3845

3946
expect(FeatureFlag::isOn('some-feature'))->toBeFalse();
47+
expect(FeatureFlag::isOff('some-feature'))->toBeTrue();
4048
expect(cache()->store('array')->get('testing.some-feature'))->toBeNull();
4149
});
4250

@@ -47,6 +55,7 @@
4755
]);
4856

4957
expect(FeatureFlag::isOn('some-feature'))->toBeFalse();
58+
expect(FeatureFlag::isOff('some-feature'))->toBeTrue();
5059
expect(cache()->store('array')->get('testing.some-feature'))->toBe(FeatureState::off()->value);
5160
});
5261

@@ -57,6 +66,7 @@
5766
]);
5867

5968
expect(FeatureFlag::isOn('some-feature'))->toBeTrue();
69+
expect(FeatureFlag::isOff('some-feature'))->toBeFalse();
6070
expect(cache()->store('array')->get('testing.some-feature'))->toBe(FeatureState::on()->value);
6171
});
6272

@@ -71,6 +81,7 @@
7181
});
7282

7383
expect(FeatureFlag::isOn('some-feature'))->toBeTrue();
84+
expect(FeatureFlag::isOff('some-feature'))->toBeFalse();
7485
expect(cache()->store('array')->get('testing.some-feature'))->toBe(FeatureState::dynamic()->value);
7586
});
7687

@@ -85,6 +96,7 @@
8596
});
8697

8798
expect(FeatureFlag::isOn('some-feature'))->toBeFalse();
99+
expect(FeatureFlag::isOff('some-feature'))->toBeTrue();
88100
expect(cache()->store('array')->get('testing.some-feature'))->toBe(FeatureState::dynamic()->value);
89101
});
90102

@@ -99,6 +111,7 @@
99111
});
100112

101113
expect(FeatureFlag::isOn('some-feature'))->toBeTrue();
114+
expect(FeatureFlag::isOff('some-feature'))->toBeFalse();
102115
expect(cache()->store('array')->get('testing.some-feature'))->toBe(FeatureState::dynamic()->value);
103116
});
104117

@@ -109,6 +122,26 @@
109122
]);
110123

111124
expect(FeatureFlag::isOn('some-feature'))->toBeFalse();
125+
expect(FeatureFlag::isOff('some-feature'))->toBeTrue();
126+
});
127+
128+
it('resolves the current state', function () {
129+
Feature::factory()->create([
130+
'name' => 'some-off-feature',
131+
'state' => FeatureState::off()
132+
]);
133+
Feature::factory()->create([
134+
'name' => 'some-dynamic-feature',
135+
'state' => FeatureState::dynamic()
136+
]);
137+
Feature::factory()->create([
138+
'name' => 'some-on-feature',
139+
'state' => FeatureState::on()
140+
]);
141+
142+
expect(FeatureFlag::getState('some-off-feature'))->toBe(FeatureState::off());
143+
expect(FeatureFlag::getState('some-dynamic-feature'))->toBe(FeatureState::dynamic());
144+
expect(FeatureFlag::getState('some-on-feature'))->toBe(FeatureState::on());
112145
});
113146

114147
it('can update a features state', function () {
@@ -125,6 +158,7 @@
125158

126159
Event::assertDispatched(\Codinglabs\FeatureFlags\Events\FeatureUpdatedEvent::class);
127160
expect(FeatureFlag::isOn('some-feature'))->toBeTrue();
161+
expect(FeatureFlag::isOff('some-feature'))->toBeFalse();
128162
expect(cache()->store('array')->get('testing.some-feature'))->toBe(FeatureState::on()->value);
129163
});
130164

0 commit comments

Comments
 (0)