Skip to content

Commit 2fcf63e

Browse files
committed
always-on config item allows features to always be on
1 parent 17491ee commit 2fcf63e

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

config/feature-flags.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313

1414
'features' => [],
1515

16+
/*
17+
|--------------------------------------------------------------------------
18+
| Always On
19+
|--------------------------------------------------------------------------
20+
|
21+
| Declare the environments where features will be synced to the on
22+
| state. This is useful if features should always be on locally.
23+
| Note this only impacts the behaviour of the sync action.
24+
*/
25+
26+
'always_on' => [],
27+
1628
/*
1729
|--------------------------------------------------------------------------
1830
| Cache

src/Actions/SyncFeaturesAction.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Codinglabs\FeatureFlags\Actions;
44

55
use Codinglabs\FeatureFlags\Models\Feature;
6+
use Codinglabs\FeatureFlags\Enums\FeatureState;
67

78
class SyncFeaturesAction
89
{
@@ -11,7 +12,9 @@ public function __invoke(): void
1112
$features = collect(config('feature-flags.features'))
1213
->map(fn ($state, $name) => [
1314
'name' => $name,
14-
'state' => $state
15+
'state' => app()->environment(config('feature-flags.always_on', []))
16+
? FeatureState::on()
17+
: $state
1518
]);
1619

1720
$featureModels = Feature::all();

tests/SyncFeaturesActionTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,67 @@
110110
'name' => 'some-other-feature',
111111
]);
112112
});
113+
114+
it('overrides the state when the always on config is used and the environment matches', function () {
115+
app()->detectEnvironment(fn () => 'staging');
116+
117+
config([
118+
'feature-flags.features' => [
119+
'some-feature' => FeatureState::on(),
120+
'some-other-feature' => FeatureState::off(),
121+
'some-dynamic-feature' => FeatureState::dynamic(),
122+
],
123+
'feature-flags.always_on' => ['staging'],
124+
]);
125+
126+
(new SyncFeaturesAction)->__invoke();
127+
128+
$this->assertDatabaseCount('features', 3);
129+
130+
$this->assertDatabaseHas('features', [
131+
'name' => 'some-feature',
132+
'state' => FeatureState::on(),
133+
]);
134+
135+
$this->assertDatabaseHas('features', [
136+
'name' => 'some-other-feature',
137+
'state' => FeatureState::on(),
138+
]);
139+
140+
$this->assertDatabaseHas('features', [
141+
'name' => 'some-dynamic-feature',
142+
'state' => FeatureState::on(),
143+
]);
144+
});
145+
146+
it('does not override the state when the always on environment does not match', function () {
147+
app()->detectEnvironment(fn () => 'production');
148+
149+
config([
150+
'feature-flags.features' => [
151+
'some-feature' => FeatureState::on(),
152+
'some-other-feature' => FeatureState::off(),
153+
'some-dynamic-feature' => FeatureState::dynamic(),
154+
],
155+
'feature-flags.always_on' => ['local', 'staging'],
156+
]);
157+
158+
(new SyncFeaturesAction)->__invoke();
159+
160+
$this->assertDatabaseCount('features', 3);
161+
162+
$this->assertDatabaseHas('features', [
163+
'name' => 'some-feature',
164+
'state' => FeatureState::on(),
165+
]);
166+
167+
$this->assertDatabaseHas('features', [
168+
'name' => 'some-other-feature',
169+
'state' => FeatureState::off(),
170+
]);
171+
172+
$this->assertDatabaseHas('features', [
173+
'name' => 'some-dynamic-feature',
174+
'state' => FeatureState::dynamic(),
175+
]);
176+
});

0 commit comments

Comments
 (0)