CacheEventMutexTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Illuminate\Tests\Console\Scheduling;
  3. use Illuminate\Console\Scheduling\CacheEventMutex;
  4. use Illuminate\Console\Scheduling\Event;
  5. use Illuminate\Contracts\Cache\Factory;
  6. use Illuminate\Contracts\Cache\Repository;
  7. use Mockery as m;
  8. use PHPUnit\Framework\TestCase;
  9. class CacheEventMutexTest extends TestCase
  10. {
  11. /**
  12. * @var \Illuminate\Console\Scheduling\CacheEventMutex
  13. */
  14. protected $cacheMutex;
  15. /**
  16. * @var \Illuminate\Console\Scheduling\Event
  17. */
  18. protected $event;
  19. /**
  20. * @var \Illuminate\Contracts\Cache\Factory
  21. */
  22. protected $cacheFactory;
  23. /**
  24. * @var \Illuminate\Contracts\Cache\Repository
  25. */
  26. protected $cacheRepository;
  27. protected function setUp(): void
  28. {
  29. parent::setUp();
  30. $this->cacheFactory = m::mock(Factory::class);
  31. $this->cacheRepository = m::mock(Repository::class);
  32. $this->cacheFactory->shouldReceive('store')->andReturn($this->cacheRepository);
  33. $this->cacheMutex = new CacheEventMutex($this->cacheFactory);
  34. $this->event = new Event($this->cacheMutex, 'command');
  35. }
  36. public function testPreventOverlap()
  37. {
  38. $this->cacheRepository->shouldReceive('add')->once();
  39. $this->cacheMutex->create($this->event);
  40. }
  41. public function testCustomConnection()
  42. {
  43. $this->cacheFactory->shouldReceive('store')->with('test')->andReturn($this->cacheRepository);
  44. $this->cacheRepository->shouldReceive('add')->once();
  45. $this->cacheMutex->useStore('test');
  46. $this->cacheMutex->create($this->event);
  47. }
  48. public function testPreventOverlapFails()
  49. {
  50. $this->cacheRepository->shouldReceive('add')->once()->andReturn(false);
  51. $this->assertFalse($this->cacheMutex->create($this->event));
  52. }
  53. public function testOverlapsForNonRunningTask()
  54. {
  55. $this->cacheRepository->shouldReceive('has')->once()->andReturn(false);
  56. $this->assertFalse($this->cacheMutex->exists($this->event));
  57. }
  58. public function testOverlapsForRunningTask()
  59. {
  60. $this->cacheRepository->shouldReceive('has')->once()->andReturn(true);
  61. $this->assertTrue($this->cacheMutex->exists($this->event));
  62. }
  63. public function testResetOverlap()
  64. {
  65. $this->cacheRepository->shouldReceive('forget')->once();
  66. $this->cacheMutex->forget($this->event);
  67. }
  68. }