|
1 | 1 | <?php |
2 | 2 |
|
3 | | -use Illuminate\Support\Facades\Route; |
4 | 3 | use Codinglabs\FeatureFlags\Models\Feature; |
5 | 4 | use Codinglabs\FeatureFlags\Enums\FeatureState; |
6 | 5 | use Codinglabs\FeatureFlags\Facades\FeatureFlag; |
7 | | -use Codinglabs\FeatureFlags\Middleware\VerifyFeatureIsOn; |
| 6 | +use Codinglabs\FeatureFlags\Events\FeatureUpdatedEvent; |
8 | 7 | use Codinglabs\FeatureFlags\Exceptions\MissingFeatureException; |
9 | 8 |
|
10 | 9 | beforeEach(function () { |
|
13 | 12 | 'feature-flags.cache_prefix' => 'testing', |
14 | 13 | ]); |
15 | 14 |
|
16 | | - Route::get('test-middleware', function () { |
17 | | - return 'ok'; |
18 | | - })->middleware(VerifyFeatureIsOn::class . ':some-feature'); |
19 | | - |
20 | 15 | cache()->store('array')->clear(); |
21 | 16 | }); |
22 | 17 |
|
23 | 18 | afterEach(function () { |
24 | 19 | FeatureFlag::reset(); |
25 | 20 | }); |
26 | 21 |
|
| 22 | +it('throws an exception if casting to a feature state that does not exist', function () { |
| 23 | + $this->expectException(\InvalidArgumentException::class); |
| 24 | + |
| 25 | + Feature::factory()->create([ |
| 26 | + 'name' => 'some-feature', |
| 27 | + 'state' => 'foo', |
| 28 | + ]); |
| 29 | +}); |
| 30 | + |
27 | 31 | it('throws an exception if calling isOn on a feature that does not exist', function () { |
28 | 32 | $this->expectException(MissingFeatureException::class); |
29 | 33 |
|
|
146 | 150 | ->and(FeatureFlag::getState('some-on-feature'))->toBe(FeatureState::on()); |
147 | 151 | }); |
148 | 152 |
|
149 | | -it('can update a features state', function () { |
| 153 | +it('can turn on a feature', function () { |
150 | 154 | Event::fake(); |
151 | 155 |
|
152 | 156 | Feature::factory()->create([ |
|
156 | 160 |
|
157 | 161 | cache()->store('array')->set('testing.some-feature', 'off'); |
158 | 162 |
|
159 | | - FeatureFlag::updateFeatureState('some-feature', FeatureState::on()); |
| 163 | + FeatureFlag::turnOn('some-feature'); |
160 | 164 |
|
161 | | - Event::assertDispatched(\Codinglabs\FeatureFlags\Events\FeatureUpdatedEvent::class); |
| 165 | + Event::assertDispatched(FeatureUpdatedEvent::class); |
162 | 166 | expect(FeatureFlag::isOn('some-feature'))->toBeTrue() |
163 | 167 | ->and(FeatureFlag::isOff('some-feature'))->toBeFalse() |
164 | 168 | ->and(cache()->store('array')->get('testing.some-feature'))->toBe(FeatureState::on()->value); |
165 | 169 | }); |
166 | 170 |
|
167 | | -it('uses the default cache store when cache store has not been set', function () { |
168 | | - config(['cache.default' => 'file']); |
169 | | - |
170 | | - config(['feature-flags.cache_store' => env('FEATURES_CACHE_STORE', config('cache.default'))]); |
| 171 | +it('can turn off a feature', function () { |
| 172 | + Event::fake(); |
171 | 173 |
|
172 | | - expect(config('feature-flags.cache_store'))->toBe('file'); |
173 | | -}); |
| 174 | + Feature::factory()->create([ |
| 175 | + 'name' => 'some-feature', |
| 176 | + 'state' => FeatureState::on() |
| 177 | + ]); |
174 | 178 |
|
175 | | -it('returns a 500 status when a feature does not exist', function () { |
176 | | - $this->withoutExceptionHandling(); |
| 179 | + cache()->store('array')->set('testing.some-feature', 'on'); |
177 | 180 |
|
178 | | - $this->expectException(MissingFeatureException::class); |
| 181 | + FeatureFlag::turnOff('some-feature'); |
179 | 182 |
|
180 | | - $this->get('test-middleware') |
181 | | - ->assertStatus(500); |
| 183 | + Event::assertDispatched(FeatureUpdatedEvent::class); |
| 184 | + expect(FeatureFlag::isOn('some-feature'))->toBeFalse() |
| 185 | + ->and(FeatureFlag::isOff('some-feature'))->toBeTrue() |
| 186 | + ->and(cache()->store('array')->get('testing.some-feature'))->toBe(FeatureState::off()->value); |
182 | 187 | }); |
183 | 188 |
|
184 | | -it('returns a 404 status when a feature is off', function () { |
| 189 | +it('can make a feature dynamic', function () { |
| 190 | + Event::fake(); |
| 191 | + |
185 | 192 | Feature::factory()->create([ |
186 | 193 | 'name' => 'some-feature', |
187 | | - 'state' => FeatureState::off() |
| 194 | + 'state' => FeatureState::on() |
188 | 195 | ]); |
189 | 196 |
|
190 | | - $this->get('test-middleware') |
191 | | - ->assertStatus(404); |
192 | | -}); |
| 197 | + cache()->store('array')->set('testing.some-feature', 'on'); |
193 | 198 |
|
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 | + FeatureFlag::makeDynamic('some-feature'); |
199 | 200 |
|
200 | | - $this->get('test-middleware') |
201 | | - ->assertStatus(404); |
| 201 | + Event::assertDispatched(FeatureUpdatedEvent::class); |
| 202 | + expect(FeatureFlag::isOn('some-feature'))->toBeFalse() |
| 203 | + ->and(FeatureFlag::isOff('some-feature'))->toBeTrue() |
| 204 | + ->and(cache()->store('array')->get('testing.some-feature'))->toBe(FeatureState::dynamic()->value); |
202 | 205 | }); |
203 | 206 |
|
204 | | -it('returns an ok status when a feature is dynamic and enabled', function () { |
| 207 | +it('can update a features state', function () { |
| 208 | + Event::fake(); |
| 209 | + |
205 | 210 | Feature::factory()->create([ |
206 | 211 | 'name' => 'some-feature', |
207 | | - 'state' => FeatureState::dynamic() |
| 212 | + 'state' => FeatureState::off() |
208 | 213 | ]); |
209 | 214 |
|
210 | | - FeatureFlag::registerDynamicHandler('some-feature', fn ($feature) => true); |
| 215 | + cache()->store('array')->set('testing.some-feature', 'off'); |
211 | 216 |
|
212 | | - $this->get('test-middleware') |
213 | | - ->assertOk(); |
| 217 | + FeatureFlag::updateFeatureState('some-feature', FeatureState::on()); |
| 218 | + |
| 219 | + Event::assertDispatched(FeatureUpdatedEvent::class); |
| 220 | + expect(FeatureFlag::isOn('some-feature'))->toBeTrue() |
| 221 | + ->and(FeatureFlag::isOff('some-feature'))->toBeFalse() |
| 222 | + ->and(cache()->store('array')->get('testing.some-feature'))->toBe(FeatureState::on()->value); |
214 | 223 | }); |
215 | 224 |
|
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 | | - ]); |
| 225 | +it('uses the default cache store when cache store has not been set', function () { |
| 226 | + config(['cache.default' => 'file']); |
221 | 227 |
|
222 | | - $this->get('test-middleware') |
223 | | - ->assertOk(); |
| 228 | + config(['feature-flags.cache_store' => env('FEATURES_CACHE_STORE', config('cache.default'))]); |
| 229 | + |
| 230 | + expect(config('feature-flags.cache_store'))->toBe('file'); |
224 | 231 | }); |
0 commit comments