|
1 | 1 | <?php
|
2 | 2 |
|
| 3 | +use Illuminate\Support\Facades\Route; |
3 | 4 | use Codinglabs\FeatureFlags\Models\Feature;
|
4 | 5 | use Codinglabs\FeatureFlags\Enums\FeatureState;
|
5 | 6 | use Codinglabs\FeatureFlags\Facades\FeatureFlag;
|
| 7 | +use Codinglabs\FeatureFlags\Middleware\VerifyFeatureIsOn; |
6 | 8 | use Codinglabs\FeatureFlags\Exceptions\MissingFeatureException;
|
7 | 9 |
|
8 | 10 | beforeEach(function () {
|
|
11 | 13 | 'feature-flags.cache_prefix' => 'testing',
|
12 | 14 | ]);
|
13 | 15 |
|
| 16 | + Route::get('test-middleware', function () { |
| 17 | + return 'ok'; |
| 18 | + })->middleware(VerifyFeatureIsOn::class . ':some-feature'); |
| 19 | + |
14 | 20 | cache()->store('array')->clear();
|
15 | 21 | });
|
16 | 22 |
|
|
76 | 82 | 'state' => FeatureState::dynamic(),
|
77 | 83 | ]);
|
78 | 84 |
|
79 |
| - FeatureFlag::registerDynamicHandler('some-feature', function ($feature) { |
80 |
| - return true; |
81 |
| - }); |
| 85 | + FeatureFlag::registerDynamicHandler('some-feature', fn ($feature) => true); |
82 | 86 |
|
83 | 87 | expect(FeatureFlag::isOn('some-feature'))->toBeTrue()
|
84 | 88 | ->and(FeatureFlag::isOff('some-feature'))->toBeFalse()
|
|
91 | 95 | 'state' => FeatureState::dynamic(),
|
92 | 96 | ]);
|
93 | 97 |
|
94 |
| - FeatureFlag::registerDynamicHandler('some-feature', function ($feature) { |
95 |
| - return false; |
96 |
| - }); |
| 98 | + FeatureFlag::registerDynamicHandler('some-feature', fn ($feature) => false); |
97 | 99 |
|
98 | 100 | expect(FeatureFlag::isOn('some-feature'))->toBeFalse()
|
99 | 101 | ->and(FeatureFlag::isOff('some-feature'))->toBeTrue()
|
|
169 | 171 |
|
170 | 172 | expect(config('feature-flags.cache_store'))->toBe('file');
|
171 | 173 | });
|
| 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