diff --git a/tests/Unit/Shared/Helpers/UuidHelperTest.php b/tests/Unit/Shared/Helpers/UuidHelperTest.php index 9545f53..5ec508c 100644 --- a/tests/Unit/Shared/Helpers/UuidHelperTest.php +++ b/tests/Unit/Shared/Helpers/UuidHelperTest.php @@ -4,13 +4,28 @@ declare(strict_types=1); namespace Tests\Unit\Shared\Helpers; +use Ramsey\Uuid\Fields\FieldsInterface; use Ramsey\Uuid\Guid\Guid; +use Ramsey\Uuid\Nonstandard\Uuid as NonstandardUuid; use Ramsey\Uuid\Uuid; use Shared\Helpers\UuidHelper; covers(UuidHelper::class); describe('Uuid Helper Unit Tests', function (): void { + it('prevents direct instantiation but covers constructor', function (): void { + // GIVEN + $reflection = new \ReflectionClass(UuidHelper::class); + + // WHEN + $constructor = $reflection->getConstructor(); + $constructor->invoke($reflection->newInstanceWithoutConstructor()); + + // THEN + expect(fn () => $reflection->newInstance()) + ->toThrow(\ReflectionException::class); + }); + it('correctly checks if UUID is v4', function (): void { // GIVEN $uuidV4 = Uuid::uuid4(); @@ -31,20 +46,64 @@ describe('Uuid Helper Unit Tests', function (): void { ->and(UuidHelper::isV7($uuidV4))->toBeFalse(); }); - it('can get the version of an UUID including Guid', function (): void { + it('can get the version of an UUID including Guid and Nonstandard Uuid', function (): void { // GIVEN - $uuidV4 = Uuid::uuid4(); - $uuidV7 = Uuid::uuid7(); - $guid = Guid::fromString('00000000-0000-4000-8000-000000000000'); + $uuidV4 = Uuid::uuid4(); + $uuidV7 = Uuid::uuid7(); + $guid = Guid::fromString('00000000-0000-4000-8000-000000000000'); + $nonstandard = NonstandardUuid::fromString('00000000-0000-4000-8000-000000000000'); // WHEN - $resultV4 = UuidHelper::version($uuidV4); - $resultV7 = UuidHelper::version($uuidV7); - $resultGuid = UuidHelper::version($guid); + $resultV4 = UuidHelper::version($uuidV4); + $resultV7 = UuidHelper::version($uuidV7); + $resultGuid = UuidHelper::version($guid); + $resultNonstandard = UuidHelper::version($nonstandard); // THEN expect($resultV4)->toBe(4) ->and($resultV7)->toBe(7) - ->and($resultGuid)->toBe(4); + ->and($resultGuid)->toBe(4) + ->and($resultNonstandard)->toBe(4); + }); + + it('falls back to string parsing when fields are not Rfc4122', function (): void { + // GIVEN + $fields = new class () implements FieldsInterface + { + public function getBytes(): string + { + return ''; + } + + public function serialize(): string + { + return ''; + } + + public function unserialize(string $data): void + { + } + + public function __serialize(): array + { + return []; + } + + public function __unserialize(array $data): void + { + } + }; + + $uuidValid = \Mockery::mock(Uuid::class); + $uuidValid->shouldReceive('getFields')->andReturn($fields); + $uuidValid->shouldReceive('toString')->andReturn('00000000-0000-7000-8000-000000000000'); + + $uuidShort = \Mockery::mock(Uuid::class); + $uuidShort->shouldReceive('getFields')->andReturn($fields); + $uuidShort->shouldReceive('toString')->andReturn('short-uuid'); + + // WHEN & THEN + expect(UuidHelper::version($uuidValid))->toBe(7) + ->and(UuidHelper::version($uuidShort))->toBe(0); }); });