Skip to content

Commit

Permalink
Merge pull request #12 from friendsofcat/add-exists-helper-method
Browse files Browse the repository at this point in the history
Add a exists() method for checking if a feature flag exists
  • Loading branch information
luisdalmolin authored May 1, 2020
2 parents d816c88 + 8030e22 commit e08cec7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ LARAVEL_FEATURE_FLAG_VIEW="layouts.default"

Visit `/admin/feature_flags` to manage features via the UI.

### Checking if a feature flag exists
For this you can use the exists() method

~~~
if(\FriendsOfCat\LaravelFeatureFlags\Feature::exists('see-twitter-field'))
{
//do something
}
~~~


## Usage Non Auth

Expand Down
12 changes: 12 additions & 0 deletions src/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ public function __construct()
$this->instance = Cache::get(Feature::FEATURE_FLAG_CACHE_KEY, []);
}

/**
* Check if a feature flag exists.
*
* @param string $featureKey
* @param mixed $variant (optional)
* @return bool
*/
public function exists($featureKey)
{
return isset($this->instance[$featureKey]);
}

/**
* Check if a feature flag is enabled.
*
Expand Down
27 changes: 27 additions & 0 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use FriendsOfCat\LaravelFeatureFlags\FeatureFlag;
use FriendsOfCat\LaravelFeatureFlags\FeatureFlagUser;
use FriendsOfCat\LaravelFeatureFlags\FeatureFlagHelper;
use FriendsOfCat\LaravelFeatureFlags\FeatureFlagsForJavascript;
use Illuminate\Foundation\Testing\DatabaseTransactions;

/**
Expand All @@ -17,6 +18,32 @@ class FeatureTest extends TestCase
{
use DatabaseTransactions, FeatureFlagHelper;

/**
* @test
* @covers ::isEnabled
*/
public function testExists()
{
$user = factory(FeatureFlagUser::class)->create();
$this->be($user);

factory(FeatureFlag::class)->create([
'key' => 'feature_1',
'variants' => 'on'
]);

factory(FeatureFlag::class)->create([
'key' => 'feature_2',
'variants' => 'off'
]);

FeatureFlagsForJavascript::get();

$this->assertTrue((new Feature)->exists('feature_1'));
$this->assertTrue((new Feature)->exists('feature_2'));
$this->assertFalse((new Feature)->exists('feature_3'));
}

/**
* @test
* @covers ::isEnabled
Expand Down

0 comments on commit e08cec7

Please sign in to comment.