Skip to content

Commit 8c9a157

Browse files
committed
Add the ability to whitelist a feature for a user
1 parent 795a0cf commit 8c9a157

File tree

6 files changed

+155
-1
lines changed

6 files changed

+155
-1
lines changed

src/Console/AddUserCommand.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Jaspaul\LaravelRollout\Console;
4+
5+
use Opensoft\Rollout\Rollout;
6+
use Illuminate\Console\Command;
7+
use Jaspaul\LaravelRollout\Helpers\User;
8+
9+
class AddUserCommand extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'rollout:add-user {feature} {user}';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Adds the provided user id to the feature.';
24+
25+
/**
26+
* The rollout service.
27+
*
28+
* @var \Opensoft\Rollout\Rollout
29+
*/
30+
protected $rollout;
31+
32+
/**
33+
* Initialize our create feature command with an instance of the rollout
34+
* service.
35+
*
36+
* @param \Opensoft\Rollout\Rollout $rollout
37+
* The rollout service.
38+
*/
39+
public function __construct(Rollout $rollout)
40+
{
41+
parent::__construct();
42+
$this->rollout = $rollout;
43+
}
44+
45+
/**
46+
* Creates the provided feature.
47+
*
48+
* @return void
49+
*/
50+
public function handle()
51+
{
52+
$name = $this->argument('feature');
53+
$id = $this->argument('user');
54+
55+
$feature = $this->rollout->activateUser($name, new User($id));
56+
}
57+
}

src/Helpers/User.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Jaspaul\LaravelRollout\Helpers;
4+
5+
use Jaspaul\LaravelRollout\Contracts\User as Contract;
6+
7+
class User implements Contract
8+
{
9+
/**
10+
* The id of the user.
11+
*
12+
* @var string
13+
*/
14+
protected $id;
15+
16+
/**
17+
* Constructs our user helper with an id.
18+
*
19+
* @param string $id
20+
* The id of the user.
21+
*/
22+
public function __construct(string $id)
23+
{
24+
$this->id = $id;
25+
}
26+
27+
/**
28+
* The identifier to use with rollout.
29+
*
30+
* @return string
31+
* The id.
32+
*/
33+
public function getRolloutIdentifier()
34+
{
35+
return $this->id;
36+
}
37+
}

src/ServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Jaspaul\LaravelRollout\Drivers\Cache;
77
use Jaspaul\LaravelRollout\Console\ListCommand;
88
use Jaspaul\LaravelRollout\Console\CreateCommand;
9+
use Jaspaul\LaravelRollout\Console\AddUserCommand;
910
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
1011

1112
class ServiceProvider extends IlluminateServiceProvider
@@ -22,6 +23,7 @@ public function boot()
2223
});
2324

2425
$this->commands([
26+
AddUserCommand::class,
2527
CreateCommand::class,
2628
ListCommand::class
2729
]);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Tests\Drivers;
4+
5+
use Mockery;
6+
use Tests\TestCase;
7+
use Opensoft\Rollout\Rollout;
8+
use Illuminate\Cache\ArrayStore;
9+
use Illuminate\Cache\Repository;
10+
use Illuminate\Support\Facades\Artisan;
11+
use Jaspaul\LaravelRollout\Drivers\Cache;
12+
use Symfony\Component\Console\Input\Input;
13+
use Symfony\Component\Console\Output\Output;
14+
use Jaspaul\LaravelRollout\Console\CreateCommand;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
18+
19+
class AddUserCommandTest extends TestCase
20+
{
21+
/**
22+
* @test
23+
*/
24+
function running_the_command_with_a_feature_will_create_the_corresponding_feature()
25+
{
26+
Artisan::call('rollout:add-user', [
27+
'feature' => 'derp',
28+
'user' => 1
29+
]);
30+
31+
$store = app()->make('cache.store')->getStore();
32+
33+
$this->assertEquals('derp', $store->get('rollout.feature:__features__'));
34+
$this->assertEquals('0|1||', $store->get('rollout.feature:derp'));
35+
}
36+
}

tests/Helpers/UserTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Tests\Helpers;
4+
5+
use Tests\TestCase;
6+
use Jaspaul\LaravelRollout\Helpers\User;
7+
8+
class UserTest extends TestCase
9+
{
10+
/**
11+
* @test
12+
*/
13+
function get_rollout_identifier_returns_the_id_the_user_was_constructed_with()
14+
{
15+
$id = "id";
16+
17+
$user = new User($id);
18+
19+
$this->assertEquals($id, $user->getRolloutIdentifier());
20+
}
21+
}

tests/ServiceProviderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Jaspaul\LaravelRollout\ServiceProvider;
1010
use Jaspaul\LaravelRollout\Console\ListCommand;
1111
use Jaspaul\LaravelRollout\Console\CreateCommand;
12+
use Jaspaul\LaravelRollout\Console\AddUserCommand;
1213
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
1314

1415
class ServiceProviderTest extends TestCase
@@ -62,7 +63,7 @@ function booting_registers_our_commands()
6263
$serviceProvider->boot();
6364

6465
$this->assertEquals(
65-
[CreateCommand::class, ListCommand::class],
66+
[AddUserCommand::class, CreateCommand::class, ListCommand::class],
6667
$serviceProvider->commands
6768
);
6869
}

0 commit comments

Comments
 (0)