FrequencyTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace Illuminate\Tests\Console\Scheduling;
  3. use Illuminate\Console\Scheduling\Event;
  4. use Illuminate\Console\Scheduling\EventMutex;
  5. use Illuminate\Support\Carbon;
  6. use Mockery as m;
  7. use PHPUnit\Framework\TestCase;
  8. class FrequencyTest extends TestCase
  9. {
  10. /*
  11. * @var \Illuminate\Console\Scheduling\Event
  12. */
  13. protected $event;
  14. protected function setUp(): void
  15. {
  16. Carbon::setTestNow();
  17. $this->event = new Event(
  18. m::mock(EventMutex::class),
  19. 'php foo'
  20. );
  21. }
  22. public function testEveryMinute()
  23. {
  24. $this->assertSame('* * * * *', $this->event->getExpression());
  25. $this->assertSame('* * * * *', $this->event->everyMinute()->getExpression());
  26. }
  27. public function testEveryXMinutes()
  28. {
  29. $this->assertSame('*/2 * * * *', $this->event->everyTwoMinutes()->getExpression());
  30. $this->assertSame('*/3 * * * *', $this->event->everyThreeMinutes()->getExpression());
  31. $this->assertSame('*/4 * * * *', $this->event->everyFourMinutes()->getExpression());
  32. $this->assertSame('*/5 * * * *', $this->event->everyFiveMinutes()->getExpression());
  33. $this->assertSame('*/10 * * * *', $this->event->everyTenMinutes()->getExpression());
  34. $this->assertSame('*/15 * * * *', $this->event->everyFifteenMinutes()->getExpression());
  35. $this->assertSame('0,30 * * * *', $this->event->everyThirtyMinutes()->getExpression());
  36. }
  37. public function testDaily()
  38. {
  39. $this->assertSame('0 0 * * *', $this->event->daily()->getExpression());
  40. }
  41. public function testDailyAt()
  42. {
  43. $this->assertSame('8 13 * * *', $this->event->dailyAt('13:08')->getExpression());
  44. }
  45. public function testTwiceDaily()
  46. {
  47. $this->assertSame('0 3,15 * * *', $this->event->twiceDaily(3, 15)->getExpression());
  48. }
  49. public function testTwiceDailyAt()
  50. {
  51. $this->assertSame('5 3,15 * * *', $this->event->twiceDailyAt(3, 15, 5)->getExpression());
  52. }
  53. public function testWeekly()
  54. {
  55. $this->assertSame('0 0 * * 0', $this->event->weekly()->getExpression());
  56. }
  57. public function testWeeklyOn()
  58. {
  59. $this->assertSame('0 8 * * 1', $this->event->weeklyOn(1, '8:00')->getExpression());
  60. }
  61. public function testOverrideWithHourly()
  62. {
  63. $this->assertSame('0 * * * *', $this->event->everyFiveMinutes()->hourly()->getExpression());
  64. $this->assertSame('37 * * * *', $this->event->hourlyAt(37)->getExpression());
  65. $this->assertSame('15,30,45 * * * *', $this->event->hourlyAt([15, 30, 45])->getExpression());
  66. }
  67. public function testHourly()
  68. {
  69. $this->assertSame('0 */2 * * *', $this->event->everyTwoHours()->getExpression());
  70. $this->assertSame('0 */3 * * *', $this->event->everyThreeHours()->getExpression());
  71. $this->assertSame('0 */4 * * *', $this->event->everyFourHours()->getExpression());
  72. $this->assertSame('0 */6 * * *', $this->event->everySixHours()->getExpression());
  73. }
  74. public function testMonthly()
  75. {
  76. $this->assertSame('0 0 1 * *', $this->event->monthly()->getExpression());
  77. }
  78. public function testMonthlyOn()
  79. {
  80. $this->assertSame('0 15 4 * *', $this->event->monthlyOn(4, '15:00')->getExpression());
  81. }
  82. public function testLastDayOfMonth()
  83. {
  84. Carbon::setTestNow('2020-10-10 10:10:10');
  85. $this->assertSame('0 0 31 * *', $this->event->lastDayOfMonth()->getExpression());
  86. }
  87. public function testTwiceMonthly()
  88. {
  89. $this->assertSame('0 0 1,16 * *', $this->event->twiceMonthly(1, 16)->getExpression());
  90. }
  91. public function testTwiceMonthlyAtTime()
  92. {
  93. $this->assertSame('30 1 1,16 * *', $this->event->twiceMonthly(1, 16, '1:30')->getExpression());
  94. }
  95. public function testMonthlyOnWithMinutes()
  96. {
  97. $this->assertSame('15 15 4 * *', $this->event->monthlyOn(4, '15:15')->getExpression());
  98. }
  99. public function testWeekdaysDaily()
  100. {
  101. $this->assertSame('0 0 * * 1-5', $this->event->weekdays()->daily()->getExpression());
  102. }
  103. public function testWeekdaysHourly()
  104. {
  105. $this->assertSame('0 * * * 1-5', $this->event->weekdays()->hourly()->getExpression());
  106. }
  107. public function testWeekdays()
  108. {
  109. $this->assertSame('* * * * 1-5', $this->event->weekdays()->getExpression());
  110. }
  111. public function testWeekends()
  112. {
  113. $this->assertSame('* * * * 6,0', $this->event->weekends()->getExpression());
  114. }
  115. public function testSundays()
  116. {
  117. $this->assertSame('* * * * 0', $this->event->sundays()->getExpression());
  118. }
  119. public function testMondays()
  120. {
  121. $this->assertSame('* * * * 1', $this->event->mondays()->getExpression());
  122. }
  123. public function testTuesdays()
  124. {
  125. $this->assertSame('* * * * 2', $this->event->tuesdays()->getExpression());
  126. }
  127. public function testWednesdays()
  128. {
  129. $this->assertSame('* * * * 3', $this->event->wednesdays()->getExpression());
  130. }
  131. public function testThursdays()
  132. {
  133. $this->assertSame('* * * * 4', $this->event->thursdays()->getExpression());
  134. }
  135. public function testFridays()
  136. {
  137. $this->assertSame('* * * * 5', $this->event->fridays()->getExpression());
  138. }
  139. public function testSaturdays()
  140. {
  141. $this->assertSame('* * * * 6', $this->event->saturdays()->getExpression());
  142. }
  143. public function testQuarterly()
  144. {
  145. $this->assertSame('0 0 1 1-12/3 *', $this->event->quarterly()->getExpression());
  146. }
  147. public function testYearly()
  148. {
  149. $this->assertSame('0 0 1 1 *', $this->event->yearly()->getExpression());
  150. }
  151. public function testYearlyOn()
  152. {
  153. $this->assertSame('8 15 5 4 *', $this->event->yearlyOn(4, 5, '15:08')->getExpression());
  154. }
  155. public function testYearlyOnMondaysOnly()
  156. {
  157. $this->assertSame('1 9 * 7 1', $this->event->mondays()->yearlyOn(7, '*', '09:01')->getExpression());
  158. }
  159. public function testYearlyOnTuesdaysAndDayOfMonth20()
  160. {
  161. $this->assertSame('1 9 20 7 2', $this->event->tuesdays()->yearlyOn(7, 20, '09:01')->getExpression());
  162. }
  163. public function testFrequencyMacro()
  164. {
  165. Event::macro('everyXMinutes', function ($x) {
  166. return $this->spliceIntoPosition(1, "*/{$x}");
  167. });
  168. $this->assertSame('*/6 * * * *', $this->event->everyXMinutes(6)->getExpression());
  169. }
  170. }