1
0

MonthFieldTest.php 3.3 KB

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