Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Cache Command - Option to remove tagged items #13927

Merged
merged 2 commits into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/Illuminate/Cache/Console/ClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Cache\CacheManager;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class ClearCommand extends Command
Expand Down Expand Up @@ -47,17 +48,26 @@ public function __construct(CacheManager $cache)
*
* @return void
*/
public function fire()
public function handle()
{
$storeName = $this->argument('store');
$storeName = $this->hasArgument('store') ? $this->argument('store') : null;
$tagsNames = $this->hasOption('tags') ? $this->option('tags') : null;

$this->laravel['events']->fire('cache:clearing', [$storeName]);
$store = $this->cache->store($storeName);

$this->cache->store($storeName)->flush();
$this->laravel['events']->fire('cache:clearing', [$storeName, $tagsNames]);

$this->laravel['events']->fire('cache:cleared', [$storeName]);
if (! is_null($tagsNames)) {
$store->tags(explode(',', $tagsNames))->flush();

$this->info('Application cache cleared!');
$this->info(sprintf('Application cache tags "%s" cleared!', $tagsNames));
} else {
$store->flush();

$this->info('Application cache cleared!');
}

$this->laravel['events']->fire('cache:cleared', [$storeName, $tagsNames]);
}

/**
Expand All @@ -71,4 +81,16 @@ protected function getArguments()
['store', InputArgument::OPTIONAL, 'The name of the store you would like to clear.'],
];
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['tags', null, InputOption::VALUE_OPTIONAL, 'The cache tags you would like to clear.', null],
];
}
}
42 changes: 39 additions & 3 deletions tests/Cache/ClearCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function tearDown()
m::close();
}

public function testClearWithNoStoreOption()
public function testClearWithNoStoreArgument()
{
$command = new ClearCommandTestStub(
$cacheManager = m::mock('Illuminate\Cache\CacheManager')
Expand All @@ -28,7 +28,7 @@ public function testClearWithNoStoreOption()
$this->runCommand($command);
}

public function testClearWithStoreOption()
public function testClearWithStoreArgument()
{
$command = new ClearCommandTestStub(
$cacheManager = m::mock('Illuminate\Cache\CacheManager')
Expand All @@ -48,7 +48,7 @@ public function testClearWithStoreOption()
/**
* @expectedException InvalidArgumentException
*/
public function testClearWithInvalidStoreOption()
public function testClearWithInvalidStoreArgument()
{
$command = new ClearCommandTestStub(
$cacheManager = m::mock('Illuminate\Cache\CacheManager')
Expand All @@ -65,6 +65,42 @@ public function testClearWithInvalidStoreOption()
$this->runCommand($command, ['store' => 'bar']);
}

public function testClearWithTagsOption()
{
$command = new ClearCommandTestStub(
$cacheManager = m::mock('Illuminate\Cache\CacheManager')
);

$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');

$app = new Application();
$command->setLaravel($app);

$cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository);
$cacheRepository->shouldReceive('tags')->once()->with(['foo', 'bar'])->andReturn($cacheRepository);
$cacheRepository->shouldReceive('flush')->once();

$this->runCommand($command, ['--tags' => 'foo,bar']);
}

public function testClearWithStoreArgumentAndTagsOption()
{
$command = new ClearCommandTestStub(
$cacheManager = m::mock('Illuminate\Cache\CacheManager')
);

$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');

$app = new Application();
$command->setLaravel($app);

$cacheManager->shouldReceive('store')->once()->with('redis')->andReturn($cacheRepository);
$cacheRepository->shouldReceive('tags')->once()->with(['foo'])->andReturn($cacheRepository);
$cacheRepository->shouldReceive('flush')->once();

$this->runCommand($command, ['store' => 'redis', '--tags' => 'foo']);
}

protected function runCommand($command, $input = [])
{
return $command->run(new Symfony\Component\Console\Input\ArrayInput($input), new Symfony\Component\Console\Output\NullOutput);
Expand Down