2026-08-13 10:24:05 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-08-13 10:29:05 +02:00
|
|
|
namespace Tests\Unit\Shared\Helpers;
|
|
|
|
|
|
2026-08-13 13:06:01 +02:00
|
|
|
use Ramsey\Uuid\Fields\FieldsInterface;
|
2026-08-13 10:24:05 +02:00
|
|
|
use Ramsey\Uuid\Uuid;
|
2026-08-13 13:06:01 +02:00
|
|
|
use Ramsey\Uuid\UuidInterface;
|
2026-08-13 10:24:05 +02:00
|
|
|
use Shared\Helpers\UuidHelper;
|
|
|
|
|
|
|
|
|
|
covers(UuidHelper::class);
|
|
|
|
|
|
2026-08-13 13:06:01 +02:00
|
|
|
describe('Uuid Helper Unit Tests', function (): void {
|
|
|
|
|
|
|
|
|
|
it('correctly checks if UUID is v4', function (): void {
|
2026-08-13 10:24:05 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$uuidV4 = Uuid::uuid4();
|
2026-08-13 13:06:01 +02:00
|
|
|
$uuidV7 = Uuid::uuid7();
|
2026-08-13 10:24:05 +02:00
|
|
|
|
2026-08-13 13:06:01 +02:00
|
|
|
// WHEN & THEN
|
|
|
|
|
expect(UuidHelper::isV4($uuidV4))->toBeTrue()
|
|
|
|
|
->and(UuidHelper::isV4($uuidV7))->toBeFalse();
|
2026-08-13 10:24:05 +02:00
|
|
|
});
|
|
|
|
|
|
2026-08-13 13:06:01 +02:00
|
|
|
it('correctly checks if UUID is v7', function (): void {
|
2026-08-13 10:24:05 +02:00
|
|
|
// GIVEN
|
2026-08-13 13:06:01 +02:00
|
|
|
$uuidV4 = Uuid::uuid4();
|
2026-08-13 10:24:05 +02:00
|
|
|
$uuidV7 = Uuid::uuid7();
|
|
|
|
|
|
2026-08-13 13:06:01 +02:00
|
|
|
// 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');
|
|
|
|
|
|
2026-08-13 10:24:05 +02:00
|
|
|
// WHEN
|
2026-08-13 13:06:01 +02:00
|
|
|
$version = UuidHelper::version($mockUuid);
|
2026-08-13 10:24:05 +02:00
|
|
|
|
|
|
|
|
// THEN
|
2026-08-13 13:06:01 +02:00
|
|
|
expect($version)->toBe(4);
|
2026-08-13 10:24:05 +02:00
|
|
|
});
|
|
|
|
|
|
2026-08-13 13:06:01 +02:00
|
|
|
it('returns 0 when UUID string length is shorter than 15 and not Rfc4122', function (): void {
|
2026-08-13 10:24:05 +02:00
|
|
|
// GIVEN
|
2026-08-13 13:06:01 +02:00
|
|
|
$mockFields = \Mockery::mock(FieldsInterface::class);
|
|
|
|
|
$mockUuid = \Mockery::mock(UuidInterface::class);
|
|
|
|
|
$mockUuid->shouldReceive('getFields')->andReturn($mockFields);
|
|
|
|
|
$mockUuid->shouldReceive('toString')->andReturn('short');
|
2026-08-13 10:24:05 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
2026-08-13 13:06:01 +02:00
|
|
|
$version = UuidHelper::version($mockUuid);
|
2026-08-13 10:24:05 +02:00
|
|
|
|
|
|
|
|
// THEN
|
2026-08-13 13:06:01 +02:00
|
|
|
expect($version)->toBe(0);
|
2026-08-13 10:24:05 +02:00
|
|
|
});
|
|
|
|
|
});
|