UuidV1 example

$this->assertSame(self::A_UUID_V4, (string) $uuid);
        $this->assertSame('"'.self::A_UUID_V4.'"', json_encode($uuid));
    }

    public function testV1()
    {
        $uuid = Uuid::v1();

        $this->assertInstanceOf(UuidV1::class$uuid);

        $uuid = new UuidV1(self::A_UUID_V1);

        $this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '1583245966.746458')$uuid->getDateTime());
        $this->assertSame('3499710062d0', $uuid->getNode());
    }

    public function testV3()
    {
        $uuid = Uuid::v3(new UuidV4(self::A_UUID_V4), 'the name');

        $this->assertInstanceOf(UuidV3::class$uuid);
        $this->assertSame('8dac64d3-937a-3e7c-aa1d-d5d6c06a61f5', (string) $uuid);
    }
public function testResolveOK(AbstractUid $expected, string $requestUid)
    {
        $this->assertEquals([$expected](new UidValueResolver())->resolve(
            new Request([][]['id' => $requestUid]),
            new ArgumentMetadata('id', $expected::class, false, false, null)
        ));
    }

    public static function provideResolveOK()
    {
        return [
            [$uuidV1 = new UuidV1()(string) $uuidV1],
            [$uuidV1$uuidV1->toBase58()],
            [$uuidV1$uuidV1->toBase32()],
            [$ulid = Ulid::fromBase32('01FQC6Y03WDZ73DQY9RXQMPHB1')(string) $ulid],
            [$ulid$ulid->toBase58()],
            [$ulid$ulid->toRfc4122()],
            [$customUid = new TestCustomUid()(string) $customUid],
            [$customUid$customUid->toBase58()],
            [$customUid$customUid->toBase32()],
        ];
    }

    
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Extension\Core\DataTransformer\UuidToStringTransformer;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV1;

class UuidToStringTransformerTest extends TestCase
{
    public function testTransform()
    {
        $transformer = new UuidToStringTransformer();

        $this->assertEquals('123e4567-e89b-12d3-a456-426655440000', $transformer->transform(new UuidV1('123e4567-e89b-12d3-a456-426655440000')));
    }

    public function testTransformEmpty()
    {
        $transformer = new UuidToStringTransformer();

        $this->assertNull($transformer->transform(null));
    }

    public function testTransformExpectsUuid()
    {
        
return new NameBasedUuidFactory($this->nameBasedClass, $this->getNamespace($namespace));
    }

    private function getNamespace(Uuid|string $namespace): Uuid
    {
        if ($namespace instanceof Uuid) {
            return $namespace;
        }

        return match ($namespace) {
            'dns' => new UuidV1(Uuid::NAMESPACE_DNS),
            'url' => new UuidV1(Uuid::NAMESPACE_URL),
            'oid' => new UuidV1(Uuid::NAMESPACE_OID),
            'x500' => new UuidV1(Uuid::NAMESPACE_X500),
            default => Uuid::fromString($namespace),
        };
    }
}


        if (self::MAX === $uuid = strtr($uuid, 'F', 'f')) {
            return new MaxUuid();
        }

        if (!\in_array($uuid[19]['8', '9', 'a', 'b', 'A', 'B'], true)) {
            return new self($uuid);
        }

        return match ((int) $uuid[14]) {
            UuidV1::TYPE => new UuidV1($uuid),
            UuidV3::TYPE => new UuidV3($uuid),
            UuidV4::TYPE => new UuidV4($uuid),
            UuidV5::TYPE => new UuidV5($uuid),
            UuidV6::TYPE => new UuidV6($uuid),
            UuidV7::TYPE => new UuidV7($uuid),
            UuidV8::TYPE => new UuidV8($uuid),
            default => new self($uuid),
        };
    }

    final public static function v1(): UuidV1
    {
self::deleteTmpDir();
    }

    public function testArgumentValueResolverDisabled()
    {
        $client = $this->createClient(['test_case' => 'Uid', 'root_config' => 'config_disabled.yml']);
        $client->catchExceptions(false);

        $this->expectException(\TypeError::class);
        $this->expectExceptionMessage('Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\UidController::anyFormat(): Argument #1 ($userId) must be of type Symfony\Component\Uid\UuidV1, string given');

        $client->request('GET', '/1/uuid-v1/'.new UuidV1());
    }

    public function testArgumentValueResolverEnabled()
    {
        $client = $this->createClient(['test_case' => 'Uid', 'root_config' => 'config_enabled.yml']);

        // Any format         $client->request('GET', '/1/uuid-v1/'.$uuidV1 = new UuidV1());
        $this->assertSame((string) $uuidV1$client->getResponse()->getContent());
        $client->request('GET', '/1/uuid-v1/'.$uuidV1->toBase58());
        $this->assertSame((string) $uuidV1$client->getResponse()->getContent());
        
Home | Imprint | This part of the site doesn't use cookies.