Skip to content

Commit dc0d45d

Browse files
committed
bug #38986 [DoctrineBridge] accept converting Uid-as-strings to db-values (nicolas-grekas)
This PR was merged into the 5.2-dev branch. Discussion ---------- [DoctrineBridge] accept converting Uid-as-strings to db-values | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #38929 | License | MIT | Doc PR | - In #38605 I made Uid types stricter by taking inspiration from native Doctrine types. But #38929 (comment) made me realize this doesn't work with ParamConverters. Here is the fix. Commits ------- 20714d6 [DoctrineBridge] accept converting Uid-as-strings to db-values
2 parents 57e39b4 + 20714d6 commit dc0d45d

File tree

6 files changed

+45
-15
lines changed

6 files changed

+45
-15
lines changed

src/Symfony/Bridge/Doctrine/Tests/Types/UlidBinaryTypeTest.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ public function testUlidConvertsToDatabaseValue()
5252
$this->assertEquals($expected, $actual);
5353
}
5454

55-
public function testNotSupportedStringUlidConversionToDatabaseValue()
55+
public function testStringUlidConvertsToDatabaseValue()
5656
{
57-
$this->expectException(ConversionException::class);
57+
$expected = Ulid::fromString(self::DUMMY_ULID)->toBinary();
58+
$actual = $this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);
5859

59-
$this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);
60+
$this->assertEquals($expected, $actual);
6061
}
6162

6263
public function testNotSupportedTypeConversionForDatabaseValue()

src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,14 @@ public function testUlidInterfaceConvertsToDatabaseValue()
6868
$this->assertEquals('foo', $actual);
6969
}
7070

71-
public function testNotSupportedUlidStringConversionToDatabaseValue()
71+
public function testUlidStringConvertsToDatabaseValue()
7272
{
73-
$this->expectException(ConversionException::class);
73+
$actual = $this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);
74+
$ulid = Ulid::fromString(self::DUMMY_ULID);
7475

75-
$this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);
76+
$expected = $ulid->toRfc4122();
77+
78+
$this->assertEquals($expected, $actual);
7679
}
7780

7881
public function testNotSupportedTypeConversionForDatabaseValue()

src/Symfony/Bridge/Doctrine/Tests/Types/UuidBinaryTypeTest.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,21 @@ public function testUuidConvertsToDatabaseValue()
5252
$this->assertEquals($expected, $actual);
5353
}
5454

55-
public function testNotSupportedStringUuidConversionToDatabaseValue()
55+
public function testStringUuidConvertsToDatabaseValue()
56+
{
57+
$uuid = self::DUMMY_UUID;
58+
59+
$expected = uuid_parse(self::DUMMY_UUID);
60+
$actual = $this->type->convertToDatabaseValue($uuid, $this->platform);
61+
62+
$this->assertEquals($expected, $actual);
63+
}
64+
65+
public function testInvalidUuidConversionForDatabaseValue()
5666
{
5767
$this->expectException(ConversionException::class);
5868

59-
$this->type->convertToDatabaseValue(self::DUMMY_UUID, $this->platform);
69+
$this->type->convertToDatabaseValue('abcdefg', $this->platform);
6070
}
6171

6272
public function testNullConversionForDatabaseValue()

src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ public function testUuidInterfaceConvertsToDatabaseValue()
6868
$this->assertEquals('foo', $actual);
6969
}
7070

71-
public function testNotSupportedUuidStringConversionToDatabaseValue()
71+
public function testUuidStringConvertsToDatabaseValue()
7272
{
73-
$this->expectException(ConversionException::class);
73+
$actual = $this->type->convertToDatabaseValue(self::DUMMY_UUID, $this->platform);
7474

75-
$this->type->convertToDatabaseValue(self::DUMMY_UUID, $this->platform);
75+
$this->assertEquals(self::DUMMY_UUID, $actual);
7676
}
7777

7878
public function testNotSupportedTypeConversionForDatabaseValue()

src/Symfony/Bridge/Doctrine/Types/AbstractBinaryUidType.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,19 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
6161
return $value->toBinary();
6262
}
6363

64-
if (null === $value) {
64+
if (null === $value || '' === $value) {
6565
return null;
6666
}
6767

68-
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', AbstractUid::class]);
68+
if (!\is_string($value)) {
69+
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', AbstractUid::class]);
70+
}
71+
72+
try {
73+
return $this->getUidClass()::fromString($value)->toBinary();
74+
} catch (\InvalidArgumentException $e) {
75+
throw ConversionException::conversionFailed($value, $this->getName());
76+
}
6977
}
7078

7179
/**

src/Symfony/Bridge/Doctrine/Types/AbstractUidType.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,19 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
6161
return $value->toRfc4122();
6262
}
6363

64-
if (null === $value) {
64+
if (null === $value || '' === $value) {
6565
return null;
6666
}
6767

68-
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', AbstractUid::class]);
68+
if (!\is_string($value)) {
69+
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', AbstractUid::class]);
70+
}
71+
72+
try {
73+
return $this->getUidClass()::fromString($value)->toRfc4122();
74+
} catch (\InvalidArgumentException $e) {
75+
throw ConversionException::conversionFailed($value, $this->getName());
76+
}
6977
}
7078

7179
/**

0 commit comments

Comments
 (0)