MonthFieldTest.php 2.8 KB

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