ClearCommandTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Illuminate\Tests\Cache;
  3. use Illuminate\Cache\CacheManager;
  4. use Illuminate\Cache\Console\ClearCommand;
  5. use Illuminate\Contracts\Cache\Repository;
  6. use Illuminate\Filesystem\Filesystem;
  7. use Illuminate\Foundation\Application;
  8. use InvalidArgumentException;
  9. use Mockery as m;
  10. use PHPUnit\Framework\TestCase;
  11. use Symfony\Component\Console\Input\ArrayInput;
  12. use Symfony\Component\Console\Output\NullOutput;
  13. class ClearCommandTest extends TestCase
  14. {
  15. /**
  16. * @var \Illuminate\Tests\Cache\ClearCommandTestStub
  17. */
  18. private $command;
  19. /**
  20. * @var \Illuminate\Cache\CacheManager|\Mockery\MockInterface
  21. */
  22. private $cacheManager;
  23. /**
  24. * @var \Illuminate\Filesystem\Filesystem|\Mockery\MockInterface
  25. */
  26. private $files;
  27. /**
  28. * @var \Illuminate\Contracts\Cache\Repository|\Mockery\MockInterface
  29. */
  30. private $cacheRepository;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. protected function setUp(): void
  35. {
  36. parent::setUp();
  37. $this->cacheManager = m::mock(CacheManager::class);
  38. $this->files = m::mock(Filesystem::class);
  39. $this->cacheRepository = m::mock(Repository::class);
  40. $this->command = new ClearCommandTestStub($this->cacheManager, $this->files);
  41. $app = new Application;
  42. $app['path.storage'] = __DIR__;
  43. $this->command->setLaravel($app);
  44. }
  45. protected function tearDown(): void
  46. {
  47. m::close();
  48. }
  49. public function testClearWithNoStoreArgument()
  50. {
  51. $this->files->shouldReceive('exists')->andReturn(true);
  52. $this->files->shouldReceive('files')->andReturn([]);
  53. $this->cacheManager->shouldReceive('store')->once()->with(null)->andReturn($this->cacheRepository);
  54. $this->cacheRepository->shouldReceive('flush')->once();
  55. $this->runCommand($this->command);
  56. }
  57. public function testClearWithStoreArgument()
  58. {
  59. $this->files->shouldReceive('exists')->andReturn(true);
  60. $this->files->shouldReceive('files')->andReturn([]);
  61. $this->cacheManager->shouldReceive('store')->once()->with('foo')->andReturn($this->cacheRepository);
  62. $this->cacheRepository->shouldReceive('flush')->once();
  63. $this->runCommand($this->command, ['store' => 'foo']);
  64. }
  65. public function testClearWithInvalidStoreArgument()
  66. {
  67. $this->expectException(InvalidArgumentException::class);
  68. $this->files->shouldReceive('files')->andReturn([]);
  69. $this->cacheManager->shouldReceive('store')->once()->with('bar')->andThrow(InvalidArgumentException::class);
  70. $this->cacheRepository->shouldReceive('flush')->never();
  71. $this->runCommand($this->command, ['store' => 'bar']);
  72. }
  73. public function testClearWithTagsOption()
  74. {
  75. $this->files->shouldReceive('exists')->andReturn(true);
  76. $this->files->shouldReceive('files')->andReturn([]);
  77. $this->cacheManager->shouldReceive('store')->once()->with(null)->andReturn($this->cacheRepository);
  78. $this->cacheRepository->shouldReceive('tags')->once()->with(['foo', 'bar'])->andReturn($this->cacheRepository);
  79. $this->cacheRepository->shouldReceive('flush')->once();
  80. $this->runCommand($this->command, ['--tags' => 'foo,bar']);
  81. }
  82. public function testClearWithStoreArgumentAndTagsOption()
  83. {
  84. $this->files->shouldReceive('exists')->andReturn(true);
  85. $this->files->shouldReceive('files')->andReturn([]);
  86. $this->cacheManager->shouldReceive('store')->once()->with('redis')->andReturn($this->cacheRepository);
  87. $this->cacheRepository->shouldReceive('tags')->once()->with(['foo'])->andReturn($this->cacheRepository);
  88. $this->cacheRepository->shouldReceive('flush')->once();
  89. $this->runCommand($this->command, ['store' => 'redis', '--tags' => 'foo']);
  90. }
  91. public function testClearWillClearRealTimeFacades()
  92. {
  93. $this->cacheManager->shouldReceive('store')->once()->with(null)->andReturn($this->cacheRepository);
  94. $this->cacheRepository->shouldReceive('flush')->once();
  95. $this->files->shouldReceive('exists')->andReturn(true);
  96. $this->files->shouldReceive('files')->andReturn(['/facade-XXXX.php']);
  97. $this->files->shouldReceive('delete')->with('/facade-XXXX.php')->once();
  98. $this->runCommand($this->command);
  99. }
  100. public function testClearWillNotClearRealTimeFacadesIfCacheDirectoryDoesntExist()
  101. {
  102. $this->cacheManager->shouldReceive('store')->once()->with(null)->andReturn($this->cacheRepository);
  103. $this->cacheRepository->shouldReceive('flush')->once();
  104. // No files should be looped over and nothing should be deleted if the cache directory doesn't exist
  105. $this->files->shouldReceive('exists')->andReturn(false);
  106. $this->files->shouldNotReceive('files');
  107. $this->files->shouldNotReceive('delete');
  108. $this->runCommand($this->command);
  109. }
  110. protected function runCommand($command, $input = [])
  111. {
  112. return $command->run(new ArrayInput($input), new NullOutput);
  113. }
  114. }
  115. class ClearCommandTestStub extends ClearCommand
  116. {
  117. public function call($command, array $arguments = [])
  118. {
  119. return 0;
  120. }
  121. }