forked from alfred-nutile-inc/laravel-feature-flag
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test for User implementing our Interface
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 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
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,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); | ||
} | ||
} |