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