forked from alfred-nutile-inc/laravel-feature-flag
-
Notifications
You must be signed in to change notification settings - Fork 7
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 #16 from cvyz/allow_user_roles
Added functionality to support user roles in feature flag configuration
- Loading branch information
Showing
12 changed files
with
243 additions
and
7 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
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
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,11 @@ | ||
<?php | ||
|
||
namespace FriendsOfCat\LaravelFeatureFlags; | ||
|
||
trait FeatureFlagUserRoleTrait | ||
{ | ||
public function getFieldValueForFeatureFlags(string $fieldName): ?array | ||
{ | ||
return (array) json_decode($this->$fieldName, true); | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace FriendsOfCat\LaravelFeatureFlags; | ||
|
||
interface FeatureFlagsEnabler | ||
{ | ||
public function getFieldValueForFeatureFlags(string $fieldName): ?array; | ||
} |
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,61 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use Illuminate\Contracts\Auth\Access\Gate; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use FriendsOfCat\LaravelFeatureFlags\FeatureFlag; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use FriendsOfCat\LaravelFeatureFlags\FeatureFlagHelper; | ||
use Tests\fixtures\UserWithFeatureFlagsEnablerInterface; | ||
|
||
class FeatureFlagsEnablerTest extends TestCase | ||
{ | ||
use RefreshDatabase, WithFaker, FeatureFlagHelper; | ||
|
||
/** | ||
* @dataProvider validData | ||
*/ | ||
public function testItWorksWhenInterfaceImplented($userRoles) | ||
{ | ||
$user = new UserWithFeatureFlagsEnablerInterface([ | ||
'name' => $this->faker->word, | ||
'email' => $this->faker->email, | ||
'password' => bcrypt(str_random(25)), | ||
'roles' => json_encode($userRoles) | ||
]); | ||
$user->save(); | ||
|
||
$this->be($user); | ||
|
||
factory(FeatureFlag::class)->create( | ||
[ | ||
'key' => 'testing', | ||
'variants' => [ | ||
'roles' => [ | ||
'developer', 'admin' | ||
] | ||
] | ||
] | ||
); | ||
|
||
$this->registerFeatureFlags(); | ||
|
||
$this->assertTrue($this->app->get(Gate::class)->allows('feature-flag', 'testing')); | ||
} | ||
|
||
public function validData() | ||
{ | ||
return [ | ||
[ | ||
'admin' | ||
], | ||
[ | ||
['admin', 'developer'] | ||
], | ||
[ | ||
'developer' | ||
] | ||
]; | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace Tests\fixtures; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use FriendsOfCat\LaravelFeatureFlags\FeatureFlagsEnabler; | ||
use FriendsOfCat\LaravelFeatureFlags\FeatureFlagUserRoleTrait; | ||
use Illuminate\Auth\Authenticatable as AuthenticableTrait; | ||
|
||
class UserWithFeatureFlagsEnablerInterface extends Model implements Authenticatable, FeatureFlagsEnabler | ||
{ | ||
use AuthenticableTrait, FeatureFlagUserRoleTrait; | ||
|
||
protected $table = 'users'; | ||
protected $fillable = ['name', 'email', 'password', 'roles']; | ||
|
||
public function __construct(array $attributes = [ ]) | ||
{ | ||
parent::__construct($attributes); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/migrations/2020_09_11_1141689_add_roles_in_users_table.php
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,24 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class AddRolesInUsersTable extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->string('roles')->nullable(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
if (Schema::hasColumn('users', 'roles')) { | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->dropColumn('roles'); | ||
}); | ||
} | ||
} | ||
} |
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