Skip to content

Commit

Permalink
Added test for User implementing our Interface
Browse files Browse the repository at this point in the history
  • Loading branch information
cvyz committed Sep 28, 2020
1 parent 16488d8 commit c9e8263
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/FeatureFlagUserRoleTrait.php
@@ -0,0 +1,11 @@
<?php

namespace FriendsOfCat\LaravelFeatureFlags;

trait FeatureFlagUserRoleTrait
{
public function getFieldValueForFeatureFlags(string $fieldName): ?array
{
return (array) json_decode($this->$fieldName, true);
}
}
61 changes: 61 additions & 0 deletions tests/FeatureFlagsEnablerTest.php
@@ -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'
]
];
}
}
22 changes: 22 additions & 0 deletions tests/fixtures/UserWithFeatureFlagsEnablerInterface.php
@@ -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);
}
}

0 comments on commit c9e8263

Please sign in to comment.