1
|
|
|
<?php |
2
|
|
|
namespace Brtriver\DateRange\Test; |
3
|
|
|
|
4
|
|
|
use Brtriver\DateRange\DateRange; |
5
|
|
|
use Datetime; |
6
|
|
|
use DateInterval; |
7
|
|
|
use DatePeriod; |
8
|
|
|
|
9
|
|
|
class DateRangeTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
private $start; |
12
|
|
|
private $end; |
13
|
|
|
public function setUp() |
14
|
|
|
{ |
15
|
|
|
$this->start = new DateTime('2015-12-01'); |
16
|
|
|
$this->end = new DateTime('2015-12-31'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** @test */ |
20
|
|
|
public function acceptArguments() |
21
|
|
|
{ |
22
|
|
|
// as two date parameters. |
23
|
|
|
$range = new DateRange($this->start, $this->end); |
24
|
|
|
$this->assertSame($this->start, $range->getStart()); |
25
|
|
|
$this->assertSame($this->end, $range->getEnd()); |
26
|
|
|
|
27
|
|
|
// as one array of date |
28
|
|
|
$range = new DateRange([$this->start, $this->end]); |
29
|
|
|
$this->assertSame($this->start, $range->getStart()); |
30
|
|
|
$this->assertSame($this->end, $range->getEnd()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** @test */ |
34
|
|
|
public function convertStringToDateTimeObject() |
35
|
|
|
{ |
36
|
|
|
$this->assertEquals(new DateTime('2015-12-31 00:00:00'), DateRange::convertToDateTime('2015-12-31')); |
37
|
|
|
$param = new DateTime('2015-12-31 00:00:00'); |
38
|
|
|
$this->assertSame($param, DateRange::convertToDateTime($param)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @test |
43
|
|
|
* @expectedException InvalidArgumentException |
44
|
|
|
*/ |
45
|
|
|
public function throwErrorWhenInvalidDatetimeString() |
46
|
|
|
{ |
47
|
|
|
DateRange::convertToDateTime('2015-33-33'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** @test */ |
51
|
|
|
public function defaultInterval() |
52
|
|
|
{ |
53
|
|
|
$range = new DateRange([$this->start, $this->end]); |
54
|
|
|
$this->assertEquals(new DateInterval('P1D'), $range->getInterval()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @test */ |
58
|
|
|
public function changeInterval() |
59
|
|
|
{ |
60
|
|
|
$range = new DateRange([$this->start, $this->end]); |
61
|
|
|
$range->setInterval(new DateInterval('P1Y')); |
62
|
|
|
$this->assertEquals(new DateInterval('P1Y'), $range->getInterval()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @test */ |
66
|
|
|
public function getDatePeriod() |
67
|
|
|
{ |
68
|
|
|
$range = new DateRange([$this->start, $this->end]); |
69
|
|
|
$periodEnd = clone $this->end; |
70
|
|
|
$periodEnd->modify('+1 sec'); |
71
|
|
|
$expected = new DatePeriod($this->start, new DateInterval('P1D'), $periodEnd); |
72
|
|
|
$this->assertEquals($expected, $range->getDatePeriod()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** @test */ |
76
|
|
|
public function getIterator() |
77
|
|
|
{ |
78
|
|
|
$range = new DateRange(['2015-12-01', '2015-12-04']); |
79
|
|
|
$results = []; |
80
|
|
|
foreach($range as $d) { |
81
|
|
|
$results[] = $d; |
82
|
|
|
} |
83
|
|
|
$expected = [ |
84
|
|
|
new DateTime('2015-12-01'), |
85
|
|
|
new DateTime('2015-12-02'), |
86
|
|
|
new DateTime('2015-12-03'), |
87
|
|
|
new DateTime('2015-12-04'), |
88
|
|
|
]; |
89
|
|
|
$this->assertEquals($expected, $results); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** @test */ |
94
|
|
|
public function testContains() |
95
|
|
|
{ |
96
|
|
|
$range = new DateRange([$this->start, $this->end]); |
97
|
|
|
$this->assertFalse($range->contains('2015-11-30')); |
98
|
|
|
$this->assertTrue($range->contains('2015-12-01')); |
99
|
|
|
$this->assertTrue($range->contains('2015-12-31')); |
100
|
|
|
$this->assertFalse($range->contains('2016-01-01')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @test |
105
|
|
|
* @expectedException InvalidArgumentException |
106
|
|
|
*/ |
107
|
|
|
public function constructInvalidArrayArgument() |
108
|
|
|
{ |
109
|
|
|
// construct parameter should have an array with two values. |
110
|
|
|
new DateRange(['today']); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @test |
115
|
|
|
* @expectedException InvalidArgumentException |
116
|
|
|
*/ |
117
|
|
|
public function shouldHaveEndDateIsAfterThanStartDate() |
118
|
|
|
{ |
119
|
|
|
new DateRange(['tomorrow', 'today']); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** @test */ |
123
|
|
|
public function excludeStartDate() |
124
|
|
|
{ |
125
|
|
|
$range = new DateRange([$this->start, $this->end]); |
126
|
|
|
$range->excludeStartDate(); |
127
|
|
|
$this->assertFalse($range->contains('2015-12-01')); |
128
|
|
|
$this->assertTrue($range->contains('2015-12-02')); |
129
|
|
|
|
130
|
|
|
// dirty test .. (check the first value only) |
131
|
|
|
foreach ($range as $d) { |
132
|
|
|
$this->assertEquals(new DateTime('2015-12-02'), $d); |
133
|
|
|
break; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** @test */ |
138
|
|
|
public function excludeEndDate() |
139
|
|
|
{ |
140
|
|
|
$range = new DateRange([$this->start, $this->end]); |
141
|
|
|
$range->excludeEndDate(); |
142
|
|
|
$this->assertFalse($range->contains('2015-12-31')); |
143
|
|
|
$this->assertTrue($range->contains('2015-12-30')); |
144
|
|
|
|
145
|
|
|
$last = null; |
146
|
|
|
// dirty test.. (check the last value only) |
147
|
|
|
foreach ($range as $d) { |
148
|
|
|
$last = $d; |
149
|
|
|
} |
150
|
|
|
$this->assertEquals(new DateTime('2015-12-30'), $last); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|