-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeOfDayTest.php
More file actions
477 lines (369 loc) · 15.5 KB
/
TimeOfDayTest.php
File metadata and controls
477 lines (369 loc) · 15.5 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
declare(strict_types=1);
namespace Test\TinyBlocks\Time;
use PHPUnit\Framework\TestCase;
use TinyBlocks\Time\Instant;
use TinyBlocks\Time\Internal\Exceptions\InvalidTimeOfDay;
use TinyBlocks\Time\TimeOfDay;
final class TimeOfDayTest extends TestCase
{
public function testTimeOfDayFromCreatesValidTimeOfDay(): void
{
/** @Given valid hour and minute */
$time = TimeOfDay::from(hour: 8, minute: 30);
/** @Then the components should match */
self::assertSame(8, $time->hour);
self::assertSame(30, $time->minute);
}
public function testTimeOfDayFromWithZeros(): void
{
/** @Given hour 0 and minute 0 */
$time = TimeOfDay::from(hour: 0, minute: 0);
/** @Then it should be midnight */
self::assertSame(0, $time->hour);
self::assertSame(0, $time->minute);
}
public function testTimeOfDayFromWithMaxValues(): void
{
/** @Given maximum valid hour and minute */
$time = TimeOfDay::from(hour: 23, minute: 59);
/** @Then the components should match */
self::assertSame(23, $time->hour);
self::assertSame(59, $time->minute);
}
public function testTimeOfDayWhenHourIsNegative(): void
{
/** @Then an exception indicating that hour is out of range should be thrown */
$this->expectException(InvalidTimeOfDay::class);
/** @When creating with negative hour */
TimeOfDay::from(hour: -1, minute: 0);
}
public function testTimeOfDayWhenHourExceeds23(): void
{
/** @Then an exception indicating that hour is out of range should be thrown */
$this->expectException(InvalidTimeOfDay::class);
/** @When creating with hour 24 */
TimeOfDay::from(hour: 24, minute: 0);
}
public function testTimeOfDayWhenMinuteIsNegative(): void
{
/** @Then an exception indicating that minute is out of range should be thrown */
$this->expectException(InvalidTimeOfDay::class);
/** @When creating with negative minute */
TimeOfDay::from(hour: 10, minute: -1);
}
public function testTimeOfDayWhenMinuteExceeds59(): void
{
/** @Then an exception indicating that minute is out of range should be thrown */
$this->expectException(InvalidTimeOfDay::class);
/** @When creating with minute 60 */
TimeOfDay::from(hour: 10, minute: 60);
}
public function testTimeOfDayMidnight(): void
{
/** @Given midnight */
$time = TimeOfDay::midnight();
/** @Then it should be 00:00 */
self::assertSame(0, $time->hour);
self::assertSame(0, $time->minute);
self::assertSame('00:00', $time->toString());
}
public function testTimeOfDayNoon(): void
{
/** @Given noon */
$time = TimeOfDay::noon();
/** @Then it should be 12:00 */
self::assertSame(12, $time->hour);
self::assertSame(0, $time->minute);
self::assertSame('12:00', $time->toString());
}
public function testTimeOfDayFromInstant(): void
{
/** @Given an Instant at 14:30 UTC */
$instant = Instant::fromString(value: '2026-02-17T14:30:00+00:00');
/** @When extracting the time of day */
$time = TimeOfDay::fromInstant(instant: $instant);
/** @Then the components should match */
self::assertSame(14, $time->hour);
self::assertSame(30, $time->minute);
}
public function testTimeOfDayFromInstantAtMidnight(): void
{
/** @Given an Instant at midnight */
$instant = Instant::fromString(value: '2026-02-17T00:00:00+00:00');
/** @When extracting the time of day */
$time = TimeOfDay::fromInstant(instant: $instant);
/** @Then it should be midnight */
self::assertSame(0, $time->hour);
self::assertSame(0, $time->minute);
}
public function testTimeOfDayFromInstantAtEndOfDay(): void
{
/** @Given an Instant at 23:59 */
$instant = Instant::fromString(value: '2026-02-17T23:59:00+00:00');
/** @When extracting the time of day */
$time = TimeOfDay::fromInstant(instant: $instant);
/** @Then it should be 23:59 */
self::assertSame(23, $time->hour);
self::assertSame(59, $time->minute);
}
public function testTimeOfDayFromStringValid(): void
{
/** @Given a valid time string */
$time = TimeOfDay::fromString(value: '08:30');
/** @Then the components should match */
self::assertSame(8, $time->hour);
self::assertSame(30, $time->minute);
}
public function testTimeOfDayFromStringMidnight(): void
{
/** @Given midnight as string */
$time = TimeOfDay::fromString(value: '00:00');
/** @Then it should be midnight */
self::assertSame(0, $time->hour);
self::assertSame(0, $time->minute);
}
public function testTimeOfDayFromStringEndOfDay(): void
{
/** @Given end of day as string */
$time = TimeOfDay::fromString(value: '23:59');
/** @Then it should be 23:59 */
self::assertSame(23, $time->hour);
self::assertSame(59, $time->minute);
}
public function testTimeOfDayFromStringWhenInvalidFormat(): void
{
/** @Then an exception indicating that the format is invalid should be thrown */
$this->expectException(InvalidTimeOfDay::class);
/** @When parsing an invalid string */
TimeOfDay::fromString(value: '8:30');
}
public function testTimeOfDayFromStringWhenEmpty(): void
{
/** @Then an exception indicating that the format is invalid should be thrown */
$this->expectException(InvalidTimeOfDay::class);
/** @When parsing an empty string */
TimeOfDay::fromString(value: '');
}
public function testTimeOfDayFromStringWhenHasSeconds(): void
{
/** @Given a time string with seconds */
$time = TimeOfDay::fromString(value: '08:30:00');
/** @Then the seconds should be discarded and the components should match */
self::assertSame(8, $time->hour);
self::assertSame(30, $time->minute);
self::assertSame('08:30', $time->toString());
}
public function testTimeOfDayFromStringWhenHourOutOfRange(): void
{
/** @Then an exception indicating that hour is out of range should be thrown */
$this->expectException(InvalidTimeOfDay::class);
/** @When parsing a string with hour 25 */
TimeOfDay::fromString(value: '25:00');
}
public function testTimeOfDayFromStringWhenMinuteOutOfRange(): void
{
/** @Then an exception indicating that minute is out of range should be thrown */
$this->expectException(InvalidTimeOfDay::class);
/** @When parsing a string with minute 60 */
TimeOfDay::fromString(value: '10:60');
}
public function testTimeOfDayToMinutesSinceMidnightAtMidnight(): void
{
/** @Given midnight */
$time = TimeOfDay::midnight();
/** @Then minutes since midnight should be 0 */
self::assertSame(0, $time->toMinutesSinceMidnight());
}
public function testTimeOfDayToMinutesSinceMidnightAtNoon(): void
{
/** @Given noon */
$time = TimeOfDay::noon();
/** @Then minutes since midnight should be 720 */
self::assertSame(720, $time->toMinutesSinceMidnight());
}
public function testTimeOfDayToMinutesSinceMidnightAt0830(): void
{
/** @Given 08:30 */
$time = TimeOfDay::from(hour: 8, minute: 30);
/** @Then minutes since midnight should be 510 */
self::assertSame(510, $time->toMinutesSinceMidnight());
}
public function testTimeOfDayToMinutesSinceMidnightAtEndOfDay(): void
{
/** @Given 23:59 */
$time = TimeOfDay::from(hour: 23, minute: 59);
/** @Then minutes since midnight should be 1439 */
self::assertSame(1439, $time->toMinutesSinceMidnight());
}
public function testTimeOfDayToDuration(): void
{
/** @Given 08:30 */
$time = TimeOfDay::from(hour: 8, minute: 30);
/** @When converting to Duration */
$duration = $time->toDuration();
/** @Then the duration should be 510 minutes in seconds */
self::assertSame(510, $duration->toMinutes());
self::assertSame(30600, $duration->toSeconds());
}
public function testTimeOfDayToDurationAtMidnight(): void
{
/** @Given midnight */
$time = TimeOfDay::midnight();
/** @When converting to Duration */
$duration = $time->toDuration();
/** @Then the duration should be zero */
self::assertTrue($duration->isZero());
}
public function testTimeOfDayIsBeforeReturnsTrueWhenEarlier(): void
{
/** @Given an earlier time */
$earlier = TimeOfDay::from(hour: 8, minute: 0);
/** @And a later time */
$later = TimeOfDay::from(hour: 14, minute: 30);
/** @Then the earlier should be before the later */
self::assertTrue($earlier->isBefore(other: $later));
}
public function testTimeOfDayIsBeforeReturnsFalseWhenLater(): void
{
/** @Given a later time */
$later = TimeOfDay::from(hour: 14, minute: 30);
/** @And an earlier time */
$earlier = TimeOfDay::from(hour: 8, minute: 0);
/** @Then the later should not be before the earlier */
self::assertFalse($later->isBefore(other: $earlier));
}
public function testTimeOfDayIsBeforeReturnsFalseWhenEqual(): void
{
/** @Given a time */
$time = TimeOfDay::from(hour: 10, minute: 0);
/** @And the same time */
$same = TimeOfDay::from(hour: 10, minute: 0);
/** @Then isBefore should return false */
self::assertFalse($time->isBefore(other: $same));
}
public function testTimeOfDayIsAfterReturnsTrueWhenLater(): void
{
/** @Given a later time */
$later = TimeOfDay::from(hour: 18, minute: 0);
/** @And an earlier time */
$earlier = TimeOfDay::from(hour: 8, minute: 0);
/** @Then the later should be after the earlier */
self::assertTrue($later->isAfter(other: $earlier));
}
public function testTimeOfDayIsAfterReturnsFalseWhenEqual(): void
{
/** @Given a time */
$time = TimeOfDay::from(hour: 10, minute: 0);
/** @And the same time */
$same = TimeOfDay::from(hour: 10, minute: 0);
/** @Then isAfter should return false */
self::assertFalse($time->isAfter(other: $same));
}
public function testTimeOfDayIsBeforeOrEqualReturnsTrueWhenEqual(): void
{
/** @Given a time */
$time = TimeOfDay::from(hour: 10, minute: 0);
/** @And the same time */
$same = TimeOfDay::from(hour: 10, minute: 0);
/** @Then isBeforeOrEqual should return true */
self::assertTrue($time->isBeforeOrEqual(other: $same));
}
public function testTimeOfDayIsAfterOrEqualReturnsTrueWhenEqual(): void
{
/** @Given a time */
$time = TimeOfDay::from(hour: 10, minute: 0);
/** @And the same time */
$same = TimeOfDay::from(hour: 10, minute: 0);
/** @Then isAfterOrEqual should return true */
self::assertTrue($time->isAfterOrEqual(other: $same));
}
public function testTimeOfDayIsBeforeAndIsAfterAreMutuallyExclusive(): void
{
/** @Given an earlier time */
$earlier = TimeOfDay::from(hour: 8, minute: 0);
/** @And a later time */
$later = TimeOfDay::from(hour: 18, minute: 0);
/** @Then isBefore and isAfter should be mutually exclusive */
self::assertTrue($earlier->isBefore(other: $later));
self::assertFalse($earlier->isAfter(other: $later));
self::assertTrue($later->isAfter(other: $earlier));
self::assertFalse($later->isBefore(other: $earlier));
}
public function testTimeOfDayDurationUntilReturnsCorrectDuration(): void
{
/** @Given a start time at 08:00 */
$from = TimeOfDay::from(hour: 8, minute: 0);
/** @And an end time at 12:30 */
$to = TimeOfDay::from(hour: 12, minute: 30);
/** @When calculating the duration */
$duration = $from->durationUntil(other: $to);
/** @Then the duration should be 270 minutes */
self::assertSame(270, $duration->toMinutes());
}
public function testTimeOfDayDurationUntilWhenEndIsBeforeStart(): void
{
/** @Given a start time at 14:00 */
$from = TimeOfDay::from(hour: 14, minute: 0);
/** @And an end time at 08:00 */
$to = TimeOfDay::from(hour: 8, minute: 0);
/** @Then an exception indicating that end must be after start should be thrown */
$this->expectException(InvalidTimeOfDay::class);
/** @When calculating the duration */
$from->durationUntil(other: $to);
}
public function testTimeOfDayDurationUntilWhenEqual(): void
{
/** @Given a time at 10:00 */
$time = TimeOfDay::from(hour: 10, minute: 0);
/** @And the same time at 10:00 */
$same = TimeOfDay::from(hour: 10, minute: 0);
/** @Then an exception indicating that end must be after start should be thrown */
$this->expectException(InvalidTimeOfDay::class);
/** @When calculating the duration */
$time->durationUntil(other: $same);
}
public function testTimeOfDayToStringFormatsCorrectly(): void
{
/** @Then various times should format correctly */
self::assertSame('00:00', TimeOfDay::from(hour: 0, minute: 0)->toString());
self::assertSame('08:05', TimeOfDay::from(hour: 8, minute: 5)->toString());
self::assertSame('14:30', TimeOfDay::from(hour: 14, minute: 30)->toString());
self::assertSame('23:59', TimeOfDay::from(hour: 23, minute: 59)->toString());
}
public function testTimeOfDayFromStringAndToStringRoundTrip(): void
{
/** @Given a time string */
$original = '14:30';
/** @When parsing and formatting back */
$result = TimeOfDay::fromString(value: $original)->toString();
/** @Then the result should match the original */
self::assertSame($original, $result);
}
public function testTimeOfDayFromInstantAndFromProduceSameResult(): void
{
/** @Given an Instant at 14:30 UTC */
$instant = Instant::fromString(value: '2026-02-17T14:30:00+00:00');
/** @When creating from both methods */
$fromInstant = TimeOfDay::fromInstant(instant: $instant);
/** @And creating from hour and minute directly */
$fromFactory = TimeOfDay::from(hour: 14, minute: 30);
/** @Then both should produce the same result */
self::assertSame($fromInstant->hour, $fromFactory->hour);
self::assertSame($fromInstant->minute, $fromFactory->minute);
}
public function testTimeOfDayFromStringWhenPrefixBeforeValidPattern(): void
{
/** @Then an exception indicating that the format is invalid should be thrown */
$this->expectException(InvalidTimeOfDay::class);
/** @When parsing a string with a prefix before a valid HH:MM pattern */
TimeOfDay::fromString(value: 'abc08:30');
}
public function testTimeOfDayFromStringWhenSuffixAfterValidPattern(): void
{
/** @Then an exception indicating that the format is invalid should be thrown */
$this->expectException(InvalidTimeOfDay::class);
/** @When parsing a string with a suffix after a valid HH:MM pattern */
TimeOfDay::fromString(value: '08:30xyz');
}
}