Skip to content

add tests for middleware #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 59 additions & 6 deletions tests/FeaturesTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

use Illuminate\Support\Facades\Route;
use Codinglabs\FeatureFlags\Models\Feature;
use Codinglabs\FeatureFlags\Enums\FeatureState;
use Codinglabs\FeatureFlags\Facades\FeatureFlag;
use Codinglabs\FeatureFlags\Middleware\VerifyFeatureIsOn;
use Codinglabs\FeatureFlags\Exceptions\MissingFeatureException;

beforeEach(function () {
Expand All @@ -11,6 +13,10 @@
'feature-flags.cache_prefix' => 'testing',
]);

Route::get('test-middleware', function () {
return 'ok';
})->middleware(VerifyFeatureIsOn::class . ':some-feature');

cache()->store('array')->clear();
});

Expand Down Expand Up @@ -76,9 +82,7 @@
'state' => FeatureState::dynamic(),
]);

FeatureFlag::registerDynamicHandler('some-feature', function ($feature) {
return true;
});
FeatureFlag::registerDynamicHandler('some-feature', fn ($feature) => true);

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

FeatureFlag::registerDynamicHandler('some-feature', function ($feature) {
return false;
});
FeatureFlag::registerDynamicHandler('some-feature', fn ($feature) => false);

expect(FeatureFlag::isOn('some-feature'))->toBeFalse()
->and(FeatureFlag::isOff('some-feature'))->toBeTrue()
Expand Down Expand Up @@ -169,3 +171,54 @@

expect(config('feature-flags.cache_store'))->toBe('file');
});

it('returns a 500 status when a feature does not exist', function () {
$this->withoutExceptionHandling();

$this->expectException(MissingFeatureException::class);

$this->get('test-middleware')
->assertStatus(500);
});

it('returns a 404 status when a feature is off', function () {
Feature::factory()->create([
'name' => 'some-feature',
'state' => FeatureState::off()
]);

$this->get('test-middleware')
->assertStatus(404);
});

it('returns a 404 status when a feature is dynamic', function () {
Feature::factory()->create([
'name' => 'some-feature',
'state' => FeatureState::dynamic()
]);

$this->get('test-middleware')
->assertStatus(404);
});

it('returns an ok status when a feature is dynamic and enabled', function () {
Feature::factory()->create([
'name' => 'some-feature',
'state' => FeatureState::dynamic()
]);

FeatureFlag::registerDynamicHandler('some-feature', fn ($feature) => true);

$this->get('test-middleware')
->assertOk();
});

it('returns an ok status when a feature is on', function () {
Feature::factory()->create([
'name' => 'some-feature',
'state' => FeatureState::on()
]);

$this->get('test-middleware')
->assertOk();
});