CronExpressionTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. namespace Cron\Tests;
  3. use Cron\CronExpression;
  4. use DateTime;
  5. use DateTimeZone;
  6. use InvalidArgumentException;
  7. use PHPUnit_Framework_TestCase;
  8. /**
  9. * @author Michael Dowling <mtdowling@gmail.com>
  10. */
  11. class CronExpressionTest extends PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @covers Cron\CronExpression::factory
  15. */
  16. public function testFactoryRecognizesTemplates()
  17. {
  18. $this->assertEquals('0 0 1 1 *', CronExpression::factory('@annually')->getExpression());
  19. $this->assertEquals('0 0 1 1 *', CronExpression::factory('@yearly')->getExpression());
  20. $this->assertEquals('0 0 * * 0', CronExpression::factory('@weekly')->getExpression());
  21. }
  22. /**
  23. * @covers Cron\CronExpression::__construct
  24. * @covers Cron\CronExpression::getExpression
  25. * @covers Cron\CronExpression::__toString
  26. */
  27. public function testParsesCronSchedule()
  28. {
  29. // '2010-09-10 12:00:00'
  30. $cron = CronExpression::factory('1 2-4 * 4,5,6 */3');
  31. $this->assertEquals('1', $cron->getExpression(CronExpression::MINUTE));
  32. $this->assertEquals('2-4', $cron->getExpression(CronExpression::HOUR));
  33. $this->assertEquals('*', $cron->getExpression(CronExpression::DAY));
  34. $this->assertEquals('4,5,6', $cron->getExpression(CronExpression::MONTH));
  35. $this->assertEquals('*/3', $cron->getExpression(CronExpression::WEEKDAY));
  36. $this->assertEquals('1 2-4 * 4,5,6 */3', $cron->getExpression());
  37. $this->assertEquals('1 2-4 * 4,5,6 */3', (string) $cron);
  38. $this->assertNull($cron->getExpression('foo'));
  39. try {
  40. $cron = CronExpression::factory('A 1 2 3 4');
  41. $this->fail('Validation exception not thrown');
  42. } catch (InvalidArgumentException $e) {
  43. }
  44. }
  45. /**
  46. * @covers Cron\CronExpression::__construct
  47. * @covers Cron\CronExpression::getExpression
  48. * @dataProvider scheduleWithDifferentSeparatorsProvider
  49. */
  50. public function testParsesCronScheduleWithAnySpaceCharsAsSeparators($schedule, array $expected)
  51. {
  52. $cron = CronExpression::factory($schedule);
  53. $this->assertEquals($expected[0], $cron->getExpression(CronExpression::MINUTE));
  54. $this->assertEquals($expected[1], $cron->getExpression(CronExpression::HOUR));
  55. $this->assertEquals($expected[2], $cron->getExpression(CronExpression::DAY));
  56. $this->assertEquals($expected[3], $cron->getExpression(CronExpression::MONTH));
  57. $this->assertEquals($expected[4], $cron->getExpression(CronExpression::WEEKDAY));
  58. $this->assertEquals($expected[5], $cron->getExpression(CronExpression::YEAR));
  59. }
  60. /**
  61. * Data provider for testParsesCronScheduleWithAnySpaceCharsAsSeparators
  62. *
  63. * @return array
  64. */
  65. public static function scheduleWithDifferentSeparatorsProvider()
  66. {
  67. return array(
  68. array("*\t*\t*\t*\t*\t*", array('*', '*', '*', '*', '*', '*')),
  69. array("* * * * * *", array('*', '*', '*', '*', '*', '*')),
  70. array("* \t * \t * \t * \t * \t *", array('*', '*', '*', '*', '*', '*')),
  71. array("*\t \t*\t \t*\t \t*\t \t*\t \t*", array('*', '*', '*', '*', '*', '*')),
  72. );
  73. }
  74. /**
  75. * @covers Cron\CronExpression::__construct
  76. * @covers Cron\CronExpression::setExpression
  77. * @covers Cron\CronExpression::setPart
  78. * @expectedException InvalidArgumentException
  79. */
  80. public function testInvalidCronsWillFail()
  81. {
  82. // Only four values
  83. $cron = CronExpression::factory('* * * 1');
  84. }
  85. /**
  86. * @covers Cron\CronExpression::setPart
  87. * @expectedException InvalidArgumentException
  88. */
  89. public function testInvalidPartsWillFail()
  90. {
  91. // Only four values
  92. $cron = CronExpression::factory('* * * * *');
  93. $cron->setPart(1, 'abc');
  94. }
  95. /**
  96. * Data provider for cron schedule
  97. *
  98. * @return array
  99. */
  100. public function scheduleProvider()
  101. {
  102. return array(
  103. array('*/2 */2 * * *', '2015-08-10 21:47:27', '2015-08-10 22:00:00', false),
  104. array('* * * * *', '2015-08-10 21:50:37', '2015-08-10 21:50:00', true),
  105. array('* 20,21,22 * * *', '2015-08-10 21:50:00', '2015-08-10 21:50:00', true),
  106. // Handles CSV values
  107. array('* 20,22 * * *', '2015-08-10 21:50:00', '2015-08-10 22:00:00', false),
  108. // CSV values can be complex
  109. array('* 5,21-22 * * *', '2015-08-10 21:50:00', '2015-08-10 21:50:00', true),
  110. array('7-9 * */9 * *', '2015-08-10 22:02:33', '2015-08-18 00:07:00', false),
  111. // 15th minute, of the second hour, every 15 days, in January, every Friday
  112. array('1 * * * 7', '2015-08-10 21:47:27', '2015-08-16 00:01:00', false),
  113. // Test with exact times
  114. array('47 21 * * *', strtotime('2015-08-10 21:47:30'), '2015-08-10 21:47:00', true),
  115. // Test Day of the week (issue #1)
  116. // According cron implementation, 0|7 = sunday, 1 => monday, etc
  117. array('* * * * 0', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false),
  118. array('* * * * 7', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false),
  119. array('* * * * 1', strtotime('2011-06-15 23:09:00'), '2011-06-20 00:00:00', false),
  120. // Should return the sunday date as 7 equals 0
  121. array('0 0 * * MON,SUN', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false),
  122. array('0 0 * * 1,7', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false),
  123. array('0 0 * * 0-4', strtotime('2011-06-15 23:09:00'), '2011-06-16 00:00:00', false),
  124. array('0 0 * * 7-4', strtotime('2011-06-15 23:09:00'), '2011-06-16 00:00:00', false),
  125. array('0 0 * * 4-7', strtotime('2011-06-15 23:09:00'), '2011-06-16 00:00:00', false),
  126. array('0 0 * * 7-3', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false),
  127. array('0 0 * * 3-7', strtotime('2011-06-15 23:09:00'), '2011-06-16 00:00:00', false),
  128. array('0 0 * * 3-7', strtotime('2011-06-18 23:09:00'), '2011-06-19 00:00:00', false),
  129. // Test lists of values and ranges (Abhoryo)
  130. array('0 0 * * 2-7', strtotime('2011-06-20 23:09:00'), '2011-06-21 00:00:00', false),
  131. array('0 0 * * 0,2-6', strtotime('2011-06-20 23:09:00'), '2011-06-21 00:00:00', false),
  132. array('0 0 * * 2-7', strtotime('2011-06-18 23:09:00'), '2011-06-19 00:00:00', false),
  133. array('0 0 * * 4-7', strtotime('2011-07-19 00:00:00'), '2011-07-21 00:00:00', false),
  134. // Test increments of ranges
  135. array('0-12/4 * * * *', strtotime('2011-06-20 12:04:00'), '2011-06-20 12:04:00', true),
  136. array('4-59/2 * * * *', strtotime('2011-06-20 12:04:00'), '2011-06-20 12:04:00', true),
  137. array('4-59/2 * * * *', strtotime('2011-06-20 12:06:00'), '2011-06-20 12:06:00', true),
  138. array('4-59/3 * * * *', strtotime('2011-06-20 12:06:00'), '2011-06-20 12:07:00', false),
  139. //array('0 0 * * 0,2-6', strtotime('2011-06-20 23:09:00'), '2011-06-21 00:00:00', false),
  140. // Test Day of the Week and the Day of the Month (issue #1)
  141. array('0 0 1 1 0', strtotime('2011-06-15 23:09:00'), '2012-01-01 00:00:00', false),
  142. array('0 0 1 JAN 0', strtotime('2011-06-15 23:09:00'), '2012-01-01 00:00:00', false),
  143. array('0 0 1 * 0', strtotime('2011-06-15 23:09:00'), '2012-01-01 00:00:00', false),
  144. array('0 0 L * *', strtotime('2011-07-15 00:00:00'), '2011-07-31 00:00:00', false),
  145. // Test the W day of the week modifier for day of the month field
  146. array('0 0 2W * *', strtotime('2011-07-01 00:00:00'), '2011-07-01 00:00:00', true),
  147. array('0 0 1W * *', strtotime('2011-05-01 00:00:00'), '2011-05-02 00:00:00', false),
  148. array('0 0 1W * *', strtotime('2011-07-01 00:00:00'), '2011-07-01 00:00:00', true),
  149. array('0 0 3W * *', strtotime('2011-07-01 00:00:00'), '2011-07-04 00:00:00', false),
  150. array('0 0 16W * *', strtotime('2011-07-01 00:00:00'), '2011-07-15 00:00:00', false),
  151. array('0 0 28W * *', strtotime('2011-07-01 00:00:00'), '2011-07-28 00:00:00', false),
  152. array('0 0 30W * *', strtotime('2011-07-01 00:00:00'), '2011-07-29 00:00:00', false),
  153. array('0 0 31W * *', strtotime('2011-07-01 00:00:00'), '2011-07-29 00:00:00', false),
  154. // Test the year field
  155. array('* * * * * 2012', strtotime('2011-05-01 00:00:00'), '2012-01-01 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('* * * * TUEL', strtotime('2011-07-24 00:00:00'), '2011-07-26 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. );
  168. }
  169. /**
  170. * @covers Cron\CronExpression::isDue
  171. * @covers Cron\CronExpression::getNextRunDate
  172. * @covers Cron\DayOfMonthField
  173. * @covers Cron\DayOfWeekField
  174. * @covers Cron\MinutesField
  175. * @covers Cron\HoursField
  176. * @covers Cron\MonthField
  177. * @covers Cron\YearField
  178. * @covers Cron\CronExpression::getRunDate
  179. * @dataProvider scheduleProvider
  180. */
  181. public function testDeterminesIfCronIsDue($schedule, $relativeTime, $nextRun, $isDue)
  182. {
  183. $relativeTimeString = is_int($relativeTime) ? date('Y-m-d H:i:s', $relativeTime) : $relativeTime;
  184. // Test next run date
  185. $cron = CronExpression::factory($schedule);
  186. if (is_string($relativeTime)) {
  187. $relativeTime = new DateTime($relativeTime);
  188. } elseif (is_int($relativeTime)) {
  189. $relativeTime = date('Y-m-d H:i:s', $relativeTime);
  190. }
  191. $this->assertEquals($isDue, $cron->isDue($relativeTime));
  192. $next = $cron->getNextRunDate($relativeTime, 0, true);
  193. $this->assertEquals(new DateTime($nextRun), $next);
  194. }
  195. /**
  196. * @covers Cron\CronExpression::isDue
  197. */
  198. public function testIsDueHandlesDifferentDates()
  199. {
  200. $cron = CronExpression::factory('* * * * *');
  201. $this->assertTrue($cron->isDue());
  202. $this->assertTrue($cron->isDue('now'));
  203. $this->assertTrue($cron->isDue(new DateTime('now')));
  204. $this->assertTrue($cron->isDue(date('Y-m-d H:i')));
  205. }
  206. /**
  207. * @covers Cron\CronExpression::isDue
  208. */
  209. public function testIsDueHandlesDifferentTimezones()
  210. {
  211. $cron = CronExpression::factory('0 15 * * 3'); //Wednesday at 15:00
  212. $date = '2014-01-01 15:00'; //Wednesday
  213. $utc = new DateTimeZone('UTC');
  214. $amsterdam = new DateTimeZone('Europe/Amsterdam');
  215. $tokyo = new DateTimeZone('Asia/Tokyo');
  216. date_default_timezone_set('UTC');
  217. $this->assertTrue($cron->isDue(new DateTime($date, $utc)));
  218. $this->assertFalse($cron->isDue(new DateTime($date, $amsterdam)));
  219. $this->assertFalse($cron->isDue(new DateTime($date, $tokyo)));
  220. date_default_timezone_set('Europe/Amsterdam');
  221. $this->assertFalse($cron->isDue(new DateTime($date, $utc)));
  222. $this->assertTrue($cron->isDue(new DateTime($date, $amsterdam)));
  223. $this->assertFalse($cron->isDue(new DateTime($date, $tokyo)));
  224. date_default_timezone_set('Asia/Tokyo');
  225. $this->assertFalse($cron->isDue(new DateTime($date, $utc)));
  226. $this->assertFalse($cron->isDue(new DateTime($date, $amsterdam)));
  227. $this->assertTrue($cron->isDue(new DateTime($date, $tokyo)));
  228. }
  229. /**
  230. * @covers Cron\CronExpression::getPreviousRunDate
  231. */
  232. public function testCanGetPreviousRunDates()
  233. {
  234. $cron = CronExpression::factory('* * * * *');
  235. $next = $cron->getNextRunDate('now');
  236. $two = $cron->getNextRunDate('now', 1);
  237. $this->assertEquals($next, $cron->getPreviousRunDate($two));
  238. $cron = CronExpression::factory('* */2 * * *');
  239. $next = $cron->getNextRunDate('now');
  240. $two = $cron->getNextRunDate('now', 1);
  241. $this->assertEquals($next, $cron->getPreviousRunDate($two));
  242. $cron = CronExpression::factory('* * * */2 *');
  243. $next = $cron->getNextRunDate('now');
  244. $two = $cron->getNextRunDate('now', 1);
  245. $this->assertEquals($next, $cron->getPreviousRunDate($two));
  246. }
  247. /**
  248. * @covers Cron\CronExpression::getMultipleRunDates
  249. */
  250. public function testProvidesMultipleRunDates()
  251. {
  252. $cron = CronExpression::factory('*/2 * * * *');
  253. $this->assertEquals(array(
  254. new DateTime('2008-11-09 00:00:00'),
  255. new DateTime('2008-11-09 00:02:00'),
  256. new DateTime('2008-11-09 00:04:00'),
  257. new DateTime('2008-11-09 00:06:00')
  258. ), $cron->getMultipleRunDates(4, '2008-11-09 00:00:00', false, true));
  259. }
  260. /**
  261. * @covers Cron\CronExpression::getMultipleRunDates
  262. * @covers Cron\CronExpression::setMaxIterationCount
  263. */
  264. public function testProvidesMultipleRunDatesForTheFarFuture() {
  265. // Fails with the default 1000 iteration limit
  266. $cron = CronExpression::factory('0 0 12 1 * */2');
  267. $cron->setMaxIterationCount(2000);
  268. $this->assertEquals(array(
  269. new DateTime('2016-01-12 00:00:00'),
  270. new DateTime('2018-01-12 00:00:00'),
  271. new DateTime('2020-01-12 00:00:00'),
  272. new DateTime('2022-01-12 00:00:00'),
  273. new DateTime('2024-01-12 00:00:00'),
  274. new DateTime('2026-01-12 00:00:00'),
  275. new DateTime('2028-01-12 00:00:00'),
  276. new DateTime('2030-01-12 00:00:00'),
  277. new DateTime('2032-01-12 00:00:00'),
  278. ), $cron->getMultipleRunDates(9, '2015-04-28 00:00:00', false, true));
  279. }
  280. /**
  281. * @covers Cron\CronExpression
  282. */
  283. public function testCanIterateOverNextRuns()
  284. {
  285. $cron = CronExpression::factory('@weekly');
  286. $nextRun = $cron->getNextRunDate("2008-11-09 08:00:00");
  287. $this->assertEquals($nextRun, new DateTime("2008-11-16 00:00:00"));
  288. // true is cast to 1
  289. $nextRun = $cron->getNextRunDate("2008-11-09 00:00:00", true, true);
  290. $this->assertEquals($nextRun, new DateTime("2008-11-16 00:00:00"));
  291. // You can iterate over them
  292. $nextRun = $cron->getNextRunDate($cron->getNextRunDate("2008-11-09 00:00:00", 1, true), 1, true);
  293. $this->assertEquals($nextRun, new DateTime("2008-11-23 00:00:00"));
  294. // You can skip more than one
  295. $nextRun = $cron->getNextRunDate("2008-11-09 00:00:00", 2, true);
  296. $this->assertEquals($nextRun, new DateTime("2008-11-23 00:00:00"));
  297. $nextRun = $cron->getNextRunDate("2008-11-09 00:00:00", 3, true);
  298. $this->assertEquals($nextRun, new DateTime("2008-11-30 00:00:00"));
  299. }
  300. /**
  301. * @covers Cron\CronExpression::getRunDate
  302. */
  303. public function testSkipsCurrentDateByDefault()
  304. {
  305. $cron = CronExpression::factory('* * * * *');
  306. $current = new DateTime('now');
  307. $next = $cron->getNextRunDate($current);
  308. $nextPrev = $cron->getPreviousRunDate($next);
  309. $this->assertEquals($current->format('Y-m-d H:i:00'), $nextPrev->format('Y-m-d H:i:s'));
  310. }
  311. /**
  312. * @covers Cron\CronExpression::getRunDate
  313. * @ticket 7
  314. */
  315. public function testStripsForSeconds()
  316. {
  317. $cron = CronExpression::factory('* * * * *');
  318. $current = new DateTime('2011-09-27 10:10:54');
  319. $this->assertEquals('2011-09-27 10:11:00', $cron->getNextRunDate($current)->format('Y-m-d H:i:s'));
  320. }
  321. /**
  322. * @covers Cron\CronExpression::getRunDate
  323. */
  324. public function testFixesPhpBugInDateIntervalMonth()
  325. {
  326. $cron = CronExpression::factory('0 0 27 JAN *');
  327. $this->assertEquals('2011-01-27 00:00:00', $cron->getPreviousRunDate('2011-08-22 00:00:00')->format('Y-m-d H:i:s'));
  328. }
  329. public function testIssue29()
  330. {
  331. $cron = CronExpression::factory('@weekly');
  332. $this->assertEquals(
  333. '2013-03-10 00:00:00',
  334. $cron->getPreviousRunDate('2013-03-17 00:00:00')->format('Y-m-d H:i:s')
  335. );
  336. }
  337. /**
  338. * @see https://github.com/mtdowling/cron-expression/issues/20
  339. */
  340. public function testIssue20() {
  341. $e = CronExpression::factory('* * * * MON#1');
  342. $this->assertTrue($e->isDue(new DateTime('2014-04-07 00:00:00')));
  343. $this->assertFalse($e->isDue(new DateTime('2014-04-14 00:00:00')));
  344. $this->assertFalse($e->isDue(new DateTime('2014-04-21 00:00:00')));
  345. $e = CronExpression::factory('* * * * SAT#2');
  346. $this->assertFalse($e->isDue(new DateTime('2014-04-05 00:00:00')));
  347. $this->assertTrue($e->isDue(new DateTime('2014-04-12 00:00:00')));
  348. $this->assertFalse($e->isDue(new DateTime('2014-04-19 00:00:00')));
  349. $e = CronExpression::factory('* * * * SUN#3');
  350. $this->assertFalse($e->isDue(new DateTime('2014-04-13 00:00:00')));
  351. $this->assertTrue($e->isDue(new DateTime('2014-04-20 00:00:00')));
  352. $this->assertFalse($e->isDue(new DateTime('2014-04-27 00:00:00')));
  353. }
  354. /**
  355. * @covers Cron\CronExpression::getRunDate
  356. */
  357. public function testKeepOriginalTime()
  358. {
  359. $now = new \DateTime;
  360. $strNow = $now->format(DateTime::ISO8601);
  361. $cron = CronExpression::factory('0 0 * * *');
  362. $cron->getPreviousRunDate($now);
  363. $this->assertEquals($strNow, $now->format(DateTime::ISO8601));
  364. }
  365. /**
  366. * @covers Cron\CronExpression::__construct
  367. * @covers Cron\CronExpression::factory
  368. * @covers Cron\CronExpression::isValidExpression
  369. * @covers Cron\CronExpression::setExpression
  370. * @covers Cron\CronExpression::setPart
  371. */
  372. public function testValidationWorks()
  373. {
  374. // Invalid. Only four values
  375. $this->assertFalse(CronExpression::isValidExpression('* * * 1'));
  376. // Valid
  377. $this->assertTrue(CronExpression::isValidExpression('* * * * 1'));
  378. }
  379. }