From 20714d66c9187498836911fef2105c16cfee0bcf Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 4 Nov 2020 12:11:20 +0100 Subject: [PATCH] [DoctrineBridge] accept converting Uid-as-strings to db-values --- .../Doctrine/Tests/Types/UlidBinaryTypeTest.php | 7 ++++--- .../Bridge/Doctrine/Tests/Types/UlidTypeTest.php | 9 ++++++--- .../Doctrine/Tests/Types/UuidBinaryTypeTest.php | 14 ++++++++++++-- .../Bridge/Doctrine/Tests/Types/UuidTypeTest.php | 6 +++--- .../Doctrine/Types/AbstractBinaryUidType.php | 12 ++++++++++-- .../Bridge/Doctrine/Types/AbstractUidType.php | 12 ++++++++++-- 6 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/Tests/Types/UlidBinaryTypeTest.php b/src/Symfony/Bridge/Doctrine/Tests/Types/UlidBinaryTypeTest.php index 8ea165b497..4141dfa55e 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Types/UlidBinaryTypeTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Types/UlidBinaryTypeTest.php @@ -52,11 +52,12 @@ class UlidBinaryTypeTest extends TestCase $this->assertEquals($expected, $actual); } - public function testNotSupportedStringUlidConversionToDatabaseValue() + public function testStringUlidConvertsToDatabaseValue() { - $this->expectException(ConversionException::class); + $expected = Ulid::fromString(self::DUMMY_ULID)->toBinary(); + $actual = $this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform); - $this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform); + $this->assertEquals($expected, $actual); } public function testNotSupportedTypeConversionForDatabaseValue() diff --git a/src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php b/src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php index 43715bb232..f3969bcb4c 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php @@ -68,11 +68,14 @@ final class UlidTypeTest extends TestCase $this->assertEquals('foo', $actual); } - public function testNotSupportedUlidStringConversionToDatabaseValue() + public function testUlidStringConvertsToDatabaseValue() { - $this->expectException(ConversionException::class); + $actual = $this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform); + $ulid = Ulid::fromString(self::DUMMY_ULID); - $this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform); + $expected = $ulid->toRfc4122(); + + $this->assertEquals($expected, $actual); } public function testNotSupportedTypeConversionForDatabaseValue() diff --git a/src/Symfony/Bridge/Doctrine/Tests/Types/UuidBinaryTypeTest.php b/src/Symfony/Bridge/Doctrine/Tests/Types/UuidBinaryTypeTest.php index 7d38606cf9..b44a52578c 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Types/UuidBinaryTypeTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Types/UuidBinaryTypeTest.php @@ -52,11 +52,21 @@ class UuidBinaryTypeTest extends TestCase $this->assertEquals($expected, $actual); } - public function testNotSupportedStringUuidConversionToDatabaseValue() + 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(self::DUMMY_UUID, $this->platform); + $this->type->convertToDatabaseValue('abcdefg', $this->platform); } public function testNullConversionForDatabaseValue() diff --git a/src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php b/src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php index c5c6658dad..da775ca815 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php @@ -68,11 +68,11 @@ final class UuidTypeTest extends TestCase $this->assertEquals('foo', $actual); } - public function testNotSupportedUuidStringConversionToDatabaseValue() + public function testUuidStringConvertsToDatabaseValue() { - $this->expectException(ConversionException::class); + $actual = $this->type->convertToDatabaseValue(self::DUMMY_UUID, $this->platform); - $this->type->convertToDatabaseValue(self::DUMMY_UUID, $this->platform); + $this->assertEquals(self::DUMMY_UUID, $actual); } public function testNotSupportedTypeConversionForDatabaseValue() diff --git a/src/Symfony/Bridge/Doctrine/Types/AbstractBinaryUidType.php b/src/Symfony/Bridge/Doctrine/Types/AbstractBinaryUidType.php index 9321d148ce..1dbb70abf5 100644 --- a/src/Symfony/Bridge/Doctrine/Types/AbstractBinaryUidType.php +++ b/src/Symfony/Bridge/Doctrine/Types/AbstractBinaryUidType.php @@ -61,11 +61,19 @@ abstract class AbstractBinaryUidType extends Type return $value->toBinary(); } - if (null === $value) { + if (null === $value || '' === $value) { return null; } - throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', AbstractUid::class]); + 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()); + } } /** diff --git a/src/Symfony/Bridge/Doctrine/Types/AbstractUidType.php b/src/Symfony/Bridge/Doctrine/Types/AbstractUidType.php index a14eb853e3..4b57bc6b85 100644 --- a/src/Symfony/Bridge/Doctrine/Types/AbstractUidType.php +++ b/src/Symfony/Bridge/Doctrine/Types/AbstractUidType.php @@ -61,11 +61,19 @@ abstract class AbstractUidType extends Type return $value->toRfc4122(); } - if (null === $value) { + if (null === $value || '' === $value) { return null; } - throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', AbstractUid::class]); + if (!\is_string($value)) { + throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', AbstractUid::class]); + } + + try { + return $this->getUidClass()::fromString($value)->toRfc4122(); + } catch (\InvalidArgumentException $e) { + throw ConversionException::conversionFailed($value, $this->getName()); + } } /**