diff --git a/src/Symfony/Bridge/Doctrine/CHANGELOG.md b/src/Symfony/Bridge/Doctrine/CHANGELOG.md index 574db63d5ec07..ea6918de35f1c 100644 --- a/src/Symfony/Bridge/Doctrine/CHANGELOG.md +++ b/src/Symfony/Bridge/Doctrine/CHANGELOG.md @@ -4,7 +4,7 @@ CHANGELOG 5.2.0 ----- - * added support for symfony/uid as `UlidType`, `UuidType`, `UlidBinaryType` and `UuidBinaryType` as Doctrine types + * added support for symfony/uid as `UlidType` and `UuidType` as Doctrine types * added `UlidGenerator`, `UUidV1Generator`, `UuidV4Generator` and `UuidV6Generator` 5.0.0 diff --git a/src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterUidTypePass.php b/src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterUidTypePass.php index f5fea85d14e9c..e30fe44429670 100644 --- a/src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterUidTypePass.php +++ b/src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterUidTypePass.php @@ -11,9 +11,7 @@ namespace Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass; -use Symfony\Bridge\Doctrine\Types\UlidBinaryType; use Symfony\Bridge\Doctrine\Types\UlidType; -use Symfony\Bridge\Doctrine\Types\UuidBinaryType; use Symfony\Bridge\Doctrine\Types\UuidType; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -40,14 +38,6 @@ public function process(ContainerBuilder $container) $typeDefinition['ulid'] = ['class' => UlidType::class]; } - if (!isset($typeDefinition['uuid_binary'])) { - $typeDefinition['uuid_binary'] = ['class' => UuidBinaryType::class]; - } - - if (!isset($typeDefinition['ulid_binary'])) { - $typeDefinition['ulid_binary'] = ['class' => UlidBinaryType::class]; - } - $container->setParameter('doctrine.dbal.connection_factory.types', $typeDefinition); } } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Types/UlidBinaryTypeTest.php b/src/Symfony/Bridge/Doctrine/Tests/Types/UlidBinaryTypeTest.php deleted file mode 100644 index 4141dfa55e540..0000000000000 --- a/src/Symfony/Bridge/Doctrine/Tests/Types/UlidBinaryTypeTest.php +++ /dev/null @@ -1,114 +0,0 @@ - - * - * 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 testNotSupportedTypeConversionForDatabaseValue() - { - $this->expectException(ConversionException::class); - - $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)); - } -} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php b/src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php index f3969bcb4c725..36aace3ed843d 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php @@ -37,6 +37,9 @@ public static function setUpBeforeClass(): void protected function setUp(): void { $this->platform = $this->createMock(AbstractPlatform::class); + $this->platform + ->method('hasNativeGuidType') + ->willReturn(true); $this->platform ->method('getGuidTypeDeclarationSQL') ->willReturn('DUMMYVARCHAR()'); diff --git a/src/Symfony/Bridge/Doctrine/Tests/Types/UuidBinaryTypeTest.php b/src/Symfony/Bridge/Doctrine/Tests/Types/UuidBinaryTypeTest.php deleted file mode 100644 index b44a52578c5d2..0000000000000 --- a/src/Symfony/Bridge/Doctrine/Tests/Types/UuidBinaryTypeTest.php +++ /dev/null @@ -1,123 +0,0 @@ - - * - * 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\UuidBinaryType; -use Symfony\Component\Uid\Uuid; - -class UuidBinaryTypeTest extends TestCase -{ - private const DUMMY_UUID = '9f755235-5a2d-4aba-9605-e9962b312e50'; - - private $platform; - - /** @var UuidBinaryType */ - private $type; - - public static function setUpBeforeClass(): void - { - Type::addType('uuid_binary', UuidBinaryType::class); - } - - protected function setUp(): void - { - $this->platform = $this->createMock(AbstractPlatform::class); - $this->platform - ->method('getBinaryTypeDeclarationSQL') - ->willReturn('DUMMYBINARY(16)'); - - $this->type = Type::getType('uuid_binary'); - } - - public function testUuidConvertsToDatabaseValue() - { - $uuid = Uuid::fromString(self::DUMMY_UUID); - - $expected = uuid_parse(self::DUMMY_UUID); - $actual = $this->type->convertToDatabaseValue($uuid, $this->platform); - - $this->assertEquals($expected, $actual); - } - - public function testStringUuidConvertsToDatabaseValue() - { - $uuid = self::DUMMY_UUID; - - $expected = uuid_parse(self::DUMMY_UUID); - $actual = $this->type->convertToDatabaseValue($uuid, $this->platform); - - $this->assertEquals($expected, $actual); - } - - public function testInvalidUuidConversionForDatabaseValue() - { - $this->expectException(ConversionException::class); - - $this->type->convertToDatabaseValue('abcdefg', $this->platform); - } - - public function testNullConversionForDatabaseValue() - { - $this->assertNull($this->type->convertToDatabaseValue(null, $this->platform)); - } - - public function testUuidConvertsToPHPValue() - { - $uuid = $this->type->convertToPHPValue(uuid_parse(self::DUMMY_UUID), $this->platform); - - $this->assertEquals(self::DUMMY_UUID, $uuid->__toString()); - } - - public function testInvalidUuidConversionForPHPValue() - { - $this->expectException(ConversionException::class); - - $this->type->convertToPHPValue('abcdefg', $this->platform); - } - - public function testNotSupportedTypeConversionForDatabaseValue() - { - $this->expectException(ConversionException::class); - - $this->type->convertToDatabaseValue(new \stdClass(), $this->platform); - } - - public function testNullConversionForPHPValue() - { - $this->assertNull($this->type->convertToPHPValue(null, $this->platform)); - } - - public function testReturnValueIfUuidForPHPValue() - { - $uuid = Uuid::v4(); - $this->assertSame($uuid, $this->type->convertToPHPValue($uuid, $this->platform)); - } - - public function testGetName() - { - $this->assertEquals('uuid_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)); - } -} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php b/src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php index da775ca81573c..45e2ee0d5dc71 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php @@ -37,6 +37,9 @@ public static function setUpBeforeClass(): void protected function setUp(): void { $this->platform = $this->createMock(AbstractPlatform::class); + $this->platform + ->method('hasNativeGuidType') + ->willReturn(true); $this->platform ->method('getGuidTypeDeclarationSQL') ->willReturn('DUMMYVARCHAR()'); diff --git a/src/Symfony/Bridge/Doctrine/Types/AbstractBinaryUidType.php b/src/Symfony/Bridge/Doctrine/Types/AbstractBinaryUidType.php deleted file mode 100644 index 1dbb70abf556a..0000000000000 --- a/src/Symfony/Bridge/Doctrine/Types/AbstractBinaryUidType.php +++ /dev/null @@ -1,86 +0,0 @@ - - * - * 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\DBAL\Platforms\AbstractPlatform; -use Doctrine\DBAL\Types\ConversionException; -use Doctrine\DBAL\Types\Type; -use Symfony\Component\Uid\AbstractUid; - -abstract class AbstractBinaryUidType extends Type -{ - abstract protected function getUidClass(): string; - - public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string - { - return $platform->getBinaryTypeDeclarationSQL([ - 'length' => '16', - 'fixed' => true, - ]); - } - - /** - * {@inheritdoc} - * - * @throws ConversionException - */ - public function convertToPHPValue($value, AbstractPlatform $platform): ?AbstractUid - { - if ($value instanceof AbstractUid || null === $value) { - return $value; - } - - if (!\is_string($value)) { - throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', AbstractUid::class]); - } - - try { - return $this->getUidClass()::fromString($value); - } catch (\InvalidArgumentException $e) { - throw ConversionException::conversionFailed($value, $this->getName(), $e); - } - } - - /** - * {@inheritdoc} - * - * @throws ConversionException - */ - public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string - { - if ($value instanceof AbstractUid) { - return $value->toBinary(); - } - - if (null === $value || '' === $value) { - return null; - } - - if (!\is_string($value)) { - throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', AbstractUid::class]); - } - - try { - return $this->getUidClass()::fromString($value)->toBinary(); - } catch (\InvalidArgumentException $e) { - throw ConversionException::conversionFailed($value, $this->getName()); - } - } - - /** - * {@inheritdoc} - */ - public function requiresSQLCommentHint(AbstractPlatform $platform): bool - { - return true; - } -} diff --git a/src/Symfony/Bridge/Doctrine/Types/AbstractUidType.php b/src/Symfony/Bridge/Doctrine/Types/AbstractUidType.php index 4b57bc6b852f6..ec0c6ef6f8078 100644 --- a/src/Symfony/Bridge/Doctrine/Types/AbstractUidType.php +++ b/src/Symfony/Bridge/Doctrine/Types/AbstractUidType.php @@ -25,7 +25,14 @@ abstract protected function getUidClass(): string; */ public function getSQLDeclaration(array $column, AbstractPlatform $platform): string { - return $platform->getGuidTypeDeclarationSQL($column); + if ($platform->hasNativeGuidType()) { + return $platform->getGuidTypeDeclarationSQL($column); + } + + return $platform->getBinaryTypeDeclarationSQL([ + 'length' => '16', + 'fixed' => true, + ]); } /** @@ -57,8 +64,10 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?Abstract */ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string { + $toString = $platform->hasNativeGuidType() ? 'toRfc4122' : 'toBinary'; + if ($value instanceof AbstractUid) { - return $value->toRfc4122(); + return $value->$toString(); } if (null === $value || '' === $value) { @@ -70,7 +79,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str } try { - return $this->getUidClass()::fromString($value)->toRfc4122(); + return $this->getUidClass()::fromString($value)->$toString(); } catch (\InvalidArgumentException $e) { throw ConversionException::conversionFailed($value, $this->getName()); } diff --git a/src/Symfony/Bridge/Doctrine/Types/UlidBinaryType.php b/src/Symfony/Bridge/Doctrine/Types/UlidBinaryType.php deleted file mode 100644 index 34077d24494e0..0000000000000 --- a/src/Symfony/Bridge/Doctrine/Types/UlidBinaryType.php +++ /dev/null @@ -1,27 +0,0 @@ - - * - * 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 Symfony\Component\Uid\Ulid; - -final class UlidBinaryType extends AbstractBinaryUidType -{ - public function getName(): string - { - return 'ulid_binary'; - } - - protected function getUidClass(): string - { - return Ulid::class; - } -} diff --git a/src/Symfony/Bridge/Doctrine/Types/UuidBinaryType.php b/src/Symfony/Bridge/Doctrine/Types/UuidBinaryType.php deleted file mode 100644 index 9e161a8ccba76..0000000000000 --- a/src/Symfony/Bridge/Doctrine/Types/UuidBinaryType.php +++ /dev/null @@ -1,27 +0,0 @@ - - * - * 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 Symfony\Component\Uid\Uuid; - -final class UuidBinaryType extends AbstractBinaryUidType -{ - public function getName(): string - { - return 'uuid_binary'; - } - - protected function getUidClass(): string - { - return Uuid::class; - } -}