CacheSchedulingMutexTest.php 2.9 KB

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