DayOfWeekFieldTest.php 4.2 KB

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