Ulid example



namespace Symfony\Component\Uid\Factory;

use Symfony\Component\Uid\Ulid;

class UlidFactory
{
    public function create(\DateTimeInterface $time = null): Ulid
    {
        return new Ulid(null === $time ? null : Ulid::generate($time));
    }
}
        $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         $client->request('GET', '/3/uuid-v1/'.$uuidV1->toBase32());
        $this->assertSame((string) $uuidV1$client->getResponse()->getContent());
        $client->request('GET', '/3/uuid-v1/'.$uuidV1);
        $this->assertSame(404, $client->getResponse()->getStatusCode());
        


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

        $this->assertSame('0xd6b3345b29054048a83cb5988e765d98', $uuid->toHex());
    }

    public function testFromUlid()
    {
        $ulid = new Ulid();
        $uuid = Uuid::fromString($ulid);

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

    public function testBase58()
    {
        $uuid = new NilUuid();
        $this->assertSame('1111111111111111111111', $uuid->toBase58());

        
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Extension\Core\DataTransformer\UlidToStringTransformer;
use Symfony\Component\Uid\Ulid;

class UlidToStringTransformerTest extends TestCase
{
    public static function provideValidUlid()
    {
        return [
            ['01D85PP1982GF6KTVFHQ7W78FB', new Ulid('01d85pp1982gf6ktvfhq7w78fb')],
        ];
    }

    /** * @dataProvider provideValidUlid */
    public function testTransform($output$input)
    {
        $transformer = new UlidToStringTransformer();

        $input = new Ulid($input);

        

class UlidValidatorTest extends ConstraintValidatorTestCase
{
    protected function createValidator(): UlidValidator
    {
        return new UlidValidator();
    }

    public function testNullIsValid()
    {
        $this->validator->validate(null, new Ulid());

        $this->assertNoViolation();
    }

    public function testEmptyStringIsValid()
    {
        $this->validator->validate('', new Ulid());

        $this->assertNoViolation();
    }

    
$this->type->convertToPHPValue('abcdefg', new SqlitePlatform());
    }

    public function testNullConversionForPHPValue()
    {
        $this->assertNull($this->type->convertToPHPValue(null, new SqlitePlatform()));
    }

    public function testReturnValueIfUlidForPHPValue()
    {
        $ulid = new Ulid();

        $this->assertSame($ulid$this->type->convertToPHPValue($ulidnew SqlitePlatform()));
    }

    public function testGetName()
    {
        $this->assertEquals('ulid', $this->type->getName());
    }

    /** * @dataProvider provideSqlDeclarations */

        $this->normalizer = new UidNormalizer();
    }

    public function testSupportsNormalization()
    {
        $this->assertTrue($this->normalizer->supportsNormalization(Uuid::v1()));
        $this->assertTrue($this->normalizer->supportsNormalization(Uuid::v3(Uuid::v1(), 'foo')));
        $this->assertTrue($this->normalizer->supportsNormalization(Uuid::v4()));
        $this->assertTrue($this->normalizer->supportsNormalization(Uuid::v5(Uuid::v1(), 'foo')));
        $this->assertTrue($this->normalizer->supportsNormalization(Uuid::v6()));
        $this->assertTrue($this->normalizer->supportsNormalization(new Ulid()));
        $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
    }

    public static function normalizeProvider()
    {
        $uidFormats = [null, 'canonical', 'base58', 'base32', 'rfc4122'];
        $data = [
             [
                 UuidV1::fromString('9b7541de-6f87-11ea-ab3c-9da9a81562fc'),
                '9b7541de-6f87-11ea-ab3c-9da9a81562fc',
                '9b7541de-6f87-11ea-ab3c-9da9a81562fc',
                
use Symfony\Component\Uid\Tests\Fixtures\CustomUlid;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\UuidV4;

class UlidTest extends TestCase
{
    /** * @group time-sensitive */
    public function testGenerate()
    {
        $a = new Ulid();
        $b = new Ulid();
        usleep(-10000);
        $c = new Ulid();

        $this->assertSame(0, strncmp($a$b, 20));
        $this->assertSame(0, strncmp($a$c, 20));
        $a = base_convert(strtr(substr($a, -6), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv'), 32, 10);
        $b = base_convert(strtr(substr($b, -6), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv'), 32, 10);
        $c = base_convert(strtr(substr($c, -6), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv'), 32, 10);
        $this->assertSame(1, $b - $a);
        $this->assertSame(1, $c - $b);
    }

        $em = (new \ReflectionClass(EntityManager::class))->newInstanceWithoutConstructor();
        $generator = new UlidGenerator();
        $ulid = $generator->generate($emnew Entity());

        $this->assertInstanceOf(Ulid::class$ulid);
        $this->assertTrue(Ulid::isValid($ulid));
    }

    public function testUlidFactory()
    {
        $ulid = new Ulid('00000000000000000000000000');
        $em = (new \ReflectionClass(EntityManager::class))->newInstanceWithoutConstructor();
        $factory = $this->createMock(UlidFactory::class);
        $factory->expects($this->any())
            ->method('create')
            ->willReturn($ulid);
        $generator = new UlidGenerator($factory);

        $this->assertSame($ulid$generator->generate($emnew Entity()));
    }
}
public function generate(EntityManager $em$entity): Ulid
    {
        return $this->generateId($em$entity);
    }

    public function generateId(EntityManagerInterface $em$entity): Ulid
    {
        if ($this->factory) {
            return $this->factory->create();
        }

        return new Ulid();
    }
}
public function reverseTransform(mixed $value): ?Ulid
    {
        if (null === $value || '' === $value) {
            return null;
        }

        if (!\is_string($value)) {
            throw new TransformationFailedException('Expected a string.');
        }

        try {
            $ulid = new Ulid($value);
        } catch (\InvalidArgumentException $e) {
            throw new TransformationFailedException(sprintf('The value "%s" is not a valid ULID.', $value)$e->getCode()$e);
        }

        return $ulid;
    }
}
use Symfony\Component\Uid\Ulid;

final class UlidTypeTest extends BaseTypeTestCase
{
    public const TESTED_TYPE = UlidType::class;

    public function testPassUlidToView()
    {
        $ulid = '01D85PP1982GF6KTVFHQ7W78FB';

        $form = $this->factory->create(static::TESTED_TYPE);
        $form->setData(new Ulid($ulid));

        $this->assertSame($ulid$form->createView()->vars['value']);
    }

    public function testSubmitNullUsesDefaultEmptyData($emptyData = '', $expectedData = null)
    {
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'empty_data' => $emptyData,
        ]);
        $form->submit(null);

        
EODUMP;
        }

        $this->assertDumpEquals($expectedDump$uuid);
    }

    public function testCastUlid()
    {
        $ulid = new Ulid('01F7B252SZQGTSQGYSGACASAW6');
        if (method_exists($ulid, 'getDateTime')) {
            $expectedDump = <<<EODUMP Symfony\Component\Uid\Ulid { #uid: "01F7B252SZQGTSQGYSGACASAW6" toBase58: "1Ba6pJPFWDwghSKFVvfQ1B" toRfc4122: "0179d622-8b3f-bc35-9bc3-d98298acab86" time: "2021-06-04 08:27:38.687 UTC" } EODUMP;
        } else {
            $expectedDump = <<<EODUMP
Home | Imprint | This part of the site doesn't use cookies.