Completed
Push — master ( ecd5d7...f9662e )
by Julián
11:22
created

CronTest::testIsActive()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
/*
4
 * janitor (http://juliangut.com/janitor).
5
 * Effortless maintenance management.
6
 *
7
 * @license BSD-3-Clause
8
 * @link https://github.com/juliangut/janitor
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
namespace Janitor\Test\Watcher\Scheduled;
13
14
use Janitor\Watcher\Scheduled\Cron;
15
16
/**
17
 * Class CronTest.
18
 */
19
class CronTest extends \PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * @var Cron
23
     */
24
    protected $watcher;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function setUp()
30
    {
31
        $this->watcher = new Cron('0 0 1 1 *', 'PT1M', 'UTC');
32
    }
33
34
    /**
35
     * @expectedException \InvalidArgumentException
36
     */
37
    public function testExpressionMutatorsAccessors()
38
    {
39
        $this->watcher->setExpression(Cron::PERIOD_ANNUALLY);
40
        self::assertEquals('0 0 1 1 *', $this->watcher->getExpression());
41
42
        $this->watcher->setExpression('invalidExpression');
43
    }
44
45
    /**
46
     * @expectedException \InvalidArgumentException
47
     */
48
    public function testIntervalMutatorsAccessors()
49
    {
50
        $this->watcher->setExpression(Cron::PERIOD_YEARLY);
51
52
        self::assertNull($this->watcher->getStart());
53
        self::assertNull($this->watcher->getEnd());
54
55
        $this->watcher->setInterval('P1Y');
56
57
        $start = new \DateTime('now', new \DateTimeZone('UTC'));
58
        $start->setDate($start->format('Y'), 1, 1);
59
        $end = clone $start;
60
        $end->add(new \DateInterval('P1Y'));
61
62
        self::assertEquals($start->format('Y m d'), $this->watcher->getStart()->format('Y m d'));
63
        self::assertEquals($end->format('Y m d'), $this->watcher->getEnd()->format('Y m d'));
64
65
        self::assertEquals(new \DateInterval('P1Y'), $this->watcher->getInterval());
66
        $this->watcher->setInterval('invalidDateInterval');
67
    }
68
69
    public function testIsNotActive()
70
    {
71
        $time   = new \DateTime('now', new \DateTimeZone('UTC'));
72
        $minute = $time->format('i');
73
        $hour   = $time->format('H');
74
        $day    = $time->format('d');
75
        $month  = $time->format('m');
76
77
        $this->watcher->setInterval('PT10S');
78
79
        $this->watcher->setExpression(sprintf('%s * * * *', $minute - 10 > -1 ? $minute - 10 : 50));
80
        self::assertFalse($this->watcher->isActive());
81
82
        $this->watcher->setExpression(sprintf('* %s * * *', $hour - 1 > -1 ? $hour - 1 : 23));
83
        self::assertFalse($this->watcher->isActive());
84
85
        $this->watcher->setExpression(sprintf('* * %s * *', $day - 1 > -1 ? $day - 1 : 28));
86
        self::assertFalse($this->watcher->isActive());
87
88
        $this->watcher->setExpression(sprintf('* * * %s *', $month - 1 > -1 ? $month - 1 : 12));
89
        self::assertFalse($this->watcher->isActive());
90
    }
91
92
    public function testIsActive()
93
    {
94
        $time = new \DateTime('now', new \DateTimeZone('UTC'));
95
        $minute = $time->format('i');
96
        $hour   = $time->format('H');
97
        $day    = $time->format('d');
98
        $month  = $time->format('m');
99
100
        $this->watcher->setInterval('PT1M');
101
        $this->watcher->setExpression(sprintf('%s * * * *', $minute));
102
        self::assertTrue($this->watcher->isActive());
103
104
        $this->watcher->setInterval('PT1H');
105
        $this->watcher->setExpression(sprintf('* %s * * *', $hour));
106
        self::assertTrue($this->watcher->isActive());
107
108
        $this->watcher->setInterval('P1D');
109
        $this->watcher->setExpression(sprintf('* * %s * *', $day));
110
        self::assertTrue($this->watcher->isActive());
111
112
        $this->watcher->setInterval('P1M');
113
        $this->watcher->setExpression(sprintf('* * * %s *', $month));
114
        self::assertTrue($this->watcher->isActive());
115
    }
116
117
    public function testScheduledTimes()
118
    {
119
        $currentTime = new \DateTime('now', new \DateTimeZone('UTC'));
120
        $currentTime->setTime($currentTime->format('H'), $currentTime->format('i'));
121
        $currentTime->add(new \DateInterval('PT1M')); //skip current minute
122
123
        $endTime = clone $currentTime;
124
        $endTime->add(new \DateInterval('PT1H'));
125
126
        $this->watcher->setInterval('PT1H');
127
        $this->watcher->setExpression('* ' . $currentTime->format('H') . ' ' . $currentTime->format('d') . ' * *');
128
129
        self::assertTrue($this->watcher->isScheduled());
130
        self::assertEquals([['start' => $currentTime, 'end' => $endTime]], $this->watcher->getScheduledTimes(1));
131
132
        $this->watcher->setExpression('* ' . $currentTime->format('H') . ' ' . $currentTime->format('D') . ' * *');
133
        self::assertEquals([], $this->watcher->getScheduledTimes());
134
    }
135
}
136