Skip to content

[DoctrineBridge] accept converting Uid-as-strings to db-values #38986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 12 additions & 2 deletions src/Symfony/Bridge/Doctrine/Tests/Types/UuidBinaryTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 10 additions & 2 deletions src/Symfony/Bridge/Doctrine/Types/AbstractBinaryUidType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/Symfony/Bridge/Doctrine/Types/AbstractUidType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

/**
Expand Down