ConsoleScheduledEventTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Illuminate\Tests\Console;
  3. use Illuminate\Console\Scheduling\Event;
  4. use Illuminate\Console\Scheduling\EventMutex;
  5. use Illuminate\Foundation\Application;
  6. use Illuminate\Support\Carbon;
  7. use Mockery as m;
  8. use PHPUnit\Framework\TestCase;
  9. class ConsoleScheduledEventTest extends TestCase
  10. {
  11. /**
  12. * The default configuration timezone.
  13. *
  14. * @var string
  15. */
  16. protected $defaultTimezone;
  17. protected function setUp(): void
  18. {
  19. $this->defaultTimezone = date_default_timezone_get();
  20. date_default_timezone_set('UTC');
  21. }
  22. protected function tearDown(): void
  23. {
  24. date_default_timezone_set($this->defaultTimezone);
  25. Carbon::setTestNow(null);
  26. m::close();
  27. }
  28. public function testBasicCronCompilation()
  29. {
  30. $app = m::mock(Application::class.'[isDownForMaintenance,environment]');
  31. $app->shouldReceive('isDownForMaintenance')->andReturn(false);
  32. $app->shouldReceive('environment')->andReturn('production');
  33. $event = new Event(m::mock(EventMutex::class), 'php foo');
  34. $this->assertSame('* * * * *', $event->getExpression());
  35. $this->assertTrue($event->isDue($app));
  36. $this->assertTrue($event->skip(function () {
  37. return true;
  38. })->isDue($app));
  39. $this->assertFalse($event->skip(function () {
  40. return true;
  41. })->filtersPass($app));
  42. $event = new Event(m::mock(EventMutex::class), 'php foo');
  43. $this->assertSame('* * * * *', $event->getExpression());
  44. $this->assertFalse($event->environments('local')->isDue($app));
  45. $event = new Event(m::mock(EventMutex::class), 'php foo');
  46. $this->assertSame('* * * * *', $event->getExpression());
  47. $this->assertFalse($event->when(function () {
  48. return false;
  49. })->filtersPass($app));
  50. $event = new Event(m::mock(EventMutex::class), 'php foo');
  51. $this->assertSame('* * * * *', $event->getExpression());
  52. $this->assertFalse($event->when(false)->filtersPass($app));
  53. // chained rules should be commutative
  54. $eventA = new Event(m::mock(EventMutex::class), 'php foo');
  55. $eventB = new Event(m::mock(EventMutex::class), 'php foo');
  56. $this->assertEquals(
  57. $eventA->daily()->hourly()->getExpression(),
  58. $eventB->hourly()->daily()->getExpression());
  59. $eventA = new Event(m::mock(EventMutex::class), 'php foo');
  60. $eventB = new Event(m::mock(EventMutex::class), 'php foo');
  61. $this->assertEquals(
  62. $eventA->weekdays()->hourly()->getExpression(),
  63. $eventB->hourly()->weekdays()->getExpression());
  64. }
  65. public function testEventIsDueCheck()
  66. {
  67. $app = m::mock(Application::class.'[isDownForMaintenance,environment]');
  68. $app->shouldReceive('isDownForMaintenance')->andReturn(false);
  69. $app->shouldReceive('environment')->andReturn('production');
  70. Carbon::setTestNow(Carbon::create(2015, 1, 1, 0, 0, 0));
  71. $event = new Event(m::mock(EventMutex::class), 'php foo');
  72. $this->assertSame('* * * * 4', $event->thursdays()->getExpression());
  73. $this->assertTrue($event->isDue($app));
  74. $event = new Event(m::mock(EventMutex::class), 'php foo');
  75. $this->assertSame('0 19 * * 3', $event->wednesdays()->at('19:00')->timezone('EST')->getExpression());
  76. $this->assertTrue($event->isDue($app));
  77. }
  78. public function testTimeBetweenChecks()
  79. {
  80. $app = m::mock(Application::class.'[isDownForMaintenance,environment]');
  81. $app->shouldReceive('isDownForMaintenance')->andReturn(false);
  82. $app->shouldReceive('environment')->andReturn('production');
  83. Carbon::setTestNow(Carbon::now()->startOfDay()->addHours(9));
  84. $event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
  85. $this->assertTrue($event->between('8:00', '10:00')->filtersPass($app));
  86. $event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
  87. $this->assertTrue($event->between('9:00', '9:00')->filtersPass($app));
  88. $event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
  89. $this->assertTrue($event->between('23:00', '10:00')->filtersPass($app));
  90. $event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
  91. $this->assertTrue($event->between('8:00', '6:00')->filtersPass($app));
  92. $event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
  93. $this->assertFalse($event->between('10:00', '11:00')->filtersPass($app));
  94. $event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
  95. $this->assertFalse($event->between('10:00', '8:00')->filtersPass($app));
  96. }
  97. public function testTimeUnlessBetweenChecks()
  98. {
  99. $app = m::mock(Application::class.'[isDownForMaintenance,environment]');
  100. $app->shouldReceive('isDownForMaintenance')->andReturn(false);
  101. $app->shouldReceive('environment')->andReturn('production');
  102. Carbon::setTestNow(Carbon::now()->startOfDay()->addHours(9));
  103. $event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
  104. $this->assertFalse($event->unlessBetween('8:00', '10:00')->filtersPass($app));
  105. $event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
  106. $this->assertFalse($event->unlessBetween('9:00', '9:00')->filtersPass($app));
  107. $event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
  108. $this->assertFalse($event->unlessBetween('23:00', '10:00')->filtersPass($app));
  109. $event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
  110. $this->assertFalse($event->unlessBetween('8:00', '6:00')->filtersPass($app));
  111. $event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
  112. $this->assertTrue($event->unlessBetween('10:00', '11:00')->filtersPass($app));
  113. $event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
  114. $this->assertTrue($event->unlessBetween('10:00', '8:00')->filtersPass($app));
  115. }
  116. }