CronExpressionTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. <?php
  2. declare(strict_types=1);
  3. namespace Cron\Tests;
  4. use Cron\CronExpression;
  5. use Cron\MonthField;
  6. use DateTime;
  7. use DateTimeImmutable;
  8. use DateTimeZone;
  9. use InvalidArgumentException;
  10. use PHPUnit\Framework\TestCase;
  11. /**
  12. * @author Michael Dowling <mtdowling@gmail.com>
  13. */
  14. class CronExpressionTest extends TestCase
  15. {
  16. /**
  17. * @covers \Cron\CronExpression::factory
  18. */
  19. public function testFactoryRecognizesTemplates(): void
  20. {
  21. $this->assertSame('0 0 1 1 *', CronExpression::factory('@annually')->getExpression());
  22. $this->assertSame('0 0 1 1 *', CronExpression::factory('@yearly')->getExpression());
  23. $this->assertSame('0 0 * * 0', CronExpression::factory('@weekly')->getExpression());
  24. }
  25. /**
  26. * @covers \Cron\CronExpression::__construct
  27. * @covers \Cron\CronExpression::getExpression
  28. * @covers \Cron\CronExpression::__toString
  29. */
  30. public function testParsesCronSchedule(): void
  31. {
  32. // '2010-09-10 12:00:00'
  33. $cron = CronExpression::factory('1 2-4 * 4,5,6 */3');
  34. $this->assertSame('1', $cron->getExpression(CronExpression::MINUTE));
  35. $this->assertSame('2-4', $cron->getExpression(CronExpression::HOUR));
  36. $this->assertSame('*', $cron->getExpression(CronExpression::DAY));
  37. $this->assertSame('4,5,6', $cron->getExpression(CronExpression::MONTH));
  38. $this->assertSame('*/3', $cron->getExpression(CronExpression::WEEKDAY));
  39. $this->assertSame('1 2-4 * 4,5,6 */3', $cron->getExpression());
  40. $this->assertSame('1 2-4 * 4,5,6 */3', (string) $cron);
  41. $this->assertNull($cron->getExpression('foo'));
  42. }
  43. /**
  44. * @covers \Cron\CronExpression::__construct
  45. * @covers \Cron\CronExpression::getExpression
  46. * @covers \Cron\CronExpression::__toString
  47. */
  48. public function testParsesCronScheduleThrowsAnException(): void
  49. {
  50. $this->expectException(InvalidArgumentException::class);
  51. $this->expectExceptionMessage('Invalid CRON field value A at position 0');
  52. CronExpression::factory('A 1 2 3 4');
  53. }
  54. /**
  55. * @covers \Cron\CronExpression::__construct
  56. * @covers \Cron\CronExpression::getExpression
  57. * @dataProvider scheduleWithDifferentSeparatorsProvider
  58. *
  59. * @param mixed $schedule
  60. */
  61. public function testParsesCronScheduleWithAnySpaceCharsAsSeparators($schedule, array $expected): void
  62. {
  63. $cron = CronExpression::factory($schedule);
  64. $this->assertSame($expected[0], $cron->getExpression(CronExpression::MINUTE));
  65. $this->assertSame($expected[1], $cron->getExpression(CronExpression::HOUR));
  66. $this->assertSame($expected[2], $cron->getExpression(CronExpression::DAY));
  67. $this->assertSame($expected[3], $cron->getExpression(CronExpression::MONTH));
  68. $this->assertSame($expected[4], $cron->getExpression(CronExpression::WEEKDAY));
  69. }
  70. /**
  71. * Data provider for testParsesCronScheduleWithAnySpaceCharsAsSeparators.
  72. *
  73. * @return array
  74. */
  75. public static function scheduleWithDifferentSeparatorsProvider(): array
  76. {
  77. return [
  78. ["*\t*\t*\t*\t*\t", ['*', '*', '*', '*', '*', '*']],
  79. ['* * * * * ', ['*', '*', '*', '*', '*', '*']],
  80. ["* \t * \t * \t * \t * \t", ['*', '*', '*', '*', '*', '*']],
  81. ["*\t \t*\t \t*\t \t*\t \t*\t \t", ['*', '*', '*', '*', '*', '*']],
  82. ];
  83. }
  84. /**
  85. * @covers \Cron\CronExpression::__construct
  86. * @covers \Cron\CronExpression::setExpression
  87. * @covers \Cron\CronExpression::setPart
  88. */
  89. public function testInvalidCronsWillFail(): void
  90. {
  91. $this->expectException(InvalidArgumentException::class);
  92. // Only four values
  93. $cron = CronExpression::factory('* * * 1');
  94. }
  95. /**
  96. * @covers \Cron\CronExpression::setPart
  97. */
  98. public function testInvalidPartsWillFail(): void
  99. {
  100. $this->expectException(InvalidArgumentException::class);
  101. // Only four values
  102. $cron = CronExpression::factory('* * * * *');
  103. $cron->setPart(1, 'abc');
  104. }
  105. /**
  106. * Data provider for cron schedule.
  107. *
  108. * @return array
  109. */
  110. public function scheduleProvider(): array
  111. {
  112. return [
  113. ['*/2 */2 * * *', '2015-08-10 21:47:27', '2015-08-10 22:00:00', false],
  114. ['* * * * *', '2015-08-10 21:50:37', '2015-08-10 21:50:00', true],
  115. ['* 20,21,22 * * *', '2015-08-10 21:50:00', '2015-08-10 21:50:00', true],
  116. // Handles CSV values
  117. ['* 20,22 * * *', '2015-08-10 21:50:00', '2015-08-10 22:00:00', false],
  118. // CSV values can be complex
  119. ['7-9 * */9 * *', '2015-08-10 22:02:33', '2015-08-10 22:07:00', false],
  120. // 15th minute, of the second hour, every 15 days, in January, every Friday
  121. ['1 * * * 7', '2015-08-10 21:47:27', '2015-08-16 00:01:00', false],
  122. // Test with exact times
  123. ['47 21 * * *', strtotime('2015-08-10 21:47:30'), '2015-08-10 21:47:00', true],
  124. // Test Day of the week (issue #1)
  125. // According cron implementation, 0|7 = sunday, 1 => monday, etc
  126. ['* * * * 0', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false],
  127. ['* * * * 7', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false],
  128. ['* * * * 1', strtotime('2011-06-15 23:09:00'), '2011-06-20 00:00:00', false],
  129. // Should return the sunday date as 7 equals 0
  130. ['0 0 * * MON,SUN', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false],
  131. ['0 0 * * 1,7', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false],
  132. ['0 0 * * 0-4', strtotime('2011-06-15 23:09:00'), '2011-06-16 00:00:00', false],
  133. ['0 0 * * 7-4', strtotime('2011-06-15 23:09:00'), '2011-06-16 00:00:00', false],
  134. ['0 0 * * 4-7', strtotime('2011-06-15 23:09:00'), '2011-06-16 00:00:00', false],
  135. ['0 0 * * 7-3', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false],
  136. ['0 0 * * 3-7', strtotime('2011-06-15 23:09:00'), '2011-06-16 00:00:00', false],
  137. ['0 0 * * 3-7', strtotime('2011-06-18 23:09:00'), '2011-06-19 00:00:00', false],
  138. // Test lists of values and ranges (Abhoryo)
  139. ['0 0 * * 2-7', strtotime('2011-06-20 23:09:00'), '2011-06-21 00:00:00', false],
  140. ['0 0 * * 2-7', strtotime('2011-06-18 23:09:00'), '2011-06-19 00:00:00', false],
  141. ['0 0 * * 4-7', strtotime('2011-07-19 00:00:00'), '2011-07-21 00:00:00', false],
  142. // Test increments of ranges
  143. ['0-12/4 * * * *', strtotime('2011-06-20 12:04:00'), '2011-06-20 12:04:00', true],
  144. ['4-59/2 * * * *', strtotime('2011-06-20 12:04:00'), '2011-06-20 12:04:00', true],
  145. ['4-59/2 * * * *', strtotime('2011-06-20 12:06:00'), '2011-06-20 12:06:00', true],
  146. ['4-59/3 * * * *', strtotime('2011-06-20 12:06:00'), '2011-06-20 12:07:00', false],
  147. // Test Day of the Week and the Day of the Month (issue #1)
  148. ['0 0 1 1 0', strtotime('2011-06-15 23:09:00'), '2012-01-01 00:00:00', false],
  149. ['0 0 1 JAN 0', strtotime('2011-06-15 23:09:00'), '2012-01-01 00:00:00', false],
  150. ['0 0 1 * 0', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false],
  151. // Test the W day of the week modifier for day of the month field
  152. ['0 0 2W * *', strtotime('2011-07-01 00:00:00'), '2011-07-01 00:00:00', true],
  153. ['0 0 1W * *', strtotime('2011-05-01 00:00:00'), '2011-05-02 00:00:00', false],
  154. ['0 0 1W * *', strtotime('2011-07-01 00:00:00'), '2011-07-01 00:00:00', true],
  155. ['0 0 3W * *', strtotime('2011-07-01 00:00:00'), '2011-07-04 00:00:00', false],
  156. ['0 0 16W * *', strtotime('2011-07-01 00:00:00'), '2011-07-15 00:00:00', false],
  157. ['0 0 28W * *', strtotime('2011-07-01 00:00:00'), '2011-07-28 00:00:00', false],
  158. ['0 0 30W * *', strtotime('2011-07-01 00:00:00'), '2011-07-29 00:00:00', false],
  159. ['0 0 31W * *', strtotime('2011-07-01 00:00:00'), '2011-07-29 00:00:00', false],
  160. // Test the last weekday of a month
  161. ['* * * * 5L', strtotime('2011-07-01 00:00:00'), '2011-07-29 00:00:00', false],
  162. ['* * * * 6L', strtotime('2011-07-01 00:00:00'), '2011-07-30 00:00:00', false],
  163. ['* * * * 7L', strtotime('2011-07-01 00:00:00'), '2011-07-31 00:00:00', false],
  164. ['* * * * 1L', strtotime('2011-07-24 00:00:00'), '2011-07-25 00:00:00', false],
  165. ['* * * 1 5L', strtotime('2011-12-25 00:00:00'), '2012-01-27 00:00:00', false],
  166. // Test the hash symbol for the nth weekday of a given month
  167. ['* * * * 5#2', strtotime('2011-07-01 00:00:00'), '2011-07-08 00:00:00', false],
  168. ['* * * * 5#1', strtotime('2011-07-01 00:00:00'), '2011-07-01 00:00:00', true],
  169. ['* * * * 3#4', strtotime('2011-07-01 00:00:00'), '2011-07-27 00:00:00', false],
  170. // Issue #7, documented example failed
  171. ['3-59/15 6-12 */15 1 2-5', strtotime('2017-01-08 00:00:00'), '2017-01-10 06:03:00', false],
  172. // https://github.com/laravel/framework/commit/07d160ac3cc9764d5b429734ffce4fa311385403
  173. ['* * * * MON-FRI', strtotime('2017-01-08 00:00:00'), strtotime('2017-01-09 00:00:00'), false],
  174. ['* * * * TUE', strtotime('2017-01-08 00:00:00'), strtotime('2017-01-10 00:00:00'), false],
  175. // Issue #60, make sure that casing is less relevant for shortcuts, months, and days
  176. ['0 1 15 JUL mon,Wed,FRi', strtotime('2019-11-14 00:00:00'), strtotime('2020-07-01 01:00:00'), false],
  177. ['0 1 15 jul mon,Wed,FRi', strtotime('2019-11-14 00:00:00'), strtotime('2020-07-01 01:00:00'), false],
  178. ['@Weekly', strtotime('2019-11-14 00:00:00'), strtotime('2019-11-17 00:00:00'), false],
  179. ['@WEEKLY', strtotime('2019-11-14 00:00:00'), strtotime('2019-11-17 00:00:00'), false],
  180. ['@WeeklY', strtotime('2019-11-14 00:00:00'), strtotime('2019-11-17 00:00:00'), false],
  181. // Issue #76, DOW and DOM do not support ?
  182. ['0 12 * * ?', strtotime('2020-08-20 00:00:00'), strtotime('2020-08-20 12:00:00'), false],
  183. ['0 12 ? * *', strtotime('2020-08-20 00:00:00'), strtotime('2020-08-20 12:00:00'), false],
  184. ];
  185. }
  186. /**
  187. * @covers \Cron\CronExpression::isDue
  188. * @covers \Cron\CronExpression::getNextRunDate
  189. * @covers \Cron\DayOfMonthField
  190. * @covers \Cron\DayOfWeekField
  191. * @covers \Cron\MinutesField
  192. * @covers \Cron\HoursField
  193. * @covers \Cron\MonthField
  194. * @covers \Cron\CronExpression::getRunDate
  195. * @dataProvider scheduleProvider
  196. *
  197. * @param mixed $schedule
  198. * @param mixed $relativeTime
  199. * @param mixed $nextRun
  200. * @param mixed $isDue
  201. */
  202. public function testDeterminesIfCronIsDue($schedule, $relativeTime, $nextRun, $isDue): void
  203. {
  204. // Test next run date
  205. $cron = CronExpression::factory($schedule);
  206. if (\is_string($relativeTime)) {
  207. $relativeTime = new DateTime($relativeTime);
  208. } elseif (\is_int($relativeTime)) {
  209. $relativeTime = date('Y-m-d H:i:s', $relativeTime);
  210. }
  211. if (\is_string($nextRun)) {
  212. $nextRunDate = new DateTime($nextRun);
  213. } elseif (\is_int($nextRun)) {
  214. $nextRunDate = new DateTime();
  215. $nextRunDate->setTimestamp($nextRun);
  216. }
  217. $this->assertSame($isDue, $cron->isDue($relativeTime));
  218. $next = $cron->getNextRunDate($relativeTime, 0, true);
  219. $this->assertEquals($nextRunDate, $next);
  220. }
  221. /**
  222. * @covers \Cron\CronExpression::isDue
  223. */
  224. public function testIsDueHandlesDifferentDates(): void
  225. {
  226. $cron = CronExpression::factory('* * * * *');
  227. $this->assertTrue($cron->isDue());
  228. $this->assertTrue($cron->isDue('now'));
  229. $this->assertTrue($cron->isDue(new DateTime('now')));
  230. $this->assertTrue($cron->isDue(date('Y-m-d H:i')));
  231. $this->assertTrue($cron->isDue(new DateTimeImmutable('now')));
  232. }
  233. /**
  234. * @covers \Cron\CronExpression::isDue
  235. */
  236. public function testIsDueHandlesDifferentDefaultTimezones(): void
  237. {
  238. $originalTimezone = date_default_timezone_get();
  239. $cron = CronExpression::factory('0 15 * * 3'); //Wednesday at 15:00
  240. $date = '2014-01-01 15:00'; //Wednesday
  241. date_default_timezone_set('UTC');
  242. $this->assertTrue($cron->isDue(new DateTime($date), 'UTC'));
  243. $this->assertFalse($cron->isDue(new DateTime($date), 'Europe/Amsterdam'));
  244. $this->assertFalse($cron->isDue(new DateTime($date), 'Asia/Tokyo'));
  245. date_default_timezone_set('Europe/Amsterdam');
  246. $this->assertFalse($cron->isDue(new DateTime($date), 'UTC'));
  247. $this->assertTrue($cron->isDue(new DateTime($date), 'Europe/Amsterdam'));
  248. $this->assertFalse($cron->isDue(new DateTime($date), 'Asia/Tokyo'));
  249. date_default_timezone_set('Asia/Tokyo');
  250. $this->assertFalse($cron->isDue(new DateTime($date), 'UTC'));
  251. $this->assertFalse($cron->isDue(new DateTime($date), 'Europe/Amsterdam'));
  252. $this->assertTrue($cron->isDue(new DateTime($date), 'Asia/Tokyo'));
  253. date_default_timezone_set($originalTimezone);
  254. }
  255. /**
  256. * @covers \Cron\CronExpression::isDue
  257. */
  258. public function testIsDueHandlesDifferentSuppliedTimezones(): void
  259. {
  260. $cron = CronExpression::factory('0 15 * * 3'); //Wednesday at 15:00
  261. $date = '2014-01-01 15:00'; //Wednesday
  262. $this->assertTrue($cron->isDue(new DateTime($date, new DateTimeZone('UTC')), 'UTC'));
  263. $this->assertFalse($cron->isDue(new DateTime($date, new DateTimeZone('UTC')), 'Europe/Amsterdam'));
  264. $this->assertFalse($cron->isDue(new DateTime($date, new DateTimeZone('UTC')), 'Asia/Tokyo'));
  265. $this->assertFalse($cron->isDue(new DateTime($date, new DateTimeZone('Europe/Amsterdam')), 'UTC'));
  266. $this->assertTrue($cron->isDue(new DateTime($date, new DateTimeZone('Europe/Amsterdam')), 'Europe/Amsterdam'));
  267. $this->assertFalse($cron->isDue(new DateTime($date, new DateTimeZone('Europe/Amsterdam')), 'Asia/Tokyo'));
  268. $this->assertFalse($cron->isDue(new DateTime($date, new DateTimeZone('Asia/Tokyo')), 'UTC'));
  269. $this->assertFalse($cron->isDue(new DateTime($date, new DateTimeZone('Asia/Tokyo')), 'Europe/Amsterdam'));
  270. $this->assertTrue($cron->isDue(new DateTime($date, new DateTimeZone('Asia/Tokyo')), 'Asia/Tokyo'));
  271. }
  272. /**
  273. * @covers \Cron\CronExpression::isDue
  274. */
  275. public function testIsDueHandlesDifferentTimezonesAsArgument(): void
  276. {
  277. $cron = CronExpression::factory('0 15 * * 3'); //Wednesday at 15:00
  278. $date = '2014-01-01 15:00'; //Wednesday
  279. $utc = new \DateTimeZone('UTC');
  280. $amsterdam = new \DateTimeZone('Europe/Amsterdam');
  281. $tokyo = new \DateTimeZone('Asia/Tokyo');
  282. $this->assertTrue($cron->isDue(new DateTime($date, $utc), 'UTC'));
  283. $this->assertFalse($cron->isDue(new DateTime($date, $amsterdam), 'UTC'));
  284. $this->assertFalse($cron->isDue(new DateTime($date, $tokyo), 'UTC'));
  285. $this->assertFalse($cron->isDue(new DateTime($date, $utc), 'Europe/Amsterdam'));
  286. $this->assertTrue($cron->isDue(new DateTime($date, $amsterdam), 'Europe/Amsterdam'));
  287. $this->assertFalse($cron->isDue(new DateTime($date, $tokyo), 'Europe/Amsterdam'));
  288. $this->assertFalse($cron->isDue(new DateTime($date, $utc), 'Asia/Tokyo'));
  289. $this->assertFalse($cron->isDue(new DateTime($date, $amsterdam), 'Asia/Tokyo'));
  290. $this->assertTrue($cron->isDue(new DateTime($date, $tokyo), 'Asia/Tokyo'));
  291. }
  292. /**
  293. * @covers \Cron\CronExpression::isDue
  294. */
  295. public function testRecognisesTimezonesAsPartOfDateTime(): void
  296. {
  297. $cron = CronExpression::factory('0 7 * * *');
  298. $tzCron = 'America/New_York';
  299. $tzServer = new \DateTimeZone('Europe/London');
  300. $dtCurrent = \DateTime::createFromFormat('!Y-m-d H:i:s', '2017-10-17 10:00:00', $tzServer);
  301. $dtPrev = $cron->getPreviousRunDate($dtCurrent, 0, true, $tzCron);
  302. $this->assertEquals('1508151600 : 2017-10-16T07:00:00-04:00 : America/New_York', $dtPrev->format('U \\: c \\: e'));
  303. $dtCurrent = \DateTimeImmutable::createFromFormat('!Y-m-d H:i:s', '2017-10-17 10:00:00', $tzServer);
  304. $dtPrev = $cron->getPreviousRunDate($dtCurrent, 0, true, $tzCron);
  305. $this->assertEquals('1508151600 : 2017-10-16T07:00:00-04:00 : America/New_York', $dtPrev->format('U \\: c \\: e'));
  306. $dtCurrent = \DateTimeImmutable::createFromFormat('!Y-m-d H:i:s', '2017-10-17 10:00:00', $tzServer);
  307. $dtPrev = $cron->getPreviousRunDate($dtCurrent->format('c'), 0, true, $tzCron);
  308. $this->assertEquals('1508151600 : 2017-10-16T07:00:00-04:00 : America/New_York', $dtPrev->format('U \\: c \\: e'));
  309. $dtCurrent = \DateTimeImmutable::createFromFormat('!Y-m-d H:i:s', '2017-10-17 10:00:00', $tzServer);
  310. $dtPrev = $cron->getPreviousRunDate($dtCurrent->format('\\@U'), 0, true, $tzCron);
  311. $this->assertEquals('1508151600 : 2017-10-16T07:00:00-04:00 : America/New_York', $dtPrev->format('U \\: c \\: e'));
  312. }
  313. /**
  314. * @covers \Cron\CronExpression::getPreviousRunDate
  315. */
  316. public function testCanGetPreviousRunDates(): void
  317. {
  318. $cron = CronExpression::factory('* * * * *');
  319. $next = $cron->getNextRunDate('now');
  320. $two = $cron->getNextRunDate('now', 1);
  321. $this->assertEquals($next, $cron->getPreviousRunDate($two));
  322. $cron = CronExpression::factory('* */2 * * *');
  323. $next = $cron->getNextRunDate('now');
  324. $two = $cron->getNextRunDate('now', 1);
  325. $this->assertEquals($next, $cron->getPreviousRunDate($two));
  326. $cron = CronExpression::factory('* * * */2 *');
  327. $next = $cron->getNextRunDate('now');
  328. $two = $cron->getNextRunDate('now', 1);
  329. $this->assertEquals($next, $cron->getPreviousRunDate($two));
  330. }
  331. /**
  332. * @covers \Cron\CronExpression::getMultipleRunDates
  333. */
  334. public function testProvidesMultipleRunDates(): void
  335. {
  336. $cron = CronExpression::factory('*/2 * * * *');
  337. $this->assertEquals([
  338. new DateTime('2008-11-09 00:00:00'),
  339. new DateTime('2008-11-09 00:02:00'),
  340. new DateTime('2008-11-09 00:04:00'),
  341. new DateTime('2008-11-09 00:06:00'),
  342. ], $cron->getMultipleRunDates(4, '2008-11-09 00:00:00', false, true));
  343. }
  344. /**
  345. * @covers \Cron\CronExpression::getMultipleRunDates
  346. * @covers \Cron\CronExpression::setMaxIterationCount
  347. */
  348. public function testProvidesMultipleRunDatesForTheFarFuture(): void
  349. {
  350. // Fails with the default 1000 iteration limit
  351. $cron = CronExpression::factory('0 0 12 1 *');
  352. $cron->setMaxIterationCount(2000);
  353. $this->assertEquals([
  354. new DateTime('2016-01-12 00:00:00'),
  355. new DateTime('2017-01-12 00:00:00'),
  356. new DateTime('2018-01-12 00:00:00'),
  357. new DateTime('2019-01-12 00:00:00'),
  358. new DateTime('2020-01-12 00:00:00'),
  359. new DateTime('2021-01-12 00:00:00'),
  360. new DateTime('2022-01-12 00:00:00'),
  361. new DateTime('2023-01-12 00:00:00'),
  362. new DateTime('2024-01-12 00:00:00'),
  363. ], $cron->getMultipleRunDates(9, '2015-04-28 00:00:00', false, true));
  364. }
  365. /**
  366. * @covers \Cron\CronExpression
  367. */
  368. public function testCanIterateOverNextRuns(): void
  369. {
  370. $cron = CronExpression::factory('@weekly');
  371. $nextRun = $cron->getNextRunDate('2008-11-09 08:00:00');
  372. $this->assertEquals($nextRun, new DateTime('2008-11-16 00:00:00'));
  373. // You can iterate over them
  374. $nextRun = $cron->getNextRunDate($cron->getNextRunDate('2008-11-09 00:00:00', 1, true), 1, true);
  375. $this->assertEquals($nextRun, new DateTime('2008-11-23 00:00:00'));
  376. // You can skip more than one
  377. $nextRun = $cron->getNextRunDate('2008-11-09 00:00:00', 2, true);
  378. $this->assertEquals($nextRun, new DateTime('2008-11-23 00:00:00'));
  379. $nextRun = $cron->getNextRunDate('2008-11-09 00:00:00', 3, true);
  380. $this->assertEquals($nextRun, new DateTime('2008-11-30 00:00:00'));
  381. }
  382. /**
  383. * @covers \Cron\CronExpression::getRunDate
  384. */
  385. public function testGetRunDateHandlesDifferentDates()
  386. {
  387. $cron = CronExpression::factory('@weekly');
  388. $date = new DateTime("2019-03-10 00:00:00");
  389. $this->assertEquals($date, $cron->getNextRunDate("2019-03-03 08:00:00"));
  390. $this->assertEquals($date, $cron->getNextRunDate(new DateTime("2019-03-03 08:00:00")));
  391. $this->assertEquals($date, $cron->getNextRunDate(new DateTimeImmutable("2019-03-03 08:00:00")));
  392. }
  393. /**
  394. * @covers \Cron\CronExpression::getRunDate
  395. */
  396. public function testSkipsCurrentDateByDefault()
  397. {
  398. $cron = CronExpression::factory('* * * * *');
  399. $current = new DateTime('now');
  400. $next = $cron->getNextRunDate($current);
  401. $nextPrev = $cron->getPreviousRunDate($next);
  402. $this->assertSame($current->format('Y-m-d H:i:00'), $nextPrev->format('Y-m-d H:i:s'));
  403. }
  404. /**
  405. * @covers \Cron\CronExpression::getRunDate
  406. * @ticket 7
  407. */
  408. public function testStripsForSeconds(): void
  409. {
  410. $cron = CronExpression::factory('* * * * *');
  411. $current = new DateTime('2011-09-27 10:10:54');
  412. $this->assertSame('2011-09-27 10:11:00', $cron->getNextRunDate($current)->format('Y-m-d H:i:s'));
  413. }
  414. /**
  415. * @covers \Cron\CronExpression::getRunDate
  416. */
  417. public function testFixesPhpBugInDateIntervalMonth(): void
  418. {
  419. $cron = CronExpression::factory('0 0 27 JAN *');
  420. $this->assertSame('2011-01-27 00:00:00', $cron->getPreviousRunDate('2011-08-22 00:00:00')->format('Y-m-d H:i:s'));
  421. }
  422. public function testIssue29(): void
  423. {
  424. $cron = CronExpression::factory('@weekly');
  425. $this->assertSame(
  426. '2013-03-10 00:00:00',
  427. $cron->getPreviousRunDate('2013-03-17 00:00:00')->format('Y-m-d H:i:s')
  428. );
  429. }
  430. /**
  431. * @see https://github.com/mtdowling/cron-expression/issues/20
  432. */
  433. public function testIssue20(): void
  434. {
  435. $e = CronExpression::factory('* * * * MON#1');
  436. $this->assertTrue($e->isDue(new DateTime('2014-04-07 00:00:00')));
  437. $this->assertFalse($e->isDue(new DateTime('2014-04-14 00:00:00')));
  438. $this->assertFalse($e->isDue(new DateTime('2014-04-21 00:00:00')));
  439. $e = CronExpression::factory('* * * * SAT#2');
  440. $this->assertFalse($e->isDue(new DateTime('2014-04-05 00:00:00')));
  441. $this->assertTrue($e->isDue(new DateTime('2014-04-12 00:00:00')));
  442. $this->assertFalse($e->isDue(new DateTime('2014-04-19 00:00:00')));
  443. $e = CronExpression::factory('* * * * SUN#3');
  444. $this->assertFalse($e->isDue(new DateTime('2014-04-13 00:00:00')));
  445. $this->assertTrue($e->isDue(new DateTime('2014-04-20 00:00:00')));
  446. $this->assertFalse($e->isDue(new DateTime('2014-04-27 00:00:00')));
  447. }
  448. /**
  449. * @covers \Cron\CronExpression::getRunDate
  450. */
  451. public function testKeepOriginalTime(): void
  452. {
  453. $now = new \DateTime();
  454. $strNow = $now->format(DateTime::ISO8601);
  455. $cron = CronExpression::factory('0 0 * * *');
  456. $cron->getPreviousRunDate($now);
  457. $this->assertSame($strNow, $now->format(DateTime::ISO8601));
  458. }
  459. /**
  460. * @covers \Cron\CronExpression::__construct
  461. * @covers \Cron\CronExpression::factory
  462. * @covers \Cron\CronExpression::isValidExpression
  463. * @covers \Cron\CronExpression::setExpression
  464. * @covers \Cron\CronExpression::setPart
  465. */
  466. public function testValidationWorks(): void
  467. {
  468. // Invalid. Only four values
  469. $this->assertFalse(CronExpression::isValidExpression('* * * 1'));
  470. // Valid
  471. $this->assertTrue(CronExpression::isValidExpression('* * * * 1'));
  472. // Issue #156, 13 is an invalid month
  473. $this->assertFalse(CronExpression::isValidExpression('* * * 13 * '));
  474. // Issue #155, 90 is an invalid second
  475. $this->assertFalse(CronExpression::isValidExpression('90 * * * *'));
  476. // Issue #154, 24 is an invalid hour
  477. $this->assertFalse(CronExpression::isValidExpression('0 24 1 12 0'));
  478. // Issue #125, this is just all sorts of wrong
  479. $this->assertFalse(CronExpression::isValidExpression('990 14 * * mon-fri0345345'));
  480. // see https://github.com/dragonmantank/cron-expression/issues/5
  481. $this->assertTrue(CronExpression::isValidExpression('2,17,35,47 5-7,11-13 * * *'));
  482. }
  483. /**
  484. * Makes sure that 00 is considered a valid value for 0-based fields
  485. * cronie allows numbers with a leading 0, so adding support for this as well.
  486. *
  487. * @see https://github.com/dragonmantank/cron-expression/issues/12
  488. */
  489. public function testDoubleZeroIsValid(): void
  490. {
  491. $this->assertTrue(CronExpression::isValidExpression('00 * * * *'));
  492. $this->assertTrue(CronExpression::isValidExpression('01 * * * *'));
  493. $this->assertTrue(CronExpression::isValidExpression('* 00 * * *'));
  494. $this->assertTrue(CronExpression::isValidExpression('* 01 * * *'));
  495. $e = CronExpression::factory('00 * * * *');
  496. $this->assertTrue($e->isDue(new DateTime('2014-04-07 00:00:00')));
  497. $e = CronExpression::factory('01 * * * *');
  498. $this->assertTrue($e->isDue(new DateTime('2014-04-07 00:01:00')));
  499. $e = CronExpression::factory('* 00 * * *');
  500. $this->assertTrue($e->isDue(new DateTime('2014-04-07 00:00:00')));
  501. $e = CronExpression::factory('* 01 * * *');
  502. $this->assertTrue($e->isDue(new DateTime('2014-04-07 01:00:00')));
  503. }
  504. /**
  505. * Ranges with large steps should "wrap around" to the appropriate value
  506. * cronie allows for steps that are larger than the range of a field, with it wrapping around like a ring buffer. We
  507. * should do the same.
  508. *
  509. * @see https://github.com/dragonmantank/cron-expression/issues/6
  510. */
  511. public function testRangesWrapAroundWithLargeSteps(): void
  512. {
  513. $f = new MonthField();
  514. $this->assertTrue($f->validate('*/123'));
  515. $this->assertSame([4], $f->getRangeForExpression('*/123', 12));
  516. $e = CronExpression::factory('* * * */123 *');
  517. $this->assertTrue($e->isDue(new DateTime('2014-04-07 00:00:00')));
  518. $nextRunDate = $e->getNextRunDate(new DateTime('2014-04-07 00:00:00'));
  519. $this->assertSame('2014-04-07 00:01:00', $nextRunDate->format('Y-m-d H:i:s'));
  520. $nextRunDate = $e->getNextRunDate(new DateTime('2014-05-07 00:00:00'));
  521. $this->assertSame('2015-04-01 00:00:00', $nextRunDate->format('Y-m-d H:i:s'));
  522. }
  523. /**
  524. * When there is an issue with a field, we should report the human readable position.
  525. *
  526. * @see https://github.com/dragonmantank/cron-expression/issues/29
  527. */
  528. public function testFieldPositionIsHumanAdjusted(): void
  529. {
  530. $this->expectException(InvalidArgumentException::class);
  531. $this->expectExceptionMessage('6 is not a valid position');
  532. $e = CronExpression::factory('0 * * * * ? *');
  533. }
  534. /**
  535. * @see https://github.com/dragonmantank/cron-expression/issues/35
  536. */
  537. public function testMakeDayOfWeekAnOrSometimes()
  538. {
  539. $cron = CronExpression::factory('30 0 1 * 1');
  540. $runs = $cron->getMultipleRunDates(5, date("2019-10-10 23:20:00"), false, true);
  541. $this->assertSame("2019-10-14 00:30:00", $runs[0]->format('Y-m-d H:i:s'));
  542. $this->assertSame("2019-10-21 00:30:00", $runs[1]->format('Y-m-d H:i:s'));
  543. $this->assertSame("2019-10-28 00:30:00", $runs[2]->format('Y-m-d H:i:s'));
  544. $this->assertSame("2019-11-01 00:30:00", $runs[3]->format('Y-m-d H:i:s'));
  545. $this->assertSame("2019-11-04 00:30:00", $runs[4]->format('Y-m-d H:i:s'));
  546. }
  547. /**
  548. * Make sure that getNextRunDate() does not add arbitrary minutes
  549. *
  550. * @see https://github.com/mtdowling/cron-expression/issues/152
  551. */
  552. public function testNextRunDateShouldNotAddMinutes()
  553. {
  554. $e = CronExpression::factory('* 19 * * *');
  555. $nextRunDate = $e->getNextRunDate();
  556. $this->assertSame("00", $nextRunDate->format("i"));
  557. }
  558. }