DayOfWeekFieldTest.php 5.2 KB

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