DayOfWeekFieldTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. declare(strict_types=1);
  3. namespace Cron\Tests;
  4. use Cron\DayOfWeekField;
  5. use DateTime;
  6. use DateTimeImmutable;
  7. use InvalidArgumentException;
  8. use PHPUnit\Framework\TestCase;
  9. /**
  10. * @author Michael Dowling <mtdowling@gmail.com>
  11. */
  12. class DayOfWeekFieldTest extends TestCase
  13. {
  14. /**
  15. * @covers \Cron\DayOfWeekField::validate
  16. */
  17. public function testValidatesField()
  18. {
  19. $f = new DayOfWeekField();
  20. $this->assertTrue($f->validate('1'));
  21. $this->assertTrue($f->validate('01'));
  22. $this->assertTrue($f->validate('00'));
  23. $this->assertTrue($f->validate('*'));
  24. $this->assertTrue($f->validate('?'));
  25. $this->assertFalse($f->validate('*/3,1,1-12'));
  26. $this->assertTrue($f->validate('SUN-2'));
  27. $this->assertFalse($f->validate('1.'));
  28. }
  29. /**
  30. * @covers \Cron\DayOfWeekField::isSatisfiedBy
  31. */
  32. public function testChecksIfSatisfied()
  33. {
  34. $f = new DayOfWeekField();
  35. $this->assertTrue($f->isSatisfiedBy(new DateTime(), '?'));
  36. $this->assertTrue($f->isSatisfiedBy(new DateTimeImmutable(), '?'));
  37. }
  38. /**
  39. * @covers \Cron\DayOfWeekField::increment
  40. */
  41. public function testIncrementsDate()
  42. {
  43. $d = new DateTime('2011-03-15 11:15:00');
  44. $f = new DayOfWeekField();
  45. $f->increment($d);
  46. $this->assertSame('2011-03-16 00:00:00', $d->format('Y-m-d H:i:s'));
  47. $d = new DateTime('2011-03-15 11:15:00');
  48. $f->increment($d, true);
  49. $this->assertSame('2011-03-14 23:59:00', $d->format('Y-m-d H:i:s'));
  50. }
  51. /**
  52. * @covers \Cron\DayOfWeekField::increment
  53. */
  54. public function testIncrementsDateTimeImmutable()
  55. {
  56. $d = new DateTimeImmutable('2011-03-15 11:15:00');
  57. $f = new DayOfWeekField();
  58. $f->increment($d);
  59. $this->assertSame('2011-03-16 00:00:00', $d->format('Y-m-d H:i:s'));
  60. }
  61. /**
  62. * @covers \Cron\DayOfWeekField::isSatisfiedBy
  63. */
  64. public function testValidatesHashValueWeekday()
  65. {
  66. $this->expectException(InvalidArgumentException::class);
  67. $this->expectExceptionMessage('Weekday must be a value between 0 and 7. 12 given');
  68. $f = new DayOfWeekField();
  69. $this->assertTrue($f->isSatisfiedBy(new DateTime(), '12#1'));
  70. }
  71. /**
  72. * @covers \Cron\DayOfWeekField::isSatisfiedBy
  73. */
  74. public function testValidatesHashValueNth()
  75. {
  76. $this->expectException(InvalidArgumentException::class);
  77. $this->expectExceptionMessage('There are never more than 5 or less than 1 of a given weekday in a month');
  78. $f = new DayOfWeekField();
  79. $this->assertTrue($f->isSatisfiedBy(new DateTime(), '3#6'));
  80. }
  81. /**
  82. * @covers \Cron\DayOfWeekField::validate
  83. */
  84. public function testValidateWeekendHash()
  85. {
  86. $f = new DayOfWeekField();
  87. $this->assertTrue($f->validate('MON#1'));
  88. $this->assertTrue($f->validate('TUE#2'));
  89. $this->assertTrue($f->validate('WED#3'));
  90. $this->assertTrue($f->validate('THU#4'));
  91. $this->assertTrue($f->validate('FRI#5'));
  92. $this->assertTrue($f->validate('SAT#1'));
  93. $this->assertTrue($f->validate('SUN#3'));
  94. $this->assertTrue($f->validate('MON#1,MON#3'));
  95. }
  96. /**
  97. * @covers \Cron\DayOfWeekField::isSatisfiedBy
  98. */
  99. public function testHandlesZeroAndSevenDayOfTheWeekValues()
  100. {
  101. $f = new DayOfWeekField();
  102. $this->assertTrue($f->isSatisfiedBy(new DateTime('2011-09-04 00:00:00'), '0-2'));
  103. $this->assertTrue($f->isSatisfiedBy(new DateTime('2011-09-04 00:00:00'), '6-0'));
  104. $this->assertTrue($f->isSatisfiedBy(new DateTime('2014-04-20 00:00:00'), 'SUN'));
  105. $this->assertTrue($f->isSatisfiedBy(new DateTime('2014-04-20 00:00:00'), 'SUN#3'));
  106. $this->assertTrue($f->isSatisfiedBy(new DateTime('2014-04-20 00:00:00'), '0#3'));
  107. $this->assertTrue($f->isSatisfiedBy(new DateTime('2014-04-20 00:00:00'), '7#3'));
  108. }
  109. /**
  110. * @covers \Cron\DayOfWeekField::isSatisfiedBy
  111. */
  112. public function testHandlesLastWeekdayOfTheMonth()
  113. {
  114. $f = new DayOfWeekField();
  115. $this->assertTrue($f->isSatisfiedBy(new DateTime('2018-12-28 00:00:00'), 'FRIL'));
  116. $this->assertTrue($f->isSatisfiedBy(new DateTime('2018-12-28 00:00:00'), '5L'));
  117. $this->assertFalse($f->isSatisfiedBy(new DateTime('2018-12-21 00:00:00'), 'FRIL'));
  118. $this->assertFalse($f->isSatisfiedBy(new DateTime('2018-12-21 00:00:00'), '5L'));
  119. }
  120. /**
  121. * @see https://github.com/mtdowling/cron-expression/issues/47
  122. */
  123. public function testIssue47()
  124. {
  125. $f = new DayOfWeekField();
  126. $this->assertFalse($f->validate('mon,'));
  127. $this->assertFalse($f->validate('mon-'));
  128. $this->assertFalse($f->validate('*/2,'));
  129. $this->assertFalse($f->validate('-mon'));
  130. $this->assertFalse($f->validate(',1'));
  131. $this->assertFalse($f->validate('*-'));
  132. $this->assertFalse($f->validate(',-'));
  133. }
  134. /**
  135. * @see https://github.com/laravel/framework/commit/07d160ac3cc9764d5b429734ffce4fa311385403
  136. */
  137. public function testLiteralsExpandProperly()
  138. {
  139. $f = new DayOfWeekField();
  140. $this->assertTrue($f->validate('MON-FRI'));
  141. $this->assertSame([1, 2, 3, 4, 5], $f->getRangeForExpression('MON-FRI', 7));
  142. }
  143. /**
  144. * Incoming literals should ignore case
  145. *
  146. * @author Chris Tankersley <chris@ctankersley.com?
  147. * @since 2019-07-29
  148. * @see https://github.com/dragonmantank/cron-expression/issues/24
  149. */
  150. public function testLiteralsIgnoreCasingProperly()
  151. {
  152. $f = new DayOfWeekField();
  153. $this->assertTrue($f->validate('MON'));
  154. $this->assertTrue($f->validate('Mon'));
  155. $this->assertTrue($f->validate('mon'));
  156. $this->assertTrue($f->validate('Mon,Wed,Fri'));
  157. }
  158. }