No Description

DayOfWeekFieldTest.php 3.7KB

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