0
0

DayOfWeekFieldTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 PHPUnit_Framework_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('*'));
  19. $this->assertTrue($f->validate('*/3,1,1-12'));
  20. $this->assertTrue($f->validate('SUN-2'));
  21. $this->assertFalse($f->validate('1.'));
  22. }
  23. /**
  24. * @covers Cron\DayOfWeekField::isSatisfiedBy
  25. */
  26. public function testChecksIfSatisfied()
  27. {
  28. $f = new DayOfWeekField();
  29. $this->assertTrue($f->isSatisfiedBy(new DateTime(), '?'));
  30. }
  31. /**
  32. * @covers Cron\DayOfWeekField::increment
  33. */
  34. public function testIncrementsDate()
  35. {
  36. $d = new DateTime('2011-03-15 11:15:00');
  37. $f = new DayOfWeekField();
  38. $f->increment($d);
  39. $this->assertEquals('2011-03-16 00:00:00', $d->format('Y-m-d H:i:s'));
  40. $d = new DateTime('2011-03-15 11:15:00');
  41. $f->increment($d, true);
  42. $this->assertEquals('2011-03-14 23:59:00', $d->format('Y-m-d H:i:s'));
  43. }
  44. /**
  45. * @covers Cron\DayOfWeekField::isSatisfiedBy
  46. * @expectedException InvalidArgumentException
  47. * @expectedExceptionMessage Weekday must be a value between 0 and 7. 12 given
  48. */
  49. public function testValidatesHashValueWeekday()
  50. {
  51. $f = new DayOfWeekField();
  52. $this->assertTrue($f->isSatisfiedBy(new DateTime(), '12#1'));
  53. }
  54. /**
  55. * @covers Cron\DayOfWeekField::isSatisfiedBy
  56. * @expectedException InvalidArgumentException
  57. * @expectedExceptionMessage There are never more than 5 of a given weekday in a month
  58. */
  59. public function testValidatesHashValueNth()
  60. {
  61. $f = new DayOfWeekField();
  62. $this->assertTrue($f->isSatisfiedBy(new DateTime(), '3#6'));
  63. }
  64. /**
  65. * @covers Cron\DayOfWeekField::validate
  66. */
  67. public function testValidateWeekendHash()
  68. {
  69. $f = new DayOfWeekField();
  70. $this->assertTrue($f->validate('MON#1'));
  71. $this->assertTrue($f->validate('TUE#2'));
  72. $this->assertTrue($f->validate('WED#3'));
  73. $this->assertTrue($f->validate('THU#4'));
  74. $this->assertTrue($f->validate('FRI#5'));
  75. $this->assertTrue($f->validate('SAT#1'));
  76. $this->assertTrue($f->validate('SUN#3'));
  77. $this->assertTrue($f->validate('MON#1,MON#3'));
  78. }
  79. /**
  80. * @covers Cron\DayOfWeekField::isSatisfiedBy
  81. */
  82. public function testHandlesZeroAndSevenDayOfTheWeekValues()
  83. {
  84. $f = new DayOfWeekField();
  85. $this->assertTrue($f->isSatisfiedBy(new DateTime('2011-09-04 00:00:00'), '0-2'));
  86. $this->assertTrue($f->isSatisfiedBy(new DateTime('2011-09-04 00:00:00'), '6-0'));
  87. $this->assertTrue($f->isSatisfiedBy(new DateTime('2014-04-20 00:00:00'), 'SUN'));
  88. $this->assertTrue($f->isSatisfiedBy(new DateTime('2014-04-20 00:00:00'), 'SUN#3'));
  89. $this->assertTrue($f->isSatisfiedBy(new DateTime('2014-04-20 00:00:00'), '0#3'));
  90. $this->assertTrue($f->isSatisfiedBy(new DateTime('2014-04-20 00:00:00'), '7#3'));
  91. }
  92. /**
  93. * @see https://github.com/mtdowling/cron-expression/issues/47
  94. */
  95. public function testIssue47() {
  96. $f = new DayOfWeekField();
  97. $this->assertFalse($f->validate('mon,'));
  98. $this->assertFalse($f->validate('mon-'));
  99. $this->assertFalse($f->validate('*/2,'));
  100. $this->assertFalse($f->validate('-mon'));
  101. $this->assertFalse($f->validate(',1'));
  102. $this->assertFalse($f->validate('*-'));
  103. $this->assertFalse($f->validate(',-'));
  104. }
  105. }