UuidV4 example


        $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());
        $client->request('GET', '/1/uuid-v1/'.$uuidV1->toRfc4122());
        $this->assertSame((string) $uuidV1$client->getResponse()->getContent());
        // Bad version         $client->request('GET', '/1/uuid-v1/'.$uuidV4 = new UuidV4());
        $this->assertSame(404, $client->getResponse()->getStatusCode());

        // Only base58 format         $client->request('GET', '/2/ulid/'.($ulid = new Ulid())->toBase58());
        $this->assertSame((string) $ulid$client->getResponse()->getContent());
        $client->request('GET', '/2/ulid/'.$ulid);
        $this->assertSame(404, $client->getResponse()->getStatusCode());
        $client->request('GET', '/2/ulid/'.$ulid->toRfc4122());
        $this->assertSame(404, $client->getResponse()->getStatusCode());

        // Only base32 format
yield ['8dac64d3-937a-1e7c-fa1d-d5d6c06a61f5'];
        yield ['8dac64d3-937a-3e7c-fa1d-d5d6c06a61f5'];
        yield ['8dac64d3-937a-4e7c-fa1d-d5d6c06a61f5'];
        yield ['8dac64d3-937a-5e7c-fa1d-d5d6c06a61f5'];
        yield ['8dac64d3-937a-6e7c-fa1d-d5d6c06a61f5'];
        yield ['8dac64d3-937a-7e7c-fa1d-d5d6c06a61f5'];
        yield ['8dac64d3-937a-8e7c-fa1d-d5d6c06a61f5'];
    }

    public function testConstructorWithValidUuid()
    {
        $uuid = new UuidV4(self::A_UUID_V4);

        $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);

        
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\UuidV4;
use Symfony\Component\Uid\UuidV6;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;

final class SymfonyCasterTest extends TestCase
{
    use VarDumperTestTrait;

    public function testCastUuid()
    {
        $uuid = new UuidV4('83a9db35-3c8c-4040-b3c1-02eccc00b419');
        $expectedDump = <<<EODUMP Symfony\Component\Uid\UuidV4 { #uid: "83a9db35-3c8c-4040-b3c1-02eccc00b419" toBase58: "HFzAAuYvev42cCjwqpnKqz" toBase32: "43N7DKAF4C810B7G82XK601D0S" } EODUMP;
        $this->assertDumpEquals($expectedDump$uuid);

        $uuid = new UuidV6('1ebc50e9-8a23-6704-ad6f-59afd5cda7e5');
        if (method_exists($uuid, 'getDateTime')) {
            
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
    {
        return new UuidV1();
    }
public function testUuidCanBeGenerated()
    {
        $em = (new \ReflectionClass(EntityManager::class))->newInstanceWithoutConstructor();
        $generator = new UuidGenerator();
        $uuid = $generator->generate($emnew Entity());

        $this->assertInstanceOf(Uuid::class$uuid);
    }

    public function testCustomUuidfactory()
    {
        $uuid = new UuidV4();
        $em = (new \ReflectionClass(EntityManager::class))->newInstanceWithoutConstructor();
        $factory = $this->createMock(UuidFactory::class);
        $factory->expects($this->any())
            ->method('create')
            ->willReturn($uuid);
        $generator = new UuidGenerator($factory);

        $this->assertSame($uuid$generator->generate($emnew Entity()));
    }

    public function testUuidfactory()
    {

        if (!$expected) {
            $this->assertSame([](new UidValueResolver())->resolve($request$argument));
        }

        $this->assertSame($expected(new UidValueResolver())->supports($request$argument));
    }

    public static function provideSupports()
    {
        return [
            'Variadic argument' => [false, new Request([][]['foo' => (string) $uuidV4 = new UuidV4()])new ArgumentMetadata('foo', UuidV4::class, true, false, null)],
            'No attribute for argument' => [false, new Request([][][])new ArgumentMetadata('foo', UuidV4::class, false, false, null)],
            'Attribute is not a string' => [false, new Request([][]['foo' => ['bar']])new ArgumentMetadata('foo', UuidV4::class, false, false, null)],
            'Argument has no type' => [false, new Request([][]['foo' => (string) $uuidV4])new ArgumentMetadata('foo', null, false, false, null)],
            'Argument type is not a class' => [false, new Request([][]['foo' => (string) $uuidV4])new ArgumentMetadata('foo', 'string', false, false, null)],
            'Argument type is not a subclass of AbstractUid' => [false, new Request([][]['foo' => (string) $uuidV4])new ArgumentMetadata('foo', UlidFactory::class, false, false, null)],
            'AbstractUid is not supported' => [false, new Request([][]['foo' => (string) $uuidV4])new ArgumentMetadata('foo', AbstractUid::class, false, false, null)],
            'Custom abstract subclass is supported but will fail in resolve' => [true, new Request([][]['foo' => (string) $uuidV4])new ArgumentMetadata('foo', TestAbstractCustomUid::class, false, false, null)],
            'Known subclass' => [true, new Request([][]['foo' => (string) $uuidV4])new ArgumentMetadata('foo', UuidV4::class, false, false, null)],
            'Format does not matter' => [true, new Request([][]['foo' => (string) $uuidV4])new ArgumentMetadata('foo', Ulid::class, false, false, null)],
            'Custom subclass' => [true, new Request([][]['foo' => '01FPND7BD15ZV07X5VGDXAJ8VD'])new ArgumentMetadata('foo', TestCustomUid::class, false, false, null)],
        ];
    }
$this->assertTrue($ulid->equals(Ulid::fromString(hex2bin('7fffffffffffffffffffffffffffffff'))));
    }

    public function toHex()
    {
        $ulid = Ulid::fromString('1BVXue8CnY8ogucrHX3TeF');
        $this->assertSame('0x0177058f4dacd0b2a990a49af02bc008', $ulid->toHex());
    }

    public function testFromUuid()
    {
        $uuid = new UuidV4();

        $ulid = Ulid::fromString($uuid);

        $this->assertSame($uuid->toBase32()(string) $ulid);
        $this->assertSame($ulid->toBase32()(string) $ulid);
        $this->assertSame((string) $uuid$ulid->toRfc4122());
        $this->assertTrue($ulid->equals(Ulid::fromString($uuid)));
    }

    public function testBase58()
    {
        
Home | Imprint | This part of the site doesn't use cookies.