MinutesFieldTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Cron\Tests;
  3. use Cron\MinutesField;
  4. use DateTime;
  5. use DateTimeImmutable;
  6. use PHPUnit\Framework\TestCase;
  7. /**
  8. * @author Michael Dowling <mtdowling@gmail.com>
  9. */
  10. class MinutesFieldTest extends TestCase
  11. {
  12. /**
  13. * @covers \Cron\MinutesField::validate
  14. */
  15. public function testValidatesField()
  16. {
  17. $f = new MinutesField();
  18. $this->assertTrue($f->validate('1'));
  19. $this->assertTrue($f->validate('*'));
  20. $this->assertFalse($f->validate('*/3,1,1-12'));
  21. }
  22. /**
  23. * @covers \Cron\MinutesField::isSatisfiedBy
  24. */
  25. public function testChecksIfSatisfied()
  26. {
  27. $f = new MinutesField();
  28. $this->assertTrue($f->isSatisfiedBy(new DateTime(), '?'));
  29. $this->assertTrue($f->isSatisfiedBy(new DateTimeImmutable(), '?'));
  30. }
  31. /**
  32. * @covers \Cron\MinutesField::increment
  33. */
  34. public function testIncrementsDate()
  35. {
  36. $d = new DateTime('2011-03-15 11:15:00');
  37. $f = new MinutesField();
  38. $f->increment($d);
  39. $this->assertSame('2011-03-15 11:16:00', $d->format('Y-m-d H:i:s'));
  40. $f->increment($d, true);
  41. $this->assertSame('2011-03-15 11:15:00', $d->format('Y-m-d H:i:s'));
  42. }
  43. /**
  44. * @covers \Cron\MinutesField::increment
  45. */
  46. public function testIncrementsDateTimeImmutable()
  47. {
  48. $d = new DateTimeImmutable('2011-03-15 11:15:00');
  49. $f = new MinutesField();
  50. $f->increment($d);
  51. $this->assertSame('2011-03-15 11:16:00', $d->format('Y-m-d H:i:s'));
  52. }
  53. /**
  54. * Various bad syntaxes that are reported to work, but shouldn't.
  55. *
  56. * @author Chris Tankersley
  57. * @since 2017-08-18
  58. */
  59. public function testBadSyntaxesShouldNotValidate()
  60. {
  61. $f = new MinutesField();
  62. $this->assertFalse($f->validate('*-1'));
  63. $this->assertFalse($f->validate('1-2-3'));
  64. $this->assertFalse($f->validate('-1'));
  65. }
  66. }