Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
36 / 36 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | use Phexium\Domain\Id\TimestampId; |
| 11 | use Phexium\Plugin\IdGenerator\Adapter\TimestampIdGenerator; |
| 12 | |
| 13 | test('Generator returns a TimestampId object from an int', function (): void { |
| 14 | $generator = new TimestampIdGenerator(); |
| 15 | |
| 16 | $result = $generator->from(123); |
| 17 | |
| 18 | expect($result)->toBeInstanceOf(TimestampId::class) |
| 19 | ->and($result->getValue())->toBe(123) |
| 20 | ; |
| 21 | }); |
| 22 | |
| 23 | test('Generator returns a TimestampId object from a string', function (): void { |
| 24 | $generator = new TimestampIdGenerator(); |
| 25 | |
| 26 | $result = $generator->from('123'); |
| 27 | |
| 28 | expect($result)->toBeInstanceOf(TimestampId::class) |
| 29 | ->and($result->getValue())->toBe(123) |
| 30 | ; |
| 31 | }); |
| 32 | |
| 33 | test('Generator returns a TimestampId object from current time', function (): void { |
| 34 | $generator = new TimestampIdGenerator(); |
| 35 | |
| 36 | $before = (int) (microtime(true) * 1000000); |
| 37 | $result = $generator->generate(); |
| 38 | $after = (int) (microtime(true) * 1000000); |
| 39 | |
| 40 | expect($result)->toBeInstanceOf(TimestampId::class) |
| 41 | ->and($result->getValue())->toBeGreaterThanOrEqual($before) |
| 42 | ->toBeLessThanOrEqual($after + 999) |
| 43 | ; |
| 44 | }); |
| 45 | |
| 46 | test('TimestampId equality', function (): void { |
| 47 | $id1 = new TimestampId(123); |
| 48 | expect($id1->equals($id1))->toBeTrue(); |
| 49 | |
| 50 | $id2 = TimestampId::from(234); |
| 51 | expect($id1->equals($id2))->toBeFalse(); |
| 52 | }); |
| 53 | |
| 54 | test('TimestampId can be converted to string', function (): void { |
| 55 | $id = new TimestampId(123); |
| 56 | |
| 57 | expect((string) $id)->toBeString() |
| 58 | ->toBe((string) $id->getValue()) |
| 59 | ; |
| 60 | }); |