1
0

DayOfMonthFieldTest.php 2.1 KB

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