-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DoctrineBridge] Ulid and Uuid as Doctrine Types #37678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass; | ||
|
||
use Symfony\Bridge\Doctrine\Types\UlidType; | ||
use Symfony\Bridge\Doctrine\Types\UuidType; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\Uid\AbstractUid; | ||
|
||
final class RegisterUidTypePass implements CompilerPassInterface | ||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!class_exists(AbstractUid::class)) { | ||
return; | ||
} | ||
|
||
$typeDefinition = $container->getParameter('doctrine.dbal.connection_factory.types'); | ||
|
||
if (!isset($typeDefinition['uuid'])) { | ||
$typeDefinition['uuid'] = ['class' => UuidType::class]; | ||
} | ||
|
||
if (!isset($typeDefinition['ulid'])) { | ||
$typeDefinition['ulid'] = ['class' => UlidType::class]; | ||
} | ||
|
||
$container->setParameter('doctrine.dbal.connection_factory.types', $typeDefinition); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/* | ||
< 8000 td class="blob-code blob-code-addition js-file-line"> * This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Types; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The namespace does not match the folder path. BTW, why not call it "Generator"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you send a PR please to fix the namespace?
not sure what you mean here sorry... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
got it! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, it was just not very clear to have the namespace "Id" but "Uid" looks better to me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Good catch. Thx. |
||
|
||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Id\AbstractIdGenerator; | ||
use Symfony\Component\Uid\Ulid; | ||
|
||
final class UlidGenerator extends AbstractIdGenerator | ||
{ | ||
public function generate(EntityManager $em, $entity): Ulid | ||
{ | ||
return new Ulid(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
A3E2 * For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Types; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Id\AbstractIdGenerator; | ||
use Symfony\Component\Uid\UuidV1; | ||
|
||
final class UuidV1Generator extends AbstractIdGenerator | ||
{ | ||
public function generate(EntityManager $em, $entity): UuidV1 | ||
{ | ||
return new UuidV1(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Types; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Id\AbstractIdGenerator; | ||
use Symfony\Component\Uid\UuidV4; | ||
|
||
final class UuidV4Generator extends AbstractIdGenerator | ||
{ | ||
public function generate(EntityManager $em, $entity): UuidV4 | ||
{ | ||
return new UuidV4(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Types; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Id\AbstractIdGenerator; | ||
use Symfony\Component\Uid\UuidV6; | ||
|
||
final class UuidV6Generator extends AbstractIdGenerator | ||
{ | ||
public function generate(EntityManager $em, $entity): UuidV6 | ||
{ | ||
return new UuidV6(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Tests\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\ConversionException; | ||
use Doctrine\DBAL\Types\Type; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bridge\Doctrine\Types\UlidBinaryType; | ||
use Symfony\Component\Uid\Ulid; | ||
|
||
class UlidBinaryTypeTest extends TestCase | ||
{ | ||
private const DUMMY_ULID = '01EEDQEK6ZAZE93J8KG5B4MBJC'; | ||
|
||
private $platform; | ||
|
||
/** @var UlidBinaryType */ | ||
private $type; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
Type::addType('ulid_binary', UlidBinaryType::class); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->platform = $this->createMock(AbstractPlatform::class); | ||
$this->platform | ||
->method('getBinaryTypeDeclarationSQL') | ||
->willReturn('DUMMYBINARY(16)'); | ||
|
||
$this->type = Type::getType('ulid_binary'); | ||
} | ||
|
||
public function testUlidConvertsToDatabaseValue() | ||
{ | ||
$uuid = Ulid::fromString(self::DUMMY_ULID); | ||
|
||
$expected = $uuid->toBinary(); | ||
$actual = $this->type->convertToDatabaseValue($uuid, $this->platform); | ||
|
||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
public function testStringUlidConvertsToDatabaseValue() | ||
{ | ||
$expected = Ulid::fromString(self::DUMMY_ULID)->toBinary(); | ||
$actual = $this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform); | ||
|
||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
public function testInvalidUlidConversionForDatabaseValue() | ||
{ | ||
$this->expectException(ConversionException::class); | ||
|
||
$this->type->convertToDatabaseValue('abcdefg', $this->platform); | ||
} | ||
|
||
public function testNotSupportedTypeConversionForDatabaseValue() | ||
{ | ||
$this->assertNull($this->type->convertToDatabaseValue(new \stdClass(), $this->platform)); | ||
} | ||
|
||
public function testNullConversionForDatabaseValue() | ||
{ | ||
$this->assertNull($this->type->convertToDatabaseValue(null, $this->platform)); | ||
} | ||
|
||
public function testUlidConvertsToPHPValue() | ||
{ | ||
$uuid = $this->type->convertToPHPValue(self::DUMMY_ULID, $this->platform); | ||
|
||
$this->assertEquals(self::DUMMY_ULID, $uuid->__toString()); | ||
} | ||
|
||
public function testInvalidUlidConversionForPHPValue() | ||
{ | ||
$this->expectException(ConversionException::class); | ||
|
||
$this->type->convertToPHPValue('abcdefg', $this->platform); | ||
} | ||
|
||
public function testNullConversionForPHPValue() | ||
{ | ||
$this->assertNull($this->type->convertToPHPValue(null, $this->platform)); | ||
} | ||
|
||
public function testReturnValueIfUlidForPHPValue() | ||
{ | ||
$uuid = new Ulid(); | ||
$this->assertSame($uuid, $this->type->convertToPHPValue($uuid, $this->platform)); | ||
} | ||
|
||
public function testGetName() | ||
{ | ||
$this->assertEquals('ulid_binary', $this->type->getName()); | ||
} | ||
|
||
public function testGetGuidTypeDeclarationSQL() | ||
{ | ||
$this->assertEquals('DUMMYBINARY(16)', $this->type->getSqlDeclaration(['length' => 36], $this->platform)); | ||
} | ||
|
||
public function testRequiresSQLCommentHint() | ||
{ | ||
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform)); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.