-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from codinglabsau/st/sc-7287-move-syncfeaturesa…
…ction-over-to-package-with St/sc 7287 move syncfeaturesaction over to package with
- Loading branch information
Showing
8 changed files
with
403 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Codinglabs\FeatureFlags\Actions; | ||
|
||
use Codinglabs\FeatureFlags\Models\Feature; | ||
use Codinglabs\FeatureFlags\Enums\FeatureState; | ||
|
||
class SyncFeaturesAction | ||
{ | ||
public function __invoke(): void | ||
{ | ||
$features = collect(config('feature-flags.features')) | ||
->map(fn ($state, $name) => [ | ||
'name' => $name, | ||
'state' => app()->environment(config('feature-flags.always_on', [])) | ||
? FeatureState::on() | ||
: $state | ||
]); | ||
|
||
$featureModels = Feature::all(); | ||
|
||
$featureModels->whereNotIn('name', $features->pluck('name')) | ||
->each(fn (Feature $feature) => $feature->delete()); | ||
|
||
$features->whereNotIn('name', $featureModels->pluck('name')) | ||
->each(fn (array $feature) => Feature::create($feature)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
use Codinglabs\FeatureFlags\Models\Feature; | ||
use Codinglabs\FeatureFlags\Enums\FeatureState; | ||
use Codinglabs\FeatureFlags\Facades\FeatureFlag; | ||
use Illuminate\Foundation\Testing\Concerns\InteractsWithViews; | ||
|
||
uses(InteractsWithViews::class); | ||
|
||
beforeEach(function () { | ||
config([ | ||
'feature-flags.cache_store' => 'array', | ||
'feature-flags.cache_prefix' => 'testing', | ||
]); | ||
|
||
cache()->store('array')->clear(); | ||
}); | ||
|
||
afterEach(function () { | ||
FeatureFlag::reset(); | ||
}); | ||
|
||
it('does not reveal things when feature is off', function () { | ||
Feature::factory()->create([ | ||
'name' => 'some-feature', | ||
'state' => FeatureState::off() | ||
]); | ||
|
||
$view = $this->blade("@feature('some-feature') secret things @endfeature"); | ||
|
||
$view->assertDontSee('secret things'); | ||
}); | ||
|
||
it('reveals things when feature is on ', function () { | ||
Feature::factory()->create([ | ||
'name' => 'some-feature', | ||
'state' => FeatureState::on() | ||
]); | ||
|
||
$view = $this->blade("@feature('some-feature') secret things @endfeature"); | ||
|
||
$view->assertSee('secret things'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?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 () { | ||
config([ | ||
'feature-flags.cache_store' => 'array', | ||
'feature-flags.cache_prefix' => 'testing', | ||
]); | ||
|
||
Route::get('test-middleware', function () { | ||
return 'ok'; | ||
})->middleware(VerifyFeatureIsOn::class . ':some-feature'); | ||
|
||
cache()->store('array')->clear(); | ||
}); | ||
|
||
afterEach(function () { | ||
FeatureFlag::reset(); | ||
}); | ||
|
||
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(); | ||
}); |
Oops, something went wrong.