|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use PhpTypedValues\Code\Exception\StringTypeException; |
| 6 | +use PhpTypedValues\String\StringUuidV7; |
| 7 | + |
| 8 | +it('accepts a valid lowercase UUID v7 and preserves value', function (): void { |
| 9 | + // Matches: xxxxxxxx-xxxx-7xxx-[89ab]xxx-xxxxxxxxxxxx |
| 10 | + $uuid = '01890f2a-5bcd-7def-8abc-1234567890ab'; |
| 11 | + $s = new StringUuidV7($uuid); |
| 12 | + |
| 13 | + expect($s->value())->toBe($uuid) |
| 14 | + ->and($s->toString())->toBe($uuid); |
| 15 | +}); |
| 16 | + |
| 17 | +it('normalizes uppercase input to lowercase while preserving the UUID semantics', function (): void { |
| 18 | + $upper = '01890F2A-5BCD-7DEF-9ABC-1234567890AB'; |
| 19 | + $s = StringUuidV7::fromString($upper); |
| 20 | + |
| 21 | + expect($s->value())->toBe('01890f2a-5bcd-7def-9abc-1234567890ab') |
| 22 | + ->and($s->toString())->toBe('01890f2a-5bcd-7def-9abc-1234567890ab'); |
| 23 | +}); |
| 24 | + |
| 25 | +it('throws on empty string', function (): void { |
| 26 | + expect(fn() => new StringUuidV7('')) |
| 27 | + ->toThrow(StringTypeException::class, 'Expected non-empty UUID v7 (xxxxxxxx-xxxx-7xxx-[89ab]xxx-xxxxxxxxxxxx), got ""'); |
| 28 | +}); |
| 29 | + |
| 30 | +it('throws when UUID version is not 7 (e.g., version 4)', function (): void { |
| 31 | + $v4 = '550e8400-e29b-41d4-a716-446655440000'; |
| 32 | + expect(fn() => StringUuidV7::fromString($v4)) |
| 33 | + ->toThrow(StringTypeException::class, 'Expected UUID v7 (xxxxxxxx-xxxx-7xxx-[89ab]xxx-xxxxxxxxxxxx), got "' . $v4 . '"'); |
| 34 | +}); |
| 35 | + |
| 36 | +it('throws when UUID variant nibble is invalid (must be 8,9,a,b)', function (): void { |
| 37 | + // Fourth group starts with '7' which is invalid for RFC 4122 variant |
| 38 | + $badVariant = '550e8400-e29b-7d14-7716-446655440000'; |
| 39 | + expect(fn() => new StringUuidV7($badVariant)) |
| 40 | + ->toThrow(StringTypeException::class, 'Expected UUID v7 (xxxxxxxx-xxxx-7xxx-[89ab]xxx-xxxxxxxxxxxx), got "' . $badVariant . '"'); |
| 41 | +}); |
| 42 | + |
| 43 | +it('throws on invalid characters or format (non-hex character)', function (): void { |
| 44 | + $badChar = '01890f2a-5bcd-7def-8abc-1234567890ag'; |
| 45 | + expect(fn() => StringUuidV7::fromString($badChar)) |
| 46 | + ->toThrow(StringTypeException::class, 'Expected UUID v7 (xxxxxxxx-xxxx-7xxx-[89ab]xxx-xxxxxxxxxxxx), got "' . $badChar . '"'); |
| 47 | +}); |
0 commit comments