123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?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 \Illuminate\Tests\Cache\ClearCommandTestStub
- */
- private $command;
- /**
- * @var \Illuminate\Cache\CacheManager|\Mockery\MockInterface
- */
- private $cacheManager;
- /**
- * @var \Illuminate\Filesystem\Filesystem|\Mockery\MockInterface
- */
- private $files;
- /**
- * @var \Illuminate\Contracts\Cache\Repository|\Mockery\MockInterface
- */
- 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 = [])
- {
- return 0;
- }
- }
|