Geen omschrijving

CronExpressionTest.php 17KB

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