8000 [DoctrineBridge] accept converting Uid-as-strings to db-values · symfony/symfony@20714d6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 20714d6

Browse files
[DoctrineBridge] accept converting Uid-as-strings to db-values
1 parent 57e39b4 commit 20714d6

File tree

6 files changed

+45
-15
lines changed

6 files changed

+45
-15
lines changed

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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 6 additions & 3 deletions
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

Lines changed: 12 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 10 additions & 2 deletions
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

Lines changed: 10 additions & 2 deletions
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)
0