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.6] Fix clearing the cache without a cache directory #23538

Merged
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
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