-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathChronosTimeTest.php
More file actions
331 lines (261 loc) · 11.7 KB
/
ChronosTimeTest.php
File metadata and controls
331 lines (261 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Test\TestCase;
use Cake\Chronos\Chronos;
use Cake\Chronos\ChronosTime;
use DateTimeImmutable;
use InvalidArgumentException;
class ChronosTimeTest extends TestCase
{
public function testConstructNow(): void
{
Chronos::setTestNow('2001-01-01 12:13:14.123456');
$t = new ChronosTime();
$this->assertSame('12:13:14.123456', $t->format('H:i:s.u'));
$t = new ChronosTime(null);
$this->assertSame('12:13:14.123456', $t->format('H:i:s.u'));
}
public function testConstructFromString(): void
{
$t = new ChronosTime('12:13');
$this->assertSame('12:13:00.000000', $t->format('H:i:s.u'));
$t = new ChronosTime('0.0.0.0');
$this->assertSame('00:00:00.000000', $t->format('H:i:s.u'));
$t = new ChronosTime('1:01:1.000001');
$this->assertSame('01:01:01.000001', $t->format('H:i:s.u'));
$t = new ChronosTime('23:59.59.999999');
$this->assertSame('23:59:59.999999', $t->format('H:i:s.u'));
$t = new ChronosTime('23:59.59.9999991');
$this->assertSame('23:59:59.999999', $t->format('H:i:s.u'));
$t = new ChronosTime('24:59.59.9999991');
$this->assertSame('00:59:59.999999', $t->format('H:i:s.u'));
}
public function testConstructFromInstance(): void
{
$t = new ChronosTime(new DateTimeImmutable('23:59:59.999999'));
$this->assertSame('23:59:59.999999', $t->format('H:i:s.u'));
$t = new ChronosTime(new Chronos('23:59:59.999999'));
$this->assertSame('23:59:59.999999', $t->format('H:i:s.u'));
$t = new ChronosTime(new ChronosTime(new Chronos('23:59:59.999999')));
$this->assertSame('23:59:59.999999', $t->format('H:i:s.u'));
}
public function testConstructInvalid(): void
{
$this->expectException(InvalidArgumentException::class);
new ChronosTime('tomorrow');
}
public function testConstructIncomplete(): void
{
$this->expectException(InvalidArgumentException::class);
new ChronosTime('23');
}
public function testConstructInvalidHours(): void
{
$this->expectException(InvalidArgumentException::class);
new ChronosTime('25:00:00');
}
public function testConstructInvalidMinutes(): void
{
$this->expectException(InvalidArgumentException::class);
new ChronosTime('23:60:00');
}
public function testConstructInvalidSeconds(): void
{
$this->expectException(InvalidArgumentException::class);
new ChronosTime('23:59:60');
}
public function testParse(): void
{
$t = ChronosTime::parse('23:59:59.999999');
$this->assertSame('23:59:59.999999', $t->format('H:i:s.u'));
Chronos::setTestNow(new Chronos('2001-01-01 12:13:14.123456', 'America/Chicago'));
$t = ChronosTime::parse(null, 'America/New_York');
$this->assertSame('13:13:14.123456', $t->format('H:i:s.u'));
}
public function testNow(): void
{
Chronos::setTestNow(new Chronos('2001-01-01 12:13:14.123456', 'America/Chicago'));
$t = ChronosTime::now();
$this->assertSame('12:13:14.123456', $t->format('H:i:s.u'));
$t = ChronosTime::now('America/New_York');
$this->assertSame('13:13:14.123456', $t->format('H:i:s.u'));
}
public function testMidnight(): void
{
$t = ChronosTime::midnight();
$this->assertSame('00:00:00.000000', $t->format('H:i:s.u'));
}
public function testNoon(): void
{
$t = ChronosTime::noon();
$this->assertSame('12:00:00.000000', $t->format('H:i:s.u'));
}
public function testEndOfDay(): void
{
$t = ChronosTime::endOfDay();
$this->assertSame('23:59:59.000000', $t->format('H:i:s.u'));
$t = ChronosTime::endOfDay(microseconds: true);
$this->assertSame('23:59:59.999999', $t->format('H:i:s.u'));
}
public function testSetters(): void
{
$t = ChronosTime::midnight()->setHours(24);
$this->assertSame('00:00:00.000000', $t->format('H:i:s.u'));
$t = ChronosTime::midnight()->setHours(-1);
$this->assertSame('23:00:00.000000', $t->format('H:i:s.u'));
$t = ChronosTime::midnight()->setMinutes(60);
$this->assertSame('01:00:00.000000', $t->format('H:i:s.u'));
$t = ChronosTime::midnight()->setMinutes(-1);
$this->assertSame('23:59:00.000000', $t->format('H:i:s.u'));
$t = ChronosTime::midnight()->setSeconds(60);
$this->assertSame('00:01:00.000000', $t->format('H:i:s.u'));
$t = ChronosTime::midnight()->setSeconds(-1);
$this->assertSame('23:59:59.000000', $t->format('H:i:s.u'));
$t = ChronosTime::midnight()->setMicroseconds(1_000_000);
$this->assertSame('00:00:01.000000', $t->format('H:i:s.u'));
$t = ChronosTime::midnight()->setMicroseconds(-1);
$this->assertSame('23:59:59.999999', $t->format('H:i:s.u'));
$t = ChronosTime::midnight()->setHours(25)->setMinutes(120)->setSeconds(120)->setMicroseconds(2_000_001);
$this->assertSame('03:02:02.000001', $t->format('H:i:s.u'));
}
public function testGetters(): void
{
$t = ChronosTime::midnight()->setTime(-1, -1, -1, -1);
$this->assertSame(22, $t->getHours());
$this->assertSame(58, $t->getMinutes());
$this->assertSame(58, $t->getSeconds());
$this->assertSame(999_999, $t->getMicroseconds());
}
public function testSetTime(): void
{
$t = new ChronosTime();
$new = $t->setTime();
$this->assertNotSame($new, $t);
$this->assertSame('00:00:00.000000', $new->format('H:i:s.u'));
$t = ChronosTime::midnight()->setTime(24, 120, 120, 1_000_000);
$this->assertSame('02:02:01.000000', $t->format('H:i:s.u'));
$t = ChronosTime::midnight()->setTime(-1, -1, -1, -1);
$this->assertSame('22:58:58.999999', $t->format('H:i:s.u'));
$t = ChronosTime::midnight()->setTime(-1, 120, -1, 1_000_001);
$this->assertSame('01:00:00.000001', $t->format('H:i:s.u'));
}
public function testStartOfHour(): void
{
$t = ChronosTime::parse('12:30:45.123456');
$start = $t->startOfHour();
$this->assertNotSame($t, $start);
$this->assertSame('12:00:00.000000', $start->format('H:i:s.u'));
$t = ChronosTime::parse('00:59:59.999999');
$this->assertSame('00:00:00.000000', $t->startOfHour()->format('H:i:s.u'));
$t = ChronosTime::parse('23:01:01');
$this->assertSame('23:00:00.000000', $t->startOfHour()->format('H:i:s.u'));
}
public function testEndOfHour(): void
{
$t = ChronosTime::parse('12:30:45.123456');
$end = $t->endOfHour();
$this->assertNotSame($t, $end);
$this->assertSame('12:59:59.999999', $end->format('H:i:s.u'));
$t = ChronosTime::parse('00:00:00');
$this->assertSame('00:59:59.999999', $t->endOfHour()->format('H:i:s.u'));
$t = ChronosTime::parse('23:30:00');
$this->assertSame('23:59:59.999999', $t->endOfHour()->format('H:i:s.u'));
}
public function testFormat(): void
{
$t = new ChronosTime('23:59:59.999999');
$this->assertSame('23:59:59.999999', $t->format('H:i:s.u'));
}
public function testComparisons(): void
{
$t1 = ChronosTime::parse('00:00:00');
$t2 = ChronosTime::parse('00:00:00');
$this->assertTrue($t1->equals($t2));
$this->assertTrue($t1->greaterThanOrEquals($t2));
$this->assertTrue($t1->lessThanOrEquals($t2));
$this->assertFalse($t1->greaterThan($t2));
$this->assertFalse($t1->lessThan($t2));
$t1 = ChronosTime::parse('00:00:00');
$t2 = ChronosTime::parse('00:00:00')->setHours(24);
$this->assertTrue($t1->equals($t2));
$this->assertTrue($t1->greaterThanOrEquals($t2));
$this->assertTrue($t1->lessThanOrEquals($t2));
$this->assertFalse($t1->greaterThan($t2));
$this->assertFalse($t1->lessThan($t2));
$t1 = ChronosTime::parse('00:00:00');
$t2 = ChronosTime::parse('00:00:00.000001');
$this->assertTrue($t1->lessThan($t2));
$this->assertTrue($t1->lessThanOrEquals($t2));
$this->assertFalse($t1->equals($t2));
$this->assertFalse($t1->greaterThan($t2));
$this->assertFalse($t1->greaterThanOrEquals($t2));
$t1 = ChronosTime::parse('00:00:00.000001');
$t2 = ChronosTime::parse('00:00:00');
$this->assertTrue($t1->greaterThan($t2));
$this->assertTrue($t1->greaterThanOrEquals($t2));
$this->assertFalse($t1->equals($t2));
$this->assertFalse($t1->lessThan($t2));
$this->assertFalse($t1->lessThanOrEquals($t2));
$t1 = ChronosTime::parse('00:00:00.000001');
$t2 = ChronosTime::parse('00:00:00.000002');
$t3 = ChronosTime::parse('00:00:00.000003');
$this->assertTrue($t2->between($t1, $t3));
$t1 = ChronosTime::parse('00:00:00.000001');
$t2 = ChronosTime::parse('00:00:00.000002');
$t3 = ChronosTime::parse('00:00:00.000003');
$this->assertTrue($t2->between($t1, $t3, false));
$t1 = ChronosTime::parse('00:00:00.000001');
$t2 = ChronosTime::parse('00:00:00.000002');
$t3 = ChronosTime::parse('00:00:00.000003');
$this->assertTrue($t1->between($t1, $t3, true));
$t1 = ChronosTime::parse('00:00:00.000001');
$t2 = ChronosTime::parse('00:00:00.000002');
$t3 = ChronosTime::parse('00:00:00.000003');
$this->assertFalse($t1->between($t1, $t3, false));
$t1 = ChronosTime::parse('00:00:00.000001');
$t2 = ChronosTime::parse('00:00:00.000002');
$t3 = ChronosTime::parse('00:00:00.000003');
$this->assertTrue($t2->between($t3, $t1, false));
$t1 = ChronosTime::parse('00:00:00.000001');
$t2 = ChronosTime::parse('00:00:00.000002');
$t3 = ChronosTime::parse('00:00:00.000003');
$this->assertFalse($t1->between($t2, $t3));
$t1 = ChronosTime::parse('00:00:00.000001');
$t2 = ChronosTime::parse('00:00:00.000002');
$t3 = ChronosTime::parse('00:00:00.000003');
$this->assertFalse($t3->between($t1, $t2));
}
public function testToDateTimeImmutable(): void
{
$time = ChronosTime::parse('23:59:59.999999');
$native = $time->toDateTimeImmutable();
$this->assertSame('23:59:59.999999', $native->format('H:i:s.u'));
$native = $time->toNative();
$this->assertSame('23:59:59.999999', $native->format('H:i:s.u'));
$native = $time->toDateTimeImmutable('Asia/Tokyo');
$this->assertSame('23:59:59.999999', $native->format('H:i:s.u'));
$this->assertSame('Asia/Tokyo', $native->getTimezone()->getName());
$native = $time->toNative('Asia/Tokyo');
$this->assertSame('23:59:59.999999', $native->format('H:i:s.u'));
$this->assertSame('Asia/Tokyo', $native->getTimezone()->getName());
}
public function testToString(): void
{
$t = new ChronosTime('12:13:14.123456');
$this->assertSame('12:13:14', (string)$t);
ChronosTime::setToStringFormat('H:i:s.u');
$this->assertSame('12:13:14.123456', (string)$t);
ChronosTime::resetToStringFormat();
$this->assertSame('12:13:14', (string)$t);
}
}