Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
58 / 58 |
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\UuidV4; |
| 13 | use Phexium\Plugin\IdGenerator\Adapter\UuidV4Generator; |
| 14 | |
| 15 | describe('Generation', function (): void { |
| 16 | it('generates a UuidV4 object', function (): void { |
| 17 | $generator = new UuidV4Generator(); |
| 18 | |
| 19 | $result = $generator->generate(); |
| 20 | |
| 21 | expect($result)->toBeInstanceOf(UuidV4::class); |
| 22 | }); |
| 23 | |
| 24 | it('creates a UuidV4 from a string', function (): void { |
| 25 | $generator = new UuidV4Generator(); |
| 26 | $validUuid = '550e8400-e29b-41d4-a716-446655440000'; |
| 27 | |
| 28 | $result = $generator->from($validUuid); |
| 29 | |
| 30 | expect($result)->toBeInstanceOf(UuidV4::class) |
| 31 | ->and($result->getValue())->toBe($validUuid) |
| 32 | ; |
| 33 | }); |
| 34 | |
| 35 | it('converts to string', function (): void { |
| 36 | $uuid = '550e8400-e29b-41d4-a716-446655440000'; |
| 37 | $id = UuidV4::from($uuid); |
| 38 | |
| 39 | expect((string) $id)->toBeString() |
| 40 | ->toBe($uuid) |
| 41 | ->and($id->getValue())->toBe($uuid) |
| 42 | ; |
| 43 | }); |
| 44 | |
| 45 | it('throws exception when generator creates from int', function (): void { |
| 46 | $generator = new UuidV4Generator(); |
| 47 | |
| 48 | expect(fn (): UuidV4 => $generator->from(123)) |
| 49 | ->toThrow(LogicException::class, 'UuidV4 generator cannot create UUIDs from integers') |
| 50 | ; |
| 51 | }); |
| 52 | |
| 53 | it('throws exception when UuidV4 creates from int', function (): void { |
| 54 | expect(fn (): UuidV4 => UuidV4::from(123)) |
| 55 | ->toThrow(LogicException::class, 'UUID cannot be created from an integer') |
| 56 | ; |
| 57 | }); |
| 58 | |
| 59 | it('throws exception with invalid string', function (): void { |
| 60 | expect(fn (): UuidV4 => UuidV4::from('invalid-uuid')) |
| 61 | ->toThrow(InvalidArgumentException::class, 'Invalid UUID: invalid-uuid') |
| 62 | ; |
| 63 | }); |
| 64 | }); |
| 65 | |
| 66 | describe('Uniqueness', function (): void { |
| 67 | it('compares by value', function (): void { |
| 68 | $uuid = '550e8400-e29b-41d4-a716-446655440000'; |
| 69 | $id1 = UuidV4::from($uuid); |
| 70 | |
| 71 | expect($id1->equals($id1))->toBeTrue(); |
| 72 | |
| 73 | $anotherUuid = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; |
| 74 | $id2 = UuidV4::from($anotherUuid); |
| 75 | |
| 76 | expect($id1->equals($id2))->toBeFalse(); |
| 77 | }); |
| 78 | |
| 79 | it('generates different UUIDs', function (): void { |
| 80 | $generator = new UuidV4Generator(); |
| 81 | |
| 82 | $id1 = $generator->generate(); |
| 83 | $id2 = $generator->generate(); |
| 84 | |
| 85 | expect($id1->equals($id2))->toBeFalse() |
| 86 | ->and($id1->getValue())->not->toBe($id2->getValue()) |
| 87 | ; |
| 88 | }); |
| 89 | }); |