0
0

MonthFieldTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Cron\Tests;
  3. use Cron\MonthField;
  4. use DateTime;
  5. use PHPUnit_Framework_TestCase;
  6. /**
  7. * @author Michael Dowling <mtdowling@gmail.com>
  8. */
  9. class MonthFieldTest extends PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * @covers Cron\MonthField::validate
  13. */
  14. public function testValidatesField()
  15. {
  16. $f = new MonthField();
  17. $this->assertTrue($f->validate('12'));
  18. $this->assertTrue($f->validate('*'));
  19. $this->assertTrue($f->validate('*/10,2,1-12'));
  20. $this->assertFalse($f->validate('1.fix-regexp'));
  21. }
  22. /**
  23. * @covers Cron\MonthField::increment
  24. */
  25. public function testIncrementsDate()
  26. {
  27. $d = new DateTime('2011-03-15 11:15:00');
  28. $f = new MonthField();
  29. $f->increment($d);
  30. $this->assertEquals('2011-04-01 00:00:00', $d->format('Y-m-d H:i:s'));
  31. $d = new DateTime('2011-03-15 11:15:00');
  32. $f->increment($d, true);
  33. $this->assertEquals('2011-02-28 23:59:00', $d->format('Y-m-d H:i:s'));
  34. }
  35. /**
  36. * @covers Cron\MonthField::increment
  37. */
  38. public function testIncrementsDateWithThirtyMinuteTimezone()
  39. {
  40. $tz = date_default_timezone_get();
  41. date_default_timezone_set('America/St_Johns');
  42. $d = new DateTime('2011-03-31 11:59:59');
  43. $f = new MonthField();
  44. $f->increment($d);
  45. $this->assertEquals('2011-04-01 00:00:00', $d->format('Y-m-d H:i:s'));
  46. $d = new DateTime('2011-03-15 11:15:00');
  47. $f->increment($d, true);
  48. $this->assertEquals('2011-02-28 23:59:00', $d->format('Y-m-d H:i:s'));
  49. date_default_timezone_set($tz);
  50. }
  51. /**
  52. * @covers Cron\MonthField::increment
  53. */
  54. public function testIncrementsYearAsNeeded()
  55. {
  56. $f = new MonthField();
  57. $d = new DateTime('2011-12-15 00:00:00');
  58. $f->increment($d);
  59. $this->assertEquals('2012-01-01 00:00:00', $d->format('Y-m-d H:i:s'));
  60. }
  61. /**
  62. * @covers Cron\MonthField::increment
  63. */
  64. public function testDecrementsYearAsNeeded()
  65. {
  66. $f = new MonthField();
  67. $d = new DateTime('2011-01-15 00:00:00');
  68. $f->increment($d, true);
  69. $this->assertEquals('2010-12-31 23:59:00', $d->format('Y-m-d H:i:s'));
  70. }
  71. }