Skip to content

Commit

Permalink
Add the ability to whitelist a feature for a user
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaspaul committed Mar 29, 2017
1 parent 795a0cf commit 8c9a157
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 1 deletion.
57 changes: 57 additions & 0 deletions src/Console/AddUserCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Jaspaul\LaravelRollout\Console;

use Opensoft\Rollout\Rollout;
use Illuminate\Console\Command;
use Jaspaul\LaravelRollout\Helpers\User;

class AddUserCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'rollout:add-user {feature} {user}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Adds the provided user id to the feature.';

/**
* The rollout service.
*
* @var \Opensoft\Rollout\Rollout
*/
protected $rollout;

/**
* Initialize our create feature command with an instance of the rollout
* service.
*
* @param \Opensoft\Rollout\Rollout $rollout
* The rollout service.
*/
public function __construct(Rollout $rollout)
{
parent::__construct();
$this->rollout = $rollout;
}

/**
* Creates the provided feature.
*
* @return void
*/
public function handle()
{
$name = $this->argument('feature');
$id = $this->argument('user');

$feature = $this->rollout->activateUser($name, new User($id));
}
}
37 changes: 37 additions & 0 deletions src/Helpers/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Jaspaul\LaravelRollout\Helpers;

use Jaspaul\LaravelRollout\Contracts\User as Contract;

class User implements Contract
{
/**
* The id of the user.
*
* @var string
*/
protected $id;

/**
* Constructs our user helper with an id.
*
* @param string $id
* The id of the user.
*/
public function __construct(string $id)
{
$this->id = $id;
}

/**
* The identifier to use with rollout.
*
* @return string
* The id.
*/
public function getRolloutIdentifier()
{
return $this->id;
}
}
2 changes: 2 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Jaspaul\LaravelRollout\Drivers\Cache;
use Jaspaul\LaravelRollout\Console\ListCommand;
use Jaspaul\LaravelRollout\Console\CreateCommand;
use Jaspaul\LaravelRollout\Console\AddUserCommand;
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;

class ServiceProvider extends IlluminateServiceProvider
Expand All @@ -22,6 +23,7 @@ public function boot()
});

$this->commands([
AddUserCommand::class,
CreateCommand::class,
ListCommand::class
]);
Expand Down
36 changes: 36 additions & 0 deletions tests/Console/AddUserCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Tests\Drivers;

use Mockery;
use Tests\TestCase;
use Opensoft\Rollout\Rollout;
use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\Repository;
use Illuminate\Support\Facades\Artisan;
use Jaspaul\LaravelRollout\Drivers\Cache;
use Symfony\Component\Console\Input\Input;
use Symfony\Component\Console\Output\Output;
use Jaspaul\LaravelRollout\Console\CreateCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;

class AddUserCommandTest extends TestCase
{
/**
* @test
*/
function running_the_command_with_a_feature_will_create_the_corresponding_feature()
{
Artisan::call('rollout:add-user', [
'feature' => 'derp',
'user' => 1
]);

$store = app()->make('cache.store')->getStore();

$this->assertEquals('derp', $store->get('rollout.feature:__features__'));
$this->assertEquals('0|1||', $store->get('rollout.feature:derp'));
}
}
21 changes: 21 additions & 0 deletions tests/Helpers/UserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Tests\Helpers;

use Tests\TestCase;
use Jaspaul\LaravelRollout\Helpers\User;

class UserTest extends TestCase
{
/**
* @test
*/
function get_rollout_identifier_returns_the_id_the_user_was_constructed_with()
{
$id = "id";

$user = new User($id);

$this->assertEquals($id, $user->getRolloutIdentifier());
}
}
3 changes: 2 additions & 1 deletion tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Jaspaul\LaravelRollout\ServiceProvider;
use Jaspaul\LaravelRollout\Console\ListCommand;
use Jaspaul\LaravelRollout\Console\CreateCommand;
use Jaspaul\LaravelRollout\Console\AddUserCommand;
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;

class ServiceProviderTest extends TestCase
Expand Down Expand Up @@ -62,7 +63,7 @@ function booting_registers_our_commands()
$serviceProvider->boot();

$this->assertEquals(
[CreateCommand::class, ListCommand::class],
[AddUserCommand::class, CreateCommand::class, ListCommand::class],
$serviceProvider->commands
);
}
Expand Down

0 comments on commit 8c9a157

Please sign in to comment.