1<?php 2 3namespace Cron\Tests; 4 5use Cron\MonthField; 6use DateTime; 7use PHPUnit_Framework_TestCase; 8 9/** 10 * @author Michael Dowling <mtdowling@gmail.com> 11 */ 12class MonthFieldTest extends PHPUnit_Framework_TestCase 13{ 14 /** 15 * @covers Cron\MonthField::validate 16 */ 17 public function testValidatesField() 18 { 19 $f = new MonthField(); 20 $this->assertTrue($f->validate('12')); 21 $this->assertTrue($f->validate('*')); 22 $this->assertTrue($f->validate('*/10,2,1-12')); 23 $this->assertFalse($f->validate('1.fix-regexp')); 24 } 25 26 /** 27 * @covers Cron\MonthField::increment 28 */ 29 public function testIncrementsDate() 30 { 31 $d = new DateTime('2011-03-15 11:15:00'); 32 $f = new MonthField(); 33 $f->increment($d); 34 $this->assertEquals('2011-04-01 00:00:00', $d->format('Y-m-d H:i:s')); 35 36 $d = new DateTime('2011-03-15 11:15:00'); 37 $f->increment($d, true); 38 $this->assertEquals('2011-02-28 23:59:00', $d->format('Y-m-d H:i:s')); 39 } 40 41 /** 42 * @covers Cron\MonthField::increment 43 */ 44 public function testIncrementsDateWithThirtyMinuteTimezone() 45 { 46 $tz = date_default_timezone_get(); 47 date_default_timezone_set('America/St_Johns'); 48 $d = new DateTime('2011-03-31 11:59:59'); 49 $f = new MonthField(); 50 $f->increment($d); 51 $this->assertEquals('2011-04-01 00:00:00', $d->format('Y-m-d H:i:s')); 52 53 $d = new DateTime('2011-03-15 11:15:00'); 54 $f->increment($d, true); 55 $this->assertEquals('2011-02-28 23:59:00', $d->format('Y-m-d H:i:s')); 56 date_default_timezone_set($tz); 57 } 58 59 60 /** 61 * @covers Cron\MonthField::increment 62 */ 63 public function testIncrementsYearAsNeeded() 64 { 65 $f = new MonthField(); 66 $d = new DateTime('2011-12-15 00:00:00'); 67 $f->increment($d); 68 $this->assertEquals('2012-01-01 00:00:00', $d->format('Y-m-d H:i:s')); 69 } 70 71 /** 72 * @covers Cron\MonthField::increment 73 */ 74 public function testDecrementsYearAsNeeded() 75 { 76 $f = new MonthField(); 77 $d = new DateTime('2011-01-15 00:00:00'); 78 $f->increment($d, true); 79 $this->assertEquals('2010-12-31 23:59:00', $d->format('Y-m-d H:i:s')); 80 } 81} 82