Skip to content

Commit ca46e63

Browse files
[DoctrineBridge] Ulid and Uuid as Doctrine Types
1 parent 4c96577 commit ca46e63

17 files changed

+928
-0
lines changed

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
5.2.0
5+
-----
6+
7+
* added support for symfony/uid as `UlidType`, `UuidType`, `UlidBinaryType` and `UuidBinaryType` as Doctrine types
8+
* added `UlidGenerator`, `UUidV1Generator`, `UuidV4Generator` and `UuidV6Generator`
9+
410
5.0.0
511
-----
612

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass;
13+
14+
use Symfony\Bridge\Doctrine\Types\UlidType;
15+
use Symfony\Bridge\Doctrine\Types\UuidType;
16+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\Uid\AbstractUid;
19+
20+
final class RegisterUidTypePass implements CompilerPassInterface
21+
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function process(ContainerBuilder $container)
26+
{
27+
if (!class_exists(AbstractUid::class)) {
28+
return;
29+
}
30+
31+
$typeDefinition = $container->getParameter('doctrine.dbal.connection_factory.types');
32+
33+
if (!isset($typeDefinition['uuid'])) {
34+
$typeDefinition['uuid'] = ['class' => UuidType::class];
35+
}
36+
37+
if (!isset($typeDefinition['ulid'])) {
38+
$typeDefinition['ulid'] = ['class' => UlidType::class];
39+
}
40+
41+
$container->setParameter('doctrine.dbal.connection_factory.types', $typeDefinition);
42+
}
43+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Types;
13+
14+
use Doctrine\ORM\EntityManager;
15+
use Doctrine\ORM\Id\AbstractIdGenerator;
16+
use Symfony\Component\Uid\Ulid;
17+
18+
final class UlidGenerator extends AbstractIdGenerator
19+
{
20+
public function generate(EntityManager $em, $entity): Ulid
21+
{
22+
return new Ulid();
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Types;
13+
14+
use Doctrine\ORM\EntityManager;
15+
use Doctrine\ORM\Id\AbstractIdGenerator;
16+
use Symfony\Component\Uid\UuidV1;
17+
18+
final class UuidV1Generator extends AbstractIdGenerator
19+
{
20+
public function generate(EntityManager $em, $entity): UuidV1
21+
{
22+
return new UuidV1();
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Types;
13+
14+
use Doctrine\ORM\EntityManager;
15+
use Doctrine\ORM\Id\AbstractIdGenerator;
16+
use Symfony\Component\Uid\UuidV4;
17+
18+
final class UuidV4Generator extends AbstractIdGenerator
19+
{
20+
public function generate(EntityManager $em, $entity): UuidV4
21+
{
22+
return new UuidV4();
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Types;
13+
14+
use Doctrine\ORM\EntityManager;
15+
use Doctrine\ORM\Id\AbstractIdGenerator;
16+
use Symfony\Component\Uid\UuidV6;
17+
18+
final class UuidV6Generator extends AbstractIdGenerator
19+
{
20+
public function generate(EntityManager $em, $entity): UuidV6
21+
{
22+
return new UuidV6();
23+
}
24+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Types;
13+
14+
use Doctrine\DBAL\Platforms\AbstractPlatform;
15+
use Doctrine\DBAL\Types\ConversionException;
16+
use Doctrine\DBAL\Types\Type;
17+
use PHPUnit\Framework\TestCase;
18+
use Symfony\Bridge\Doctrine\Types\UlidBinaryType;
19+
use Symfony\Component\Uid\Ulid;
20+
21+
class UlidBinaryTypeTest extends TestCase
22+
{
23+
private const DUMMY_ULID = '01EEDQEK6ZAZE93J8KG5B4MBJC';
24+
25+
private $platform;
26+
27+
/** @var UlidBinaryType */
28+
private $type;
29+
30+
public static function setUpBeforeClass(): void
31+
{
32+
Type::addType('ulid_binary', UlidBinaryType::class);
33+
}
34+
35+
protected function setUp(): void
36+
{
37+
$this->platform = $this->createMock(AbstractPlatform::class);
38+
$this->platform
39+
->method('getBinaryTypeDeclarationSQL')
40+
->willReturn('DUMMYBINARY(16)');
41+
42+
$this->type = Type::getType('ulid_binary');
43+
}
44+
45+
public function testUlidConvertsToDatabaseValue()
46+
{
47+
$uuid = Ulid::fromString(self::DUMMY_ULID);
48+
49+
$expected = $uuid->toBinary();
50+
$actual = $this->type->convertToDatabaseValue($uuid, $this->platform);
51+
52+
$this->assertEquals($expected, $actual);
53+
}
54+
55+
public function testStringUlidConvertsToDatabaseValue()
56+
{
57+
$expected = Ulid::fromString(self::DUMMY_ULID)->toBinary();
58+
$actual = $this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);
59+
60+
$this->assertEquals($expected, $actual);
61+
}
62+
63+
public function testInvalidUlidConversionForDatabaseValue()
64+
{
65+
$this->expectException(ConversionException::class);
66+
67+
$this->type->convertToDatabaseValue('abcdefg', $this->platform);
68+
}
69+
70+
public function testNotSupportedTypeConversionForDatabaseValue()
71+
{
72+
$this->assertNull($this->type->convertToDatabaseValue(new \stdClass(), $this->platform));
73+
}
74+
75+
public function testNullConversionForDatabaseValue()
76+
{
77+
$this->assertNull($this->type->convertToDatabaseValue(null, $this->platform));
78+
}
79+
80+
public function testUlidConvertsToPHPValue()
81+
{
82+
$uuid = $this->type->convertToPHPValue(self::DUMMY_ULID, $this->platform);
83+
84+
$this->assertEquals(self::DUMMY_ULID, $uuid->__toString());
85+
}
86+
87+
public function testInvalidUlidConversionForPHPValue()
88+
{
89+
$this->expectException(ConversionException::class);
90+
91+
$this->type->convertToPHPValue('abcdefg', $this->platform);
92+
}
93+
94+
public function testNullConversionForPHPValue()
95+
{
96+
$this->assertNull($this->type->convertToPHPValue(null, $this->platform));
97+
}
98+
99+
public function testReturnValueIfUlidForPHPValue()
100+
{
101+
$uuid = new Ulid();
102+
$this->assertSame($uuid, $this->type->convertToPHPValue($uuid, $this->platform));
103+
}
104+
105+
public function testGetName()
106+
{
107+
$this->assertEquals('ulid_binary', $this->type->getName());
108+
}
109+
110+
public function testGetGuidTypeDeclarationSQL()
111+
{
112+
$this->assertEquals('DUMMYBINARY(16)', $this->type->getSqlDeclaration(['length' => 36], $this->platform));
113+
}
114+
115+
public function testRequiresSQLCommentHint()
116+
{
117+
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
118+
}
119+
}

0 commit comments

Comments
 (0)