1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HMLB\Date\Tests\Interval; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Date package. |
7
|
|
|
* |
8
|
|
|
* (c) Hugues Maignol <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
use DateInterval; |
15
|
|
|
use HMLB\Date\Date; |
16
|
|
|
use HMLB\Date\Interval; |
17
|
|
|
use HMLB\Date\Tests\AbstractTestCase; |
18
|
|
|
|
19
|
|
|
class ConstructTest extends AbstractTestCase |
20
|
|
|
{ |
21
|
|
|
public function testDefaults() |
22
|
|
|
{ |
23
|
|
|
$ci = new Interval(); |
24
|
|
|
$this->assertInstanceOfInterval($ci); |
25
|
|
|
$this->assertInterval($ci, 1, 0, 0, 0, 0, 0); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testNulls() |
29
|
|
|
{ |
30
|
|
|
$ci = new Interval(null, null, null, null, null, null); |
31
|
|
|
$this->assertInterval($ci, 0, 0, 0, 0, 0, 0); |
32
|
|
|
$ci = Interval::days(null); |
33
|
|
|
$this->assertInstanceOfInterval($ci); |
34
|
|
|
$this->assertInterval($ci, 0, 0, 0, 0, 0, 0); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testZeroes() |
38
|
|
|
{ |
39
|
|
|
$ci = new Interval(0, 0, 0, 0, 0, 0); |
40
|
|
|
$this->assertInterval($ci, 0, 0, 0, 0, 0, 0); |
41
|
|
|
|
42
|
|
|
$ci = Interval::days(0); |
43
|
|
|
$this->assertInstanceOfInterval($ci); |
44
|
|
|
$this->assertInterval($ci, 0, 0, 0, 0, 0, 0); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testZeroesChained() |
48
|
|
|
{ |
49
|
|
|
$ci = Interval::days(0)->week(0)->minutes(0); |
50
|
|
|
$this->assertInstanceOfInterval($ci); |
51
|
|
|
$this->assertInterval($ci, 0, 0, 0, 0, 0, 0); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testYears() |
55
|
|
|
{ |
56
|
|
|
$ci = new Interval(1); |
57
|
|
|
$this->assertInstanceOfInterval($ci); |
58
|
|
|
$this->assertInterval($ci, 1, 0, 0, 0, 0, 0); |
59
|
|
|
|
60
|
|
|
$ci = Interval::years(2); |
61
|
|
|
$this->assertInstanceOfInterval($ci); |
62
|
|
|
$this->assertInterval($ci, 2, 0, 0, 0, 0, 0); |
63
|
|
|
|
64
|
|
|
$ci = Interval::year(); |
65
|
|
|
$this->assertInstanceOfInterval($ci); |
66
|
|
|
$this->assertInterval($ci, 1, 0, 0, 0, 0, 0); |
67
|
|
|
|
68
|
|
|
$ci = Interval::year(3); |
69
|
|
|
$this->assertInstanceOfInterval($ci); |
70
|
|
|
$this->assertInterval($ci, 3, 0, 0, 0, 0, 0); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testMonths() |
74
|
|
|
{ |
75
|
|
|
$ci = new Interval(0, 1); |
76
|
|
|
$this->assertInstanceOfInterval($ci); |
77
|
|
|
$this->assertInterval($ci, 0, 1, 0, 0, 0, 0); |
78
|
|
|
|
79
|
|
|
$ci = Interval::months(2); |
80
|
|
|
$this->assertInstanceOfInterval($ci); |
81
|
|
|
$this->assertInterval($ci, 0, 2, 0, 0, 0, 0); |
82
|
|
|
|
83
|
|
|
$ci = Interval::month(); |
84
|
|
|
$this->assertInstanceOfInterval($ci); |
85
|
|
|
$this->assertInterval($ci, 0, 1, 0, 0, 0, 0); |
86
|
|
|
|
87
|
|
|
$ci = Interval::month(3); |
88
|
|
|
$this->assertInstanceOfInterval($ci); |
89
|
|
|
$this->assertInterval($ci, 0, 3, 0, 0, 0, 0); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
View Code Duplication |
public function testWeeks() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$ci = new Interval(0, 0, 1); |
95
|
|
|
$this->assertInstanceOfInterval($ci); |
96
|
|
|
$this->assertInterval($ci, 0, 0, 7, 0, 0, 0); |
97
|
|
|
|
98
|
|
|
$ci = Interval::weeks(2); |
99
|
|
|
$this->assertInstanceOfInterval($ci); |
100
|
|
|
$this->assertInterval($ci, 0, 0, 14, 0, 0, 0); |
101
|
|
|
|
102
|
|
|
$ci = Interval::week(); |
103
|
|
|
$this->assertInstanceOfInterval($ci); |
104
|
|
|
$this->assertInterval($ci, 0, 0, 7, 0, 0, 0); |
105
|
|
|
|
106
|
|
|
$ci = Interval::week(3); |
107
|
|
|
$this->assertInstanceOfInterval($ci); |
108
|
|
|
$this->assertInterval($ci, 0, 0, 21, 0, 0, 0); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testDays() |
112
|
|
|
{ |
113
|
|
|
$ci = new Interval(0, 0, 0, 1); |
114
|
|
|
$this->assertInstanceOfInterval($ci); |
115
|
|
|
$this->assertInterval($ci, 0, 0, 1, 0, 0, 0); |
116
|
|
|
|
117
|
|
|
$ci = Interval::days(2); |
118
|
|
|
$this->assertInstanceOfInterval($ci); |
119
|
|
|
$this->assertInterval($ci, 0, 0, 2, 0, 0, 0); |
120
|
|
|
|
121
|
|
|
$ci = Interval::dayz(2); |
122
|
|
|
$this->assertInstanceOfInterval($ci); |
123
|
|
|
$this->assertInterval($ci, 0, 0, 2, 0, 0, 0); |
124
|
|
|
|
125
|
|
|
$ci = Interval::day(); |
126
|
|
|
$this->assertInstanceOfInterval($ci); |
127
|
|
|
$this->assertInterval($ci, 0, 0, 1, 0, 0, 0); |
128
|
|
|
|
129
|
|
|
$ci = Interval::day(3); |
130
|
|
|
$this->assertInstanceOfInterval($ci); |
131
|
|
|
$this->assertInterval($ci, 0, 0, 3, 0, 0, 0); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
public function testHours() |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$ci = new Interval(0, 0, 0, 0, 1); |
137
|
|
|
$this->assertInstanceOfInterval($ci); |
138
|
|
|
$this->assertInterval($ci, 0, 0, 0, 1, 0, 0); |
139
|
|
|
|
140
|
|
|
$ci = Interval::hours(2); |
141
|
|
|
$this->assertInstanceOfInterval($ci); |
142
|
|
|
$this->assertInterval($ci, 0, 0, 0, 2, 0, 0); |
143
|
|
|
|
144
|
|
|
$ci = Interval::hour(); |
145
|
|
|
$this->assertInstanceOfInterval($ci); |
146
|
|
|
$this->assertInterval($ci, 0, 0, 0, 1, 0, 0); |
147
|
|
|
|
148
|
|
|
$ci = Interval::hour(3); |
149
|
|
|
$this->assertInstanceOfInterval($ci); |
150
|
|
|
$this->assertInterval($ci, 0, 0, 0, 3, 0, 0); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
View Code Duplication |
public function testMinutes() |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$ci = new Interval(0, 0, 0, 0, 0, 1); |
156
|
|
|
$this->assertInstanceOfInterval($ci); |
157
|
|
|
$this->assertInterval($ci, 0, 0, 0, 0, 1, 0); |
158
|
|
|
|
159
|
|
|
$ci = Interval::minutes(2); |
160
|
|
|
$this->assertInstanceOfInterval($ci); |
161
|
|
|
$this->assertInterval($ci, 0, 0, 0, 0, 2, 0); |
162
|
|
|
|
163
|
|
|
$ci = Interval::minute(); |
164
|
|
|
$this->assertInstanceOfInterval($ci); |
165
|
|
|
$this->assertInterval($ci, 0, 0, 0, 0, 1, 0); |
166
|
|
|
|
167
|
|
|
$ci = Interval::minute(3); |
168
|
|
|
$this->assertInstanceOfInterval($ci); |
169
|
|
|
$this->assertInterval($ci, 0, 0, 0, 0, 3, 0); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
View Code Duplication |
public function testSeconds() |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
$ci = new Interval(0, 0, 0, 0, 0, 0, 1); |
175
|
|
|
$this->assertInstanceOfInterval($ci); |
176
|
|
|
$this->assertInterval($ci, 0, 0, 0, 0, 0, 1); |
177
|
|
|
|
178
|
|
|
$ci = Interval::seconds(2); |
179
|
|
|
$this->assertInstanceOfInterval($ci); |
180
|
|
|
$this->assertInterval($ci, 0, 0, 0, 0, 0, 2); |
181
|
|
|
|
182
|
|
|
$ci = Interval::second(); |
183
|
|
|
$this->assertInstanceOfInterval($ci); |
184
|
|
|
$this->assertInterval($ci, 0, 0, 0, 0, 0, 1); |
185
|
|
|
|
186
|
|
|
$ci = Interval::second(3); |
187
|
|
|
$this->assertInstanceOfInterval($ci); |
188
|
|
|
$this->assertInterval($ci, 0, 0, 0, 0, 0, 3); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testYearsAndHours() |
192
|
|
|
{ |
193
|
|
|
$ci = new Interval(5, 0, 0, 0, 3, 0, 0); |
194
|
|
|
$this->assertInstanceOfInterval($ci); |
195
|
|
|
$this->assertInterval($ci, 5, 0, 0, 3, 0, 0); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function testAll() |
199
|
|
|
{ |
200
|
|
|
$ci = new Interval(5, 6, 2, 5, 9, 10, 11); |
201
|
|
|
$this->assertInstanceOfInterval($ci); |
202
|
|
|
$this->assertInterval($ci, 5, 6, 19, 9, 10, 11); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function testAllWithCreate() |
206
|
|
|
{ |
207
|
|
|
$ci = Interval::create(5, 6, 2, 5, 9, 10, 11); |
208
|
|
|
$this->assertInstanceOfInterval($ci); |
209
|
|
|
$this->assertInterval($ci, 5, 6, 19, 9, 10, 11); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function testInstance() |
213
|
|
|
{ |
214
|
|
|
$interval = new DateInterval('P2Y1M5DT22H33M44S'); |
215
|
|
|
$ci = Interval::instance($interval); |
216
|
|
|
$this->assertInstanceOfInterval($ci); |
217
|
|
|
$this->assertInterval($ci, 2, 1, 5, 22, 33, 44); |
218
|
|
|
$this->assertTrue($ci->days === false || $ci->days === Interval::PHP_DAYS_FALSE); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testInstanceWithNegativeInterval() |
222
|
|
|
{ |
223
|
|
|
$di = new DateInterval('P2Y1M5DT22H33M44S'); |
224
|
|
|
$di->invert = 1; |
225
|
|
|
$ci = Interval::instance($di); |
226
|
|
|
$this->assertInstanceOfInterval($ci); |
227
|
|
|
$this->assertInterval($ci, 2, 1, 5, 22, 33, 44); |
228
|
|
|
$this->assertTrue($ci->days === false || $ci->days === Interval::PHP_DAYS_FALSE); |
229
|
|
|
$this->assertSame(1, $ci->invert); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @expectedException \InvalidArgumentException |
234
|
|
|
*/ |
235
|
|
|
public function testInstanceWithDaysThrowsException() |
236
|
|
|
{ |
237
|
|
|
Interval::instance(Date::now()->diff(Date::now()->addWeeks(3))); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.