-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstantTest.php
More file actions
455 lines (378 loc) · 18.6 KB
/
InstantTest.php
File metadata and controls
455 lines (378 loc) · 18.6 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
<?php
declare(strict_types=1);
namespace Test\TinyBlocks\Time;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use TinyBlocks\Time\Instant;
use TinyBlocks\Time\Internal\Exceptions\InvalidInstant;
final class InstantTest extends TestCase
{
public function testInstantNowIsInUtc(): void
{
/** @Given the current moment */
/** @When creating an Instant from now */
$instant = Instant::now();
/** @Then the DateTimeImmutable timezone should be UTC */
self::assertSame('UTC', $instant->toDateTimeImmutable()->getTimezone()->getName());
}
public function testInstantNowIsCloseToCurrentTime(): void
{
/** @Given the current Unix timestamp before creating the Instant */
$before = time();
/** @When creating an Instant from now */
$instant = Instant::now();
/** @And capturing the Unix timestamp after */
$after = time();
/** @Then the Instant's Unix seconds should be within the before/after window */
self::assertGreaterThanOrEqual($before, $instant->toUnixSeconds());
self::assertLessThanOrEqual($after, $instant->toUnixSeconds());
}
public function testInstantNowPreservesMicrosecondPrecision(): void
{
/** @Given an Instant created from now */
$instant = Instant::now();
/** @When formatting the underlying DateTimeImmutable with microseconds */
$microseconds = (int)$instant->toDateTimeImmutable()->format('u');
/** @Then the microsecond component should be representable (six digits available) */
self::assertGreaterThanOrEqual(0, $microseconds);
self::assertLessThanOrEqual(999999, $microseconds);
}
public function testInstantNowIso8601HasNoFractionalSeconds(): void
{
/** @Given an Instant created from now */
$instant = Instant::now();
/** @When formatting as ISO 8601 */
$iso = $instant->toIso8601();
/** @Then the output should match YYYY-MM-DDTHH:MM:SS+00:00 without fractions */
self::assertMatchesRegularExpression('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+00:00$/', $iso);
}
public function testInstantNowProducesDistinctInstances(): void
{
/** @Given two Instants created from now in sequence */
$first = Instant::now();
$second = Instant::now();
/** @Then both should be valid Instants in UTC */
self::assertSame('UTC', $first->toDateTimeImmutable()->getTimezone()->getName());
self::assertSame('UTC', $second->toDateTimeImmutable()->getTimezone()->getName());
/** @And the second should not be before the first */
self::assertGreaterThanOrEqual(
$first->toDateTimeImmutable()->format('U.u'),
$second->toDateTimeImmutable()->format('U.u')
);
}
#[DataProvider('validStringsDataProvider')]
public function testInstantFromString(
string $value,
string $expectedIso8601,
int $expectedUnixSeconds
): void {
/** @Given a valid date-time string with offset */
/** @When creating an Instant from the string */
$instant = Instant::fromString(value: $value);
/** @Then the ISO 8601 representation should match the expected UTC value */
self::assertSame($expectedIso8601, $instant->toIso8601());
/** @And the Unix seconds should match the expected timestamp */
self::assertSame($expectedUnixSeconds, $instant->toUnixSeconds());
}
#[DataProvider('unixSecondsDataProvider')]
public function testInstantFromUnixSeconds(
int $seconds,
string $expectedIso8601
): void {
/** @Given a valid Unix timestamp in seconds */
/** @When creating an Instant from Unix seconds */
$instant = Instant::fromUnixSeconds(seconds: $seconds);
/** @Then the ISO 8601 representation should match the expected UTC value */
self::assertSame($expectedIso8601, $instant->toIso8601());
/** @And the Unix seconds should round-trip correctly */
self::assertSame($seconds, $instant->toUnixSeconds());
}
public function testInstantToDateTimeImmutableReturnsUtc(): void
{
/** @Given an Instant created from a string with a non-UTC offset */
$instant = Instant::fromString(value: '2026-02-17T15:30:00+05:00');
/** @When converting to DateTimeImmutable */
$dateTime = $instant->toDateTimeImmutable();
/** @Then the timezone should be UTC */
self::assertSame('UTC', $dateTime->getTimezone()->getName());
/** @And the date-time should reflect the UTC-converted value */
self::assertSame('2026-02-17T10:30:00', $dateTime->format('Y-m-d\TH:i:s'));
}
public function testInstantFromStringNormalizesToUtc(): void
{
/** @Given a date-time string with a positive offset */
$instant = Instant::fromString(value: '2026-02-17T18:00:00+03:00');
/** @Then the ISO 8601 output should be normalized to UTC */
self::assertSame('2026-02-17T15:00:00+00:00', $instant->toIso8601());
/** @And the DateTimeImmutable timezone should be UTC */
self::assertSame('UTC', $instant->toDateTimeImmutable()->getTimezone()->getName());
}
public function testInstantFromStringWithNegativeOffset(): void
{
/** @Given a date-time string with a negative offset */
$instant = Instant::fromString(value: '2026-02-17T07:00:00-05:00');
/** @Then the ISO 8601 output should be normalized to UTC */
self::assertSame('2026-02-17T12:00:00+00:00', $instant->toIso8601());
}
public function testInstantFromStringWithUtcOffset(): void
{
/** @Given a date-time string already in UTC */
$instant = Instant::fromString(value: '2026-02-17T10:30:00+00:00');
/** @Then the ISO 8601 output should remain unchanged */
self::assertSame('2026-02-17T10:30:00+00:00', $instant->toIso8601());
}
public function testInstantFromUnixSecondsEpoch(): void
{
/** @Given Unix timestamp zero (epoch) */
$instant = Instant::fromUnixSeconds(seconds: 0);
/** @Then the ISO 8601 output should be the Unix epoch in UTC */
self::assertSame('1970-01-01T00:00:00+00:00', $instant->toIso8601());
/** @And the Unix seconds should be zero */
self::assertSame(0, $instant->toUnixSeconds());
}
public function testInstantFromUnixSecondsNegativeValue(): void
{
/** @Given a negative Unix timestamp representing a date before the epoch */
$instant = Instant::fromUnixSeconds(seconds: -86400);
/** @Then the ISO 8601 output should be one day before the epoch */
self::assertSame('1969-12-31T00:00:00+00:00', $instant->toIso8601());
/** @And the Unix seconds should round-trip correctly */
self::assertSame(-86400, $instant->toUnixSeconds());
}
public function testInstantFromUnixSecondsToDateTimeImmutableIsUtc(): void
{
/** @Given an Instant created from Unix seconds */
$instant = Instant::fromUnixSeconds(seconds: 1771324200);
/** @When converting to DateTimeImmutable */
$dateTime = $instant->toDateTimeImmutable();
/** @Then the timezone should be UTC */
self::assertSame('UTC', $dateTime->getTimezone()->getName());
}
public function testInstantFromStringAndFromUnixSecondsProduceSameResult(): void
{
/** @Given an Instant created from a string */
$fromString = Instant::fromString(value: '2026-02-17T00:00:00+00:00');
/** @And an Instant created from the equivalent Unix seconds */
$fromUnix = Instant::fromUnixSeconds(seconds: $fromString->toUnixSeconds());
/** @Then both should produce the same ISO 8601 output */
self::assertSame($fromString->toIso8601(), $fromUnix->toIso8601());
/** @And both should produce the same Unix seconds */
self::assertSame($fromString->toUnixSeconds(), $fromUnix->toUnixSeconds());
}
public function testInstantDateTimeImmutablePreservesMicroseconds(): void
{
/** @Given an Instant created from a valid string */
$instant = Instant::fromString(value: '2026-02-17T10:30:00+00:00');
/** @When accessing the underlying DateTimeImmutable */
$dateTime = $instant->toDateTimeImmutable();
/** @Then the format should support microsecond precision */
$formatted = $dateTime->format('Y-m-d\TH:i:s.u');
self::assertMatchesRegularExpression('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}$/', $formatted);
}
public function testInstantIso8601OutputNeverContainsFractionalSeconds(): void
{
/** @Given an Instant created from any valid input */
$instant = Instant::fromString(value: '2026-06-15T23:59:59+00:00');
/** @When formatting as ISO 8601 */
$iso = $instant->toIso8601();
/** @Then the output should match YYYY-MM-DDTHH:MM:SS+00:00 without fractions */
self::assertMatchesRegularExpression('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+00:00$/', $iso);
}
public function testInstantFromStringWithDayBoundaryOffset(): void
{
/** @Given a date-time string where the UTC conversion crosses a day boundary */
$instant = Instant::fromString(value: '2026-02-18T01:00:00+03:00');
/** @Then the ISO 8601 output should reflect the previous day in UTC */
self::assertSame('2026-02-17T22:00:00+00:00', $instant->toIso8601());
}
public function testInstantFromStringWithMaxPositiveOffset(): void
{
/** @Given a date-time string with the maximum positive UTC offset (+14:00) */
$instant = Instant::fromString(value: '2026-02-17T14:00:00+14:00');
/** @Then the ISO 8601 output should be normalized to UTC */
self::assertSame('2026-02-17T00:00:00+00:00', $instant->toIso8601());
}
public function testInstantFromStringWithMaxNegativeOffset(): void
{
/** @Given a date-time string with the maximum negative UTC offset (-12:00) */
$instant = Instant::fromString(value: '2026-02-16T12:00:00-12:00');
/** @Then the ISO 8601 output should be normalized to UTC */
self::assertSame('2026-02-17T00:00:00+00:00', $instant->toIso8601());
}
#[DataProvider('invalidStringsDataProvider')]
public function testInstantWhenInvalidString(string $value): void
{
/** @Given an invalid date-time string */
/** @Then an InvalidInstant exception should be thrown */
$this->expectException(InvalidInstant::class);
$this->expectExceptionMessage(sprintf('The value <%s> could not be decoded into a valid instant.', $value));
/** @When trying to create an Instant from the invalid string */
Instant::fromString(value: $value);
}
#[DataProvider('validDatabaseStringsDataProvider')]
public function testInstantFromDatabaseString(
string $value,
string $expectedIso8601
): void {
/** @Given a valid database date-time string in UTC */
/** @When creating an Instant from the string */
$instant = Instant::fromString(value: $value);
/** @Then the ISO 8601 representation should match the expected UTC value */
self::assertSame($expectedIso8601, $instant->toIso8601());
}
public function testInstantFromDatabaseStringPreservesMicroseconds(): void
{
/** @Given a database date-time string with microsecond precision */
$instant = Instant::fromString(value: '2026-02-17 08:27:21.106011');
/** @When accessing the underlying DateTimeImmutable */
$dateTime = $instant->toDateTimeImmutable();
/** @Then the microseconds should be preserved */
self::assertSame('106011', $dateTime->format('u'));
}
public function testInstantFromDatabaseStringWithoutMicrosecondsHasZeroMicroseconds(): void
{
/** @Given a database date-time string without microseconds */
$instant = Instant::fromString(value: '2026-02-17 08:27:21');
/** @When accessing the underlying DateTimeImmutable */
$dateTime = $instant->toDateTimeImmutable();
/** @Then the microseconds should be zero */
self::assertSame('000000', $dateTime->format('u'));
}
public function testInstantFromDatabaseStringIsInUtc(): void
{
/** @Given a database date-time string */
$instant = Instant::fromString(value: '2026-02-17 08:27:21.106011');
/** @When converting to DateTimeImmutable */
$dateTime = $instant->toDateTimeImmutable();
/** @Then the timezone should be UTC */
self::assertSame('UTC', $dateTime->getTimezone()->getName());
}
public static function validStringsDataProvider(): array
{
return [
'UTC offset' => [
'value' => '2026-02-17T10:30:00+00:00',
'expectedIso8601' => '2026-02-17T10:30:00+00:00',
'expectedUnixSeconds' => 1771324200
],
'Midnight UTC' => [
'value' => '2026-01-01T00:00:00+00:00',
'expectedIso8601' => '2026-01-01T00:00:00+00:00',
'expectedUnixSeconds' => 1767225600
],
'End of day UTC' => [
'value' => '2026-02-17T23:59:59+00:00',
'expectedIso8601' => '2026-02-17T23:59:59+00:00',
'expectedUnixSeconds' => 1771372799
],
'Positive offset +05:30' => [
'value' => '2026-02-17T16:00:00+05:30',
'expectedIso8601' => '2026-02-17T10:30:00+00:00',
'expectedUnixSeconds' => 1771324200
],
'Negative offset -03:00' => [
'value' => '2026-02-17T07:30:00-03:00',
'expectedIso8601' => '2026-02-17T10:30:00+00:00',
'expectedUnixSeconds' => 1771324200
],
'Negative offset -05:00' => [
'value' => '2026-02-17T05:30:00-05:00',
'expectedIso8601' => '2026-02-17T10:30:00+00:00',
'expectedUnixSeconds' => 1771324200
],
'Positive offset +09:00' => [
'value' => '2026-02-17T19:30:00+09:00',
'expectedIso8601' => '2026-02-17T10:30:00+00:00',
'expectedUnixSeconds' => 1771324200
],
'Negative offset -09:30' => [
'value' => '2026-02-17T01:00:00-09:30',
'expectedIso8601' => '2026-02-17T10:30:00+00:00',
'expectedUnixSeconds' => 1771324200
]
];
}
public static function unixSecondsDataProvider(): array
{
return [
'Epoch' => [
'seconds' => 0,
'expectedIso8601' => '1970-01-01T00:00:00+00:00'
],
'Year 2000 midnight' => [
'seconds' => 946684800,
'expectedIso8601' => '2000-01-01T00:00:00+00:00'
],
'One day after epoch' => [
'seconds' => 86400,
'expectedIso8601' => '1970-01-02T00:00:00+00:00'
],
'One day before epoch' => [
'seconds' => -86400,
'expectedIso8601' => '1969-12-31T00:00:00+00:00'
],
'Year 2026 reference' => [
'seconds' => 1771324200,
'expectedIso8601' => '2026-02-17T10:30:00+00:00'
],
'Large future timestamp' => [
'seconds' => 2147483647,
'expectedIso8601' => '2038-01-19T03:14:07+00:00'
]
];
}
public static function invalidStringsDataProvider(): array
{
return [
'Date only' => ['value' => '2026-02-17'],
'Time only' => ['value' => '10:30:00'],
'Plain text' => ['value' => 'not-a-date'],
'Invalid day' => ['value' => '2026-02-30T10:30:00+00:00'],
'Empty string' => ['value' => ''],
'Invalid month' => ['value' => '2026-13-17T10:30:00+00:00'],
'Missing offset' => ['value' => '2026-02-17T10:30:00'],
'Truncated offset' => ['value' => '2026-02-17T10:30:00+00'],
'Slash-separated date' => ['value' => '2026/02/17T10:30:00+00:00'],
'Missing time separator' => ['value' => '2026-02-17 10:30:00+00:00'],
'Z suffix instead offset' => ['value' => '2026-02-17T10:30:00Z'],
'With fractional seconds' => ['value' => '2026-02-17T10:30:00.123456+00:00'],
'Unix timestamp as string' => ['value' => '1771324200'],
'Database format with invalid day' => ['value' => '2026-02-30 08:27:21.106011'],
'Database format with T separator' => ['value' => '2026-02-17T08:27:21.106011'],
'Database format with invalid month' => ['value' => '2026-13-17 08:27:21.106011']
];
}
public static function validDatabaseStringsDataProvider(): array
{
return [
'End of day' => [
'value' => '2026-12-31 23:59:59.999999',
'expectedIso8601' => '2026-12-31T23:59:59+00:00'
],
'Full microseconds' => [
'value' => '2026-02-17 08:27:21.106011',
'expectedIso8601' => '2026-02-17T08:27:21+00:00'
],
'Midnight with zeros' => [
'value' => '2026-01-01 00:00:00.000000',
'expectedIso8601' => '2026-01-01T00:00:00+00:00'
],
'Without microseconds' => [
'value' => '2026-02-17 08:27:21',
'expectedIso8601' => '2026-02-17T08:27:21+00:00'
],
'Three digit fraction' => [
'value' => '2026-02-17 08:27:21.106',
'expectedIso8601' => '2026-02-17T08:27:21+00:00'
],
'Single digit fraction' => [
'value' => '2026-02-17 08:27:21.1',
'expectedIso8601' => '2026-02-17T08:27:21+00:00'
],
'Midnight without microseconds' => [
'value' => '2026-01-01 00:00:00',
'expectedIso8601' => '2026-01-01T00:00:00+00:00'
]
];
}
}