1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
<?php
namespace Illuminate\Tests\Cache;
use Illuminate\Cache\CacheManager;
use Illuminate\Cache\Console\ClearCommand;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Application;
use InvalidArgumentException;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
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(): void
{
parent::setUp();
$this->cacheManager = m::mock(CacheManager::class);
$this->files = m::mock(Filesystem::class);
$this->cacheRepository = m::mock(Repository::class);
$this->command = new ClearCommandTestStub($this->cacheManager, $this->files);
$app = new Application;
$app['path.storage'] = __DIR__;
$this->command->setLaravel($app);
}
protected function tearDown(): void
{
m::close();
}
public function testClearWithNoStoreArgument()
{
$this->files->shouldReceive('exists')->andReturn(true);
$this->files->shouldReceive('files')->andReturn([]);
$this->cacheManager->shouldReceive('store')->once()->with(null)->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('flush')->once();
$this->runCommand($this->command);
}
public function testClearWithStoreArgument()
{
$this->files->shouldReceive('exists')->andReturn(true);
$this->files->shouldReceive('files')->andReturn([]);
$this->cacheManager->shouldReceive('store')->once()->with('foo')->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('flush')->once();
$this->runCommand($this->command, ['store' => 'foo']);
}
public function testClearWithInvalidStoreArgument()
{
$this->expectException(InvalidArgumentException::class);
$this->files->shouldReceive('files')->andReturn([]);
$this->cacheManager->shouldReceive('store')->once()->with('bar')->andThrow(InvalidArgumentException::class);
$this->cacheRepository->shouldReceive('flush')->never();
$this->runCommand($this->command, ['store' => 'bar']);
}
public function testClearWithTagsOption()
{
$this->files->shouldReceive('exists')->andReturn(true);
$this->files->shouldReceive('files')->andReturn([]);
$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();
$this->runCommand($this->command, ['--tags' => 'foo,bar']);
}
public function testClearWithStoreArgumentAndTagsOption()
{
$this->files->shouldReceive('exists')->andReturn(true);
$this->files->shouldReceive('files')->andReturn([]);
$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($this->command, ['store' => 'redis', '--tags' => 'foo']);
}
public function testClearWillClearRealTimeFacades()
{
$this->cacheManager->shouldReceive('store')->once()->with(null)->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('flush')->once();
$this->files->shouldReceive('exists')->andReturn(true);
$this->files->shouldReceive('files')->andReturn(['/facade-XXXX.php']);
$this->files->shouldReceive('delete')->with('/facade-XXXX.php')->once();
$this->runCommand($this->command);
}
public function testClearWillNotClearRealTimeFacadesIfCacheDirectoryDoesntExist()
{
$this->cacheManager->shouldReceive('store')->once()->with(null)->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('flush')->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($this->command);
}
protected function runCommand($command, $input = [])
{
return $command->run(new ArrayInput($input), new NullOutput);
}
}
class ClearCommandTestStub extends ClearCommand
{
public function call($command, array $arguments = [])
{
//
}
}
|