CronExpressionTest.php 26 KB

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