Skip to content

Commit 5153222

Browse files
authored
Merge pull request #3 from codinglabsau/st/sc-7265-test-feature-middleware-per-laravel-roles
add tests for middleware
2 parents 7b90bb2 + cf0d91a commit 5153222

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

tests/FeaturesTest.php

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

3+
use Illuminate\Support\Facades\Route;
34
use Codinglabs\FeatureFlags\Models\Feature;
45
use Codinglabs\FeatureFlags\Enums\FeatureState;
56
use Codinglabs\FeatureFlags\Facades\FeatureFlag;
7+
use Codinglabs\FeatureFlags\Middleware\VerifyFeatureIsOn;
68
use Codinglabs\FeatureFlags\Exceptions\MissingFeatureException;
79

810
beforeEach(function () {
@@ -11,6 +13,10 @@
1113
'feature-flags.cache_prefix' => 'testing',
1214
]);
1315

16+
Route::get('test-middleware', function () {
17+
return 'ok';
18+
})->middleware(VerifyFeatureIsOn::class . ':some-feature');
19+
1420
cache()->store('array')->clear();
1521
});
1622

@@ -76,9 +82,7 @@
7682
'state' => FeatureState::dynamic(),
7783
]);
7884

79-
FeatureFlag::registerDynamicHandler('some-feature', function ($feature) {
80-
return true;
81-
});
85+
FeatureFlag::registerDynamicHandler('some-feature', fn ($feature) => true);
8286

8387
expect(FeatureFlag::isOn('some-feature'))->toBeTrue()
8488
->and(FeatureFlag::isOff('some-feature'))->toBeFalse()
@@ -91,9 +95,7 @@
9195
'state' => FeatureState::dynamic(),
9296
]);
9397

94-
FeatureFlag::registerDynamicHandler('some-feature', function ($feature) {
95-
return false;
96-
});
98+
FeatureFlag::registerDynamicHandler('some-feature', fn ($feature) => false);
9799

98100
expect(FeatureFlag::isOn('some-feature'))->toBeFalse()
99101
->and(FeatureFlag::isOff('some-feature'))->toBeTrue()
@@ -169,3 +171,54 @@
169171

170172
expect(config('feature-flags.cache_store'))->toBe('file');
171173
});
174+
175+
it('returns a 500 status when a feature does not exist', function () {
176+
$this->withoutExceptionHandling();
177+
178+
$this->expectException(MissingFeatureException::class);
179+
180+
$this->get('test-middleware')
181+
->assertStatus(500);
182+
});
183+
184+
it('returns a 404 status when a feature is off', function () {
185+
Feature::factory()->create([
186+
'name' => 'some-feature',
187+
'state' => FeatureState::off()
188+
]);
189+
190+
$this->get('test-middleware')
191+
->assertStatus(404);
192+
});
193+
194+
it('returns a 404 status when a feature is dynamic', function () {
195+
Feature::factory()->create([
196+
'name' => 'some-feature',
197+
'state' => FeatureState::dynamic()
198+
]);
199+
200+
$this->get('test-middleware')
201+
->assertStatus(404);
202+
});
203+
204+
it('returns an ok status when a feature is dynamic and enabled', function () {
205+
Feature::factory()->create([
206+
'name' => 'some-feature',
207+
'state' => FeatureState::dynamic()
208+
]);
209+
210+
FeatureFlag::registerDynamicHandler('some-feature', fn ($feature) => true);
211+
212+
$this->get('test-middleware')
213+
->assertOk();
214+
});
215+
216+
it('returns an ok status when a feature is on', function () {
217+
Feature::factory()->create([
218+
'name' => 'some-feature',
219+
'state' => FeatureState::on()
220+
]);
221+
222+
$this->get('test-middleware')
223+
->assertOk();
224+
});

0 commit comments

Comments
 (0)