AbstractFieldTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Cron\Tests;
  3. use Cron\DayOfWeekField;
  4. use Cron\HoursField;
  5. use Cron\MinutesField;
  6. use Cron\MonthField;
  7. use PHPUnit\Framework\TestCase;
  8. /**
  9. * @author Michael Dowling <mtdowling@gmail.com>
  10. */
  11. class AbstractFieldTest extends TestCase
  12. {
  13. /**
  14. * @covers \Cron\AbstractField::isRange
  15. */
  16. public function testTestsIfRange()
  17. {
  18. $f = new DayOfWeekField();
  19. $this->assertTrue($f->isRange('1-2'));
  20. $this->assertFalse($f->isRange('2'));
  21. }
  22. /**
  23. * @covers \Cron\AbstractField::isIncrementsOfRanges
  24. */
  25. public function testTestsIfIncrementsOfRanges()
  26. {
  27. $f = new DayOfWeekField();
  28. $this->assertFalse($f->isIncrementsOfRanges('1-2'));
  29. $this->assertTrue($f->isIncrementsOfRanges('1/2'));
  30. $this->assertTrue($f->isIncrementsOfRanges('*/2'));
  31. $this->assertTrue($f->isIncrementsOfRanges('3-12/2'));
  32. }
  33. /**
  34. * @covers \Cron\AbstractField::isInRange
  35. */
  36. public function testTestsIfInRange()
  37. {
  38. $f = new DayOfWeekField();
  39. $this->assertTrue($f->isInRange('1', '1-2'));
  40. $this->assertTrue($f->isInRange('2', '1-2'));
  41. $this->assertTrue($f->isInRange('5', '4-12'));
  42. $this->assertFalse($f->isInRange('3', '4-12'));
  43. $this->assertFalse($f->isInRange('13', '4-12'));
  44. }
  45. /**
  46. * @covers \Cron\AbstractField::isInIncrementsOfRanges
  47. */
  48. public function testTestsIfInIncrementsOfRangesOnZeroStartRange()
  49. {
  50. $f = new MinutesField();
  51. $this->assertTrue($f->isInIncrementsOfRanges('3', '3-59/2'));
  52. $this->assertTrue($f->isInIncrementsOfRanges('13', '3-59/2'));
  53. $this->assertTrue($f->isInIncrementsOfRanges('15', '3-59/2'));
  54. $this->assertTrue($f->isInIncrementsOfRanges('14', '*/2'));
  55. $this->assertFalse($f->isInIncrementsOfRanges('2', '3-59/13'));
  56. $this->assertFalse($f->isInIncrementsOfRanges('14', '*/13'));
  57. $this->assertFalse($f->isInIncrementsOfRanges('14', '3-59/2'));
  58. $this->assertFalse($f->isInIncrementsOfRanges('3', '2-59'));
  59. $this->assertFalse($f->isInIncrementsOfRanges('3', '2'));
  60. $this->assertFalse($f->isInIncrementsOfRanges('3', '*'));
  61. $this->assertFalse($f->isInIncrementsOfRanges('0', '*/0'));
  62. $this->assertFalse($f->isInIncrementsOfRanges('1', '*/0'));
  63. $this->assertTrue($f->isInIncrementsOfRanges('4', '4/1'));
  64. $this->assertFalse($f->isInIncrementsOfRanges('14', '4/1'));
  65. $this->assertFalse($f->isInIncrementsOfRanges('34', '4/1'));
  66. }
  67. /**
  68. * @covers \Cron\AbstractField::isInIncrementsOfRanges
  69. */
  70. public function testTestsIfInIncrementsOfRangesOnOneStartRange()
  71. {
  72. $f = new MonthField();
  73. $this->assertTrue($f->isInIncrementsOfRanges('3', '3-12/2'));
  74. $this->assertFalse($f->isInIncrementsOfRanges('13', '3-12/2'));
  75. $this->assertFalse($f->isInIncrementsOfRanges('15', '3-12/2'));
  76. $this->assertTrue($f->isInIncrementsOfRanges('3', '*/2'));
  77. $this->assertFalse($f->isInIncrementsOfRanges('3', '*/3'));
  78. $this->assertTrue($f->isInIncrementsOfRanges('7', '*/3'));
  79. $this->assertFalse($f->isInIncrementsOfRanges('14', '3-12/2'));
  80. $this->assertFalse($f->isInIncrementsOfRanges('3', '2-12'));
  81. $this->assertFalse($f->isInIncrementsOfRanges('3', '2'));
  82. $this->assertFalse($f->isInIncrementsOfRanges('3', '*'));
  83. $this->assertFalse($f->isInIncrementsOfRanges('0', '*/0'));
  84. $this->assertFalse($f->isInIncrementsOfRanges('1', '*/0'));
  85. $this->assertTrue($f->isInIncrementsOfRanges('4', '4/1'));
  86. $this->assertFalse($f->isInIncrementsOfRanges('14', '4/1'));
  87. $this->assertFalse($f->isInIncrementsOfRanges('34', '4/1'));
  88. }
  89. /**
  90. * @covers \Cron\AbstractField::isSatisfied
  91. */
  92. public function testTestsIfSatisfied()
  93. {
  94. $f = new DayOfWeekField();
  95. $this->assertTrue($f->isSatisfied('12', '3-13'));
  96. $this->assertFalse($f->isSatisfied('15', '3-7/2'));
  97. $this->assertTrue($f->isSatisfied('12', '*'));
  98. $this->assertTrue($f->isSatisfied('12', '12'));
  99. $this->assertFalse($f->isSatisfied('12', '3-11'));
  100. $this->assertFalse($f->isSatisfied('12', '3-7/2'));
  101. $this->assertFalse($f->isSatisfied('12', '11'));
  102. }
  103. /**
  104. * Allows ranges and lists to coexist in the same expression
  105. *
  106. * @see https://github.com/dragonmantank/cron-expression/issues/5
  107. */
  108. public function testAllowRangesAndLists()
  109. {
  110. $expression = '5-7,11-13';
  111. $f = new HoursField();
  112. $this->assertTrue($f->validate($expression));
  113. }
  114. /**
  115. * Makes sure that various types of ranges expand out properly
  116. *
  117. * @see https://github.com/dragonmantank/cron-expression/issues/5
  118. */
  119. public function testGetRangeForExpressionExpandsCorrectly()
  120. {
  121. $f = new HoursField();
  122. $this->assertSame([5, 6, 7, 11, 12, 13], $f->getRangeForExpression('5-7,11-13', 23));
  123. $this->assertSame(['5', '6', '7', '11', '12', '13'], $f->getRangeForExpression('5,6,7,11,12,13', 23));
  124. $this->assertSame([0, 6, 12, 18], $f->getRangeForExpression('*/6', 23));
  125. $this->assertSame([5, 11], $f->getRangeForExpression('5-13/6', 23));
  126. }
  127. }