CronExpressionTest.php 25KB

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