Skip to content

Commit

Permalink
Merge branch 'fix-clear-cache-without-cache-directory' of https://git…
Browse files Browse the repository at this point in the history
…hub.com/Jalle19/framework into Jalle19-fix-clear-cache-without-cache-directory
  • Loading branch information
taylorotwell committed Mar 14, 2018
2 parents 8eaa147 + 3737c01 commit 8e59195
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 87 deletions.
9 changes: 8 additions & 1 deletion src/Illuminate/Cache/Console/ClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ public function handle()
*/
public function flushFacades()
{
foreach ($this->files->files(storage_path('framework/cache')) as $file) {
// Applications that don't use the file cache may not have the directory
$storagePath = storage_path('framework/cache');

if (! $this->files->exists($storagePath)) {
return;
}

foreach ($this->files->files($storagePath) as $file) {
if (preg_match('/facade-.*\.php$/', $file)) {
$this->files->delete($file);
}
Expand Down
170 changes: 84 additions & 86 deletions tests/Cache/ClearCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,140 +4,138 @@

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\Cache\CacheManager;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Application;
use Illuminate\Cache\Console\ClearCommand;
use Illuminate\Contracts\Cache\Repository;

class ClearCommandTest extends TestCase
{
/**
* @var ClearCommandTestStub
*/
private $command;

/**
* @var CacheManager|m\Mock
*/
private $cacheManager;

/**
* @var Filesystem|m\Mock
*/
private $files;

/**
* @var Repository|m\Mock
*/
private $cacheRepository;

/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();

$this->cacheManager = m::mock('Illuminate\Cache\CacheManager');
$this->files = m::mock('Illuminate\Filesystem\Filesystem');
$this->cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
$this->command = new ClearCommandTestStub($this->cacheManager, $this->files);

$app = new Application;
$app['path.storage'] = __DIR__;
$this->command->setLaravel($app);
}

public function tearDown()
{
m::close();
}

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

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

$app = new Application;
$app['path.storage'] = __DIR__;
$command->setLaravel($app);
$files->shouldReceive('files')->andReturn([]);
$this->files->shouldReceive('exists')->andReturn(true);
$this->files->shouldReceive('files')->andReturn([]);

$cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository);
$cacheRepository->shouldReceive('flush')->once();
$this->cacheManager->shouldReceive('store')->once()->with(null)->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('flush')->once();

$this->runCommand($command);
$this->runCommand($this->command);
}

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

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

$app = new Application;
$app['path.storage'] = __DIR__;
$command->setLaravel($app);
$files->shouldReceive('files')->andReturn([]);
$this->files->shouldReceive('exists')->andReturn(true);
$this->files->shouldReceive('files')->andReturn([]);

$cacheManager->shouldReceive('store')->once()->with('foo')->andReturn($cacheRepository);
$cacheRepository->shouldReceive('flush')->once();
$this->cacheManager->shouldReceive('store')->once()->with('foo')->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('flush')->once();

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

/**
* @expectedException \InvalidArgumentException
*/
public function testClearWithInvalidStoreArgument()
{
$command = new ClearCommandTestStub(
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
$files = m::mock('Illuminate\Filesystem\Filesystem')
);
$this->files->shouldReceive('files')->andReturn([]);

$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
$this->cacheManager->shouldReceive('store')->once()->with('bar')->andThrow('InvalidArgumentException');
$this->cacheRepository->shouldReceive('flush')->never();

$app = new Application;
$app['path.storage'] = __DIR__;
$command->setLaravel($app);
$files->shouldReceive('files')->andReturn([]);

$cacheManager->shouldReceive('store')->once()->with('bar')->andThrow('InvalidArgumentException');
$cacheRepository->shouldReceive('flush')->never();

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

public function testClearWithTagsOption()
{
$command = new ClearCommandTestStub(
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
$files = m::mock('Illuminate\Filesystem\Filesystem')
);
$this->files->shouldReceive('exists')->andReturn(true);
$this->files->shouldReceive('files')->andReturn([]);

$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
$this->cacheManager->shouldReceive('store')->once()->with(null)->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('tags')->once()->with(['foo', 'bar'])->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('flush')->once();

$app = new Application;
$app['path.storage'] = __DIR__;
$command->setLaravel($app);
$files->shouldReceive('files')->andReturn([]);

$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']);
$this->runCommand($this->command, ['--tags' => 'foo,bar']);
}

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

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

$app = new Application;
$app['path.storage'] = __DIR__;
$command->setLaravel($app);
$files->shouldReceive('files')->andReturn([]);
$this->files->shouldReceive('exists')->andReturn(true);
$this->files->shouldReceive('files')->andReturn([]);

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

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

public function testClearWillClearsRealTimeFacades()
public function testClearWillClearRealTimeFacades()
{
$command = new ClearCommandTestStub(
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
$files = m::mock('Illuminate\Filesystem\Filesystem')
);
$this->cacheManager->shouldReceive('store')->once()->with(null)->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('flush')->once();

$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
$this->files->shouldReceive('exists')->andReturn(true);
$this->files->shouldReceive('files')->andReturn(['/facade-XXXX.php']);
$this->files->shouldReceive('delete')->with('/facade-XXXX.php')->once();

$app = new Application;
$app['path.storage'] = __DIR__;
$command->setLaravel($app);
$cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository);
$cacheRepository->shouldReceive('flush')->once();
$this->runCommand($this->command);
}

public function testClearWillNotClearRealTimeFacadesIfCacheDirectoryDoesntExist()
{
$this->cacheManager->shouldReceive('store')->once()->with(null)->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('flush')->once();

$files->shouldReceive('files')->andReturn(['/facade-XXXX.php']);
$files->shouldReceive('delete')->with('/facade-XXXX.php')->once();
// No files should be looped over and nothing should be deleted if the cache directory doesn't exist
$this->files->shouldReceive('exists')->andReturn(false);
$this->files->shouldNotReceive('files');
$this->files->shouldNotReceive('delete');

$this->runCommand($command);
$this->runCommand($this->command);
}

protected function runCommand($command, $input = [])
Expand Down

0 comments on commit 8e59195

Please sign in to comment.