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