Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
41 / 41 |
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 | pest()->group('integration'); |
| 11 | |
| 12 | use Phexium\Domain\Id\TimestampId; |
| 13 | use Phexium\Plugin\IdGenerator\Adapter\TimestampIdGenerator; |
| 14 | |
| 15 | describe('Generation', function (): void { |
| 16 | it('creates a TimestampId from an int', function (): void { |
| 17 | $generator = new TimestampIdGenerator(); |
| 18 | |
| 19 | $result = $generator->from(123); |
| 20 | |
| 21 | expect($result)->toBeInstanceOf(TimestampId::class) |
| 22 | ->and($result->getValue())->toBe(123) |
| 23 | ; |
| 24 | }); |
| 25 | |
| 26 | it('creates a TimestampId from a string', function (): void { |
| 27 | $generator = new TimestampIdGenerator(); |
| 28 | |
| 29 | $result = $generator->from('123'); |
| 30 | |
| 31 | expect($result)->toBeInstanceOf(TimestampId::class) |
| 32 | ->and($result->getValue())->toBe(123) |
| 33 | ; |
| 34 | }); |
| 35 | |
| 36 | it('generates a TimestampId from current time', function (): void { |
| 37 | $generator = new TimestampIdGenerator(); |
| 38 | |
| 39 | $before = (int) (microtime(true) * 1000000); |
| 40 | $result = $generator->generate(); |
| 41 | $after = (int) (microtime(true) * 1000000); |
| 42 | |
| 43 | expect($result)->toBeInstanceOf(TimestampId::class) |
| 44 | ->and($result->getValue())->toBeGreaterThanOrEqual($before) |
| 45 | ->toBeLessThanOrEqual($after + 999) |
| 46 | ; |
| 47 | }); |
| 48 | |
| 49 | it('converts to string', function (): void { |
| 50 | $id = new TimestampId(123); |
| 51 | |
| 52 | expect((string) $id)->toBeString() |
| 53 | ->toBe((string) $id->getValue()) |
| 54 | ; |
| 55 | }); |
| 56 | }); |
| 57 | |
| 58 | describe('Uniqueness', function (): void { |
| 59 | it('compares by value', function (): void { |
| 60 | $id1 = new TimestampId(123); |
| 61 | expect($id1->equals($id1))->toBeTrue(); |
| 62 | |
| 63 | $id2 = TimestampId::from(234); |
| 64 | expect($id1->equals($id2))->toBeFalse(); |
| 65 | }); |
| 66 | }); |