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
|
|
|
/** |
34
|
|
|
* @test |
35
|
|
|
* @expectedException InvalidArgumentException |
36
|
|
|
*/ |
37
|
|
|
public function throwErrorWhenNumberOfArgumentsIsOver3() |
38
|
|
|
{ |
39
|
|
|
new DateRange('2015-12-01', '2015-12-10', '2015-12-31'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @test |
44
|
|
|
* @expectedException InvalidArgumentException |
45
|
|
|
*/ |
46
|
|
|
public function throwErrorWhenArrayArgumentIsInvalid() |
47
|
|
|
{ |
48
|
|
|
new DateRange(['2015-12-01', '2015-12-10', '2015-12-31']); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** @test */ |
53
|
|
|
public function convertStringToDateTimeObject() |
54
|
|
|
{ |
55
|
|
|
$this->assertEquals(new DateTime('2015-12-31 00:00:00'), DateRange::convertToDateTime('2015-12-31')); |
56
|
|
|
$param = new DateTime('2015-12-31 00:00:00'); |
57
|
|
|
$this->assertSame($param, DateRange::convertToDateTime($param)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @test |
62
|
|
|
* @expectedException InvalidArgumentException |
63
|
|
|
*/ |
64
|
|
|
public function throwErrorWhenInvalidDatetimeString() |
65
|
|
|
{ |
66
|
|
|
DateRange::convertToDateTime('2015-33-33'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** @test */ |
70
|
|
|
public function defaultInterval() |
71
|
|
|
{ |
72
|
|
|
$range = new DateRange([$this->start, $this->end]); |
73
|
|
|
$this->assertEquals(new DateInterval('P1D'), $range->getInterval()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** @test */ |
77
|
|
|
public function changeInterval() |
78
|
|
|
{ |
79
|
|
|
$range = new DateRange([$this->start, $this->end]); |
80
|
|
|
$range->setInterval(new DateInterval('P1Y')); |
81
|
|
|
$this->assertEquals(new DateInterval('P1Y'), $range->getInterval()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** @test */ |
85
|
|
|
public function getDatePeriod() |
86
|
|
|
{ |
87
|
|
|
$range = new DateRange([$this->start, $this->end]); |
88
|
|
|
$periodEnd = clone $this->end; |
89
|
|
|
$periodEnd->modify('+1 sec'); |
90
|
|
|
$expected = new DatePeriod($this->start, new DateInterval('P1D'), $periodEnd); |
91
|
|
|
$this->assertEquals($expected, $range->getDatePeriod()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** @test */ |
95
|
|
|
public function getIterator() |
96
|
|
|
{ |
97
|
|
|
$range = new DateRange(['2015-12-01', '2015-12-04']); |
98
|
|
|
$results = []; |
99
|
|
|
foreach ($range as $d) { |
100
|
|
|
$results[] = $d; |
101
|
|
|
} |
102
|
|
|
$expected = [ |
103
|
|
|
new DateTime('2015-12-01'), |
104
|
|
|
new DateTime('2015-12-02'), |
105
|
|
|
new DateTime('2015-12-03'), |
106
|
|
|
new DateTime('2015-12-04'), |
107
|
|
|
]; |
108
|
|
|
$this->assertEquals($expected, $results); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** @test */ |
113
|
|
|
public function testContains() |
114
|
|
|
{ |
115
|
|
|
$range = new DateRange([$this->start, $this->end]); |
116
|
|
|
$this->assertFalse($range->contains('2015-11-30')); |
117
|
|
|
$this->assertTrue($range->contains('2015-12-01')); |
118
|
|
|
$this->assertTrue($range->contains('2015-12-31')); |
119
|
|
|
$this->assertFalse($range->contains('2016-01-01')); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @test |
124
|
|
|
* @expectedException InvalidArgumentException |
125
|
|
|
*/ |
126
|
|
|
public function constructInvalidArrayArgument() |
127
|
|
|
{ |
128
|
|
|
// construct parameter should have an array with two values. |
129
|
|
|
new DateRange(['today']); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @test |
134
|
|
|
* @expectedException InvalidArgumentException |
135
|
|
|
*/ |
136
|
|
|
public function shouldHaveEndDateIsAfterThanStartDate() |
137
|
|
|
{ |
138
|
|
|
new DateRange(['tomorrow', 'today']); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** @test */ |
142
|
|
|
public function excludeStartDate() |
143
|
|
|
{ |
144
|
|
|
$range = new DateRange([$this->start, $this->end]); |
145
|
|
|
$range->excludeStartDate(); |
146
|
|
|
$this->assertFalse($range->contains('2015-12-01')); |
147
|
|
|
$this->assertTrue($range->contains('2015-12-02')); |
148
|
|
|
|
149
|
|
|
// dirty test .. (check the first value only) |
150
|
|
|
foreach ($range as $d) { |
151
|
|
|
$this->assertEquals(new DateTime('2015-12-02'), $d); |
152
|
|
|
break; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** @test */ |
157
|
|
|
public function excludeEndDate() |
158
|
|
|
{ |
159
|
|
|
$range = new DateRange([$this->start, $this->end]); |
160
|
|
|
$range->excludeEndDate(); |
161
|
|
|
$this->assertFalse($range->contains('2015-12-31')); |
162
|
|
|
$this->assertTrue($range->contains('2015-12-30')); |
163
|
|
|
|
164
|
|
|
$last = null; |
165
|
|
|
// dirty test.. (check the last value only) |
166
|
|
|
foreach ($range as $d) { |
167
|
|
|
$last = $d; |
168
|
|
|
} |
169
|
|
|
$this->assertEquals(new DateTime('2015-12-30'), $last); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** @test */ |
173
|
|
|
public function diff() |
174
|
|
|
{ |
175
|
|
|
$range = new DateRange('2015-12-01', '2015-12-03'); |
176
|
|
|
$this->assertEquals('+2 days', $range->diff()->format('%R%a days')); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** @test */ |
180
|
|
|
public function toString() |
181
|
|
|
{ |
182
|
|
|
$range = new DateRange('2015-12-01', '2015-12-03'); |
183
|
|
|
$this->assertEquals('2015-12-01 ~ 2015-12-03', $range); |
184
|
|
|
$this->assertEquals('2015/12/01 - 2015/12/03', $range->toString('Y/m/d', '-')); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|