MinutesFieldTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. namespace Cron\Tests;
  4. use Cron\MinutesField;
  5. use DateTime;
  6. use DateTimeImmutable;
  7. use PHPUnit\Framework\TestCase;
  8. /**
  9. * @author Michael Dowling <mtdowling@gmail.com>
  10. */
  11. class MinutesFieldTest extends TestCase
  12. {
  13. /**
  14. * @covers \Cron\MinutesField::validate
  15. */
  16. public function testValidatesField()
  17. {
  18. $f = new MinutesField();
  19. $this->assertTrue($f->validate('1'));
  20. $this->assertTrue($f->validate('*'));
  21. $this->assertFalse($f->validate('*/3,1,1-12'));
  22. $this->assertFalse($f->validate('1/10'));
  23. }
  24. /**
  25. * @covers \Cron\MinutesField::isSatisfiedBy
  26. */
  27. public function testChecksIfSatisfied()
  28. {
  29. $f = new MinutesField();
  30. $this->assertTrue($f->isSatisfiedBy(new DateTime(), '?'));
  31. $this->assertTrue($f->isSatisfiedBy(new DateTimeImmutable(), '?'));
  32. }
  33. /**
  34. * @covers \Cron\MinutesField::increment
  35. */
  36. public function testIncrementsDate()
  37. {
  38. $d = new DateTime('2011-03-15 11:15:00');
  39. $f = new MinutesField();
  40. $f->increment($d);
  41. $this->assertSame('2011-03-15 11:16:00', $d->format('Y-m-d H:i:s'));
  42. $f->increment($d, true);
  43. $this->assertSame('2011-03-15 11:15:00', $d->format('Y-m-d H:i:s'));
  44. }
  45. /**
  46. * @covers \Cron\MinutesField::increment
  47. */
  48. public function testIncrementsDateTimeImmutable()
  49. {
  50. $d = new DateTimeImmutable('2011-03-15 11:15:00');
  51. $f = new MinutesField();
  52. $f->increment($d);
  53. $this->assertSame('2011-03-15 11:16:00', $d->format('Y-m-d H:i:s'));
  54. }
  55. /**
  56. * Various bad syntaxes that are reported to work, but shouldn't.
  57. *
  58. * @author Chris Tankersley
  59. *
  60. * @since 2017-08-18
  61. */
  62. public function testBadSyntaxesShouldNotValidate()
  63. {
  64. $f = new MinutesField();
  65. $this->assertFalse($f->validate('*-1'));
  66. $this->assertFalse($f->validate('1-2-3'));
  67. $this->assertFalse($f->validate('-1'));
  68. }
  69. /**
  70. * Ranges that are invalid should not validate.
  71. * In this case `0/5` would be invalid because `0` is not part of the minute range.
  72. *
  73. * @author Chris Tankersley
  74. * @since 2019-07-29
  75. * @see https://github.com/dragonmantank/cron-expression/issues/18
  76. */
  77. public function testInvalidRangeShouldNotValidate()
  78. {
  79. $f = new MinutesField();
  80. $this->assertFalse($f->validate('0/5'));
  81. }
  82. }