diff --git a/src/Shared/Helpers/UuidHelper.php b/src/Shared/Helpers/UuidHelper.php index 839cf5e..2a5cb4c 100644 --- a/src/Shared/Helpers/UuidHelper.php +++ b/src/Shared/Helpers/UuidHelper.php @@ -11,7 +11,7 @@ final readonly class UuidHelper { private function __construct() { - throw new \LogicException('Static class'); + // Static class } public static function isV4(UuidInterface $uuid): bool diff --git a/tests/Unit/Shared/Helpers/UuidHelperTest.php b/tests/Unit/Shared/Helpers/UuidHelperTest.php index 34474b3..04e702c 100644 --- a/tests/Unit/Shared/Helpers/UuidHelperTest.php +++ b/tests/Unit/Shared/Helpers/UuidHelperTest.php @@ -4,45 +4,60 @@ declare(strict_types=1); namespace Tests\Unit\Shared\Helpers; +use Ramsey\Uuid\Fields\FieldsInterface; use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\UuidInterface; use Shared\Helpers\UuidHelper; covers(UuidHelper::class); -describe('Uuid Helper', function (): void { - it('can verify an UUID is v4', function (): void { - // GIVEN - $uuidV4 = Uuid::uuid4(); +describe('Uuid Helper Unit Tests', function (): void { - // WHEN - $result = UuidHelper::isV4($uuidV4); - - // THEN - expect($result)->toBeTrue(); - }); - - it('can verify an UUID is v7', function (): void { - // GIVEN - $uuidV7 = Uuid::uuid7(); - - // WHEN - $result = UuidHelper::isV7($uuidV7); - - // THEN - expect($result)->toBeTrue(); - }); - - it('can get the version of an UUID', function (): void { + it('correctly checks if UUID is v4', function (): void { // GIVEN $uuidV4 = Uuid::uuid4(); $uuidV7 = Uuid::uuid7(); + // WHEN & THEN + expect(UuidHelper::isV4($uuidV4))->toBeTrue() + ->and(UuidHelper::isV4($uuidV7))->toBeFalse(); + }); + + it('correctly checks if UUID is v7', function (): void { + // GIVEN + $uuidV4 = Uuid::uuid4(); + $uuidV7 = Uuid::uuid7(); + + // WHEN & THEN + expect(UuidHelper::isV7($uuidV7))->toBeTrue() + ->and(UuidHelper::isV7($uuidV4))->toBeFalse(); + }); + + it('gets version from string index fallback when fields are not Rfc4122', function (): void { + // GIVEN + $mockFields = \Mockery::mock(FieldsInterface::class); + $mockUuid = \Mockery::mock(UuidInterface::class); + $mockUuid->shouldReceive('getFields')->andReturn($mockFields); + $mockUuid->shouldReceive('toString')->andReturn('00000000-0000-4000-8000-000000000000'); + // WHEN - $resultV4 = UuidHelper::version($uuidV4); - $resultV7 = UuidHelper::version($uuidV7); + $version = UuidHelper::version($mockUuid); // THEN - expect($resultV4)->toBe(4) - ->and($resultV7)->toBe(7); + expect($version)->toBe(4); + }); + + it('returns 0 when UUID string length is shorter than 15 and not Rfc4122', function (): void { + // GIVEN + $mockFields = \Mockery::mock(FieldsInterface::class); + $mockUuid = \Mockery::mock(UuidInterface::class); + $mockUuid->shouldReceive('getFields')->andReturn($mockFields); + $mockUuid->shouldReceive('toString')->andReturn('short'); + + // WHEN + $version = UuidHelper::version($mockUuid); + + // THEN + expect($version)->toBe(0); }); });