DayOfMonthFieldTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Cron\Tests;
  3. use Cron\DayOfMonthField;
  4. use DateTime;
  5. use DateTimeImmutable;
  6. use PHPUnit\Framework\TestCase;
  7. /**
  8. * @author Michael Dowling <mtdowling@gmail.com>
  9. */
  10. class DayOfMonthFieldTest extends TestCase
  11. {
  12. /**
  13. * @covers \Cron\DayOfMonthField::validate
  14. */
  15. public function testValidatesField()
  16. {
  17. $f = new DayOfMonthField();
  18. $this->assertTrue($f->validate('1'));
  19. $this->assertTrue($f->validate('*'));
  20. $this->assertTrue($f->validate('L'));
  21. $this->assertTrue($f->validate('5W'));
  22. $this->assertTrue($f->validate('01'));
  23. $this->assertFalse($f->validate('5W,L'));
  24. $this->assertFalse($f->validate('1.'));
  25. }
  26. /**
  27. * @covers \Cron\DayOfMonthField::isSatisfiedBy
  28. */
  29. public function testChecksIfSatisfied()
  30. {
  31. $f = new DayOfMonthField();
  32. $this->assertTrue($f->isSatisfiedBy(new DateTime(), '?'));
  33. $this->assertTrue($f->isSatisfiedBy(new DateTimeImmutable(), '?'));
  34. }
  35. /**
  36. * @covers \Cron\DayOfMonthField::increment
  37. */
  38. public function testIncrementsDate()
  39. {
  40. $d = new DateTime('2011-03-15 11:15:00');
  41. $f = new DayOfMonthField();
  42. $f->increment($d);
  43. $this->assertSame('2011-03-16 00:00:00', $d->format('Y-m-d H:i:s'));
  44. $d = new DateTime('2011-03-15 11:15:00');
  45. $f->increment($d, true);
  46. $this->assertSame('2011-03-14 23:59:00', $d->format('Y-m-d H:i:s'));
  47. }
  48. /**
  49. * @covers \Cron\DayOfMonthField::increment
  50. */
  51. public function testIncrementsDateTimeImmutable()
  52. {
  53. $d = new DateTimeImmutable('2011-03-15 11:15:00');
  54. $f = new DayOfMonthField();
  55. $f->increment($d);
  56. $this->assertSame('2011-03-16 00:00:00', $d->format('Y-m-d H:i:s'));
  57. }
  58. /**
  59. * Day of the month cannot accept a 0 value, it must be between 1 and 31
  60. * See Github issue #120
  61. *
  62. * @since 2017-01-22
  63. */
  64. public function testDoesNotAccept0Date()
  65. {
  66. $f = new DayOfMonthField();
  67. $this->assertFalse($f->validate(0));
  68. }
  69. }