diff --git a/src/Symfony/Bridge/Doctrine/Tests/Types/UlidBinaryTypeTest.php b/src/Symfony/Bridge/Doctrine/Tests/Types/UlidBinaryTypeTest.php index 8ea165b49769a..4141dfa55e540 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Types/UlidBinaryTypeTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Types/UlidBinaryTypeTest.php @@ -52,11 +52,12 @@ public function testUlidConvertsToDatabaseValue() $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 43715bb2324a1..f3969bcb4c725 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php @@ -68,11 +68,14 @@ public function testUlidInterfaceConvertsToDatabaseValue() $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 7d38606cf9054..b44a52578c5d2 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Types/UuidBinaryTypeTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Types/UuidBinaryTypeTest.php @@ -52,11 +52,21 @@ public function testUuidConvertsToDatabaseValue() $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 c5c6658dad7bd..da775ca81573c 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php @@ -68,11 +68,11 @@ public function testUuidInterfaceConvertsToDatabaseValue() $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 9321d148ce3be..1dbb70abf556a 100644 --- a/src/Symfony/Bridge/Doctrine/Types/AbstractBinaryUidType.php +++ b/src/Symfony/Bridge/Doctrine/Types/AbstractBinaryUidType.php @@ -61,11 +61,19 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str 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 a14eb853e3868..4b57bc6b852f6 100644 --- a/src/Symfony/Bridge/Doctrine/Types/AbstractUidType.php +++ b/src/Symfony/Bridge/Doctrine/Types/AbstractUidType.php @@ -61,11 +61,19 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str 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()); + } } /**