8000 [DoctrinBridge] make Uid types stricter by nicolas-grekas · Pull Request #38605 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DoctrinBridge] make Uid types stricter 8000 #38605

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
Oct 17, 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
16 changes: 5 additions & 11 deletions src/Symfony/Bridge/Doctrine/Tests/Types/UlidBinaryTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,18 @@ public function testUlidConvertsToDatabaseValue()
$this->assertEquals($expected, $actual);
}

public function testStringUlidConvertsToDatabaseValue()
{
$expected = Ulid::fromString(self::DUMMY_ULID)->toBinary();
$actual = $this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);

$this->assertEquals($expected, $actual);
}

public function testInvalidUlidConversionForDatabaseValue()
public function testNotSupportedStringUlidConversionToDatabaseValue()
{
$this->expectException(ConversionException::class);

$this->type->convertToDatabaseValue('abcdefg', $this->platform);
$this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);
}

public function testNotSupportedTypeConversionForDatabaseValue()
{
$this->assertNull($this->type->convertToDatabaseValue(new \stdClass(), $this->platform));
$this->expectException(ConversionException::class);

$this->type->convertToDatabaseValue(new \stdClass(), $this->platform);
}

public function testNullConversionForDatabaseValue()
Expand Down
40 changes: 16 additions & 24 deletions src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function setUp(): void
$this->type = Type::getType('ulid');
}

public function testUlidConvertsToDatabaseValue(): void
public function testUlidConvertsToDatabaseValue()
{
$ulid = Ulid::fromString(self::DUMMY_ULID);

Expand All @@ -54,7 +54,7 @@ public function testUlidConvertsToDatabaseValue(): void
$this->assertEquals($expected, $actual);
}

public function testUlidInterfaceConvertsToDatabaseValue(): void
public function testUlidInterfaceConvertsToDatabaseValue()
{
$ulid = $this->createMock(AbstractUid::class);

Expand All @@ -68,79 +68,71 @@ public function testUlidInterfaceConvertsToDatabaseValue(): void
$this->assertEquals('foo', $actual);
}

public function testUlidStringConvertsToDatabaseValue(): void
{
$actual = $this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);
$ulid = Ulid::fromString(self::DUMMY_ULID);

$expected = $ulid->toRfc4122();

$this->assertEquals($expected, $actual);
}

public function testInvalidUlidConversionForDatabaseValue(): void
public function testNotSupportedUlidStringConversionToDatabaseValue()
{
$this->expectException(ConversionException::class);

$this->type->convertToDatabaseValue('abcdefg', $this->platform);
$this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);
}

public function testNotSupportedTypeConversionForDatabaseValue()
{
$this->assertNull($this->type->convertToDatabaseValue(new \stdClass(), $this->platform));
$this->expectException(ConversionException::class);

$this->type->convertToDatabaseValue(new \stdClass(), $this->platform);
}

public function testNullConversionForDatabaseValue(): void
public function testNullConversionForDatabaseValue()
{
$this->assertNull($this->type->convertToDatabaseValue(null, $this->platform));
}

public function testUlidInterfaceConvertsToPHPValue(): void
public function testUlidInterfaceConvertsToPHPValue()
{
$ulid = $this->createMock(AbstractUid::class);
$actual = $this->type->convertToPHPValue($ulid, $this->platform);

$this->assertSame($ulid, $actual);
}

public function testUlidConvertsToPHPValue(): void
public function testUlidConvertsToPHPValue()
{
$ulid = $this->type->convertToPHPValue(self::DUMMY_ULID, $this->platform);

$this->assertInstanceOf(Ulid::class, $ulid);
$this->assertEquals(self::DUMMY_ULID, $ulid->__toString());
}

public function testInvalidUlidConversionForPHPValue(): void
public function testInvalidUlidConversionForPHPValue()
{
$this->expectException(ConversionException::class);

$this->type->convertToPHPValue('abcdefg', $this->platform);
}

public function testNullConversionForPHPValue(): void
public function testNullConversionForPHPValue()
{
$this->assertNull($this->type->convertToPHPValue(null, $this->platform));
}

public function testReturnValueIfUlidForPHPValue(): void
public function testReturnValueIfUlidForPHPValue()
{
$ulid = new Ulid();

$this->assertSame($ulid, $this->type->convertToPHPValue($ulid, $this->platform));
}

public function testGetName(): void
public function testGetName()
{
$this->assertEquals('ulid', $this->type->getName());
}

public function testGetGuidTypeDeclarationSQL(): void
public function testGetGuidTypeDeclarationSQL()
{
$this->assertEquals('DUMMYVARCHAR()', $this->type->getSqlDeclaration(['length' => 36], $this->platform));
}

public function testRequiresSQLCommentHint(): void
public function testRequiresSQLCommentHint()
{
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
}
Expand Down
18 changes: 5 additions & 13 deletions src/Symfony/Bridge/Doctrine/Tests/Types/UuidBinaryTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,11 @@ public function testUuidConvertsToDatabaseValue()
$this->assertEquals($expected, $actual);
}

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()
public function testNotSupportedStringUuidConversionToDatabaseValue()
{
$this->expectException(ConversionException::class);

$this->type->convertToDatabaseValue('abcdefg', $this->platform);
$this->type->convertToDatabaseValue(self::DUMMY_UUID, $this->platform);
}

public function testNullConversionForDatabaseValue()
Expand All @@ -90,7 +80,9 @@ public function testInvalidUuidConversionForPHPValue()

public function testNotSupportedTypeConversionForDatabaseValue()
{
$this->assertNull($this->type->convertToDatabaseValue(new \stdClass(), $this->platform));
$this->expectException(ConversionException::class);

$this->type->convertToDatabaseValue(new \stdClass(), $this->platform);
}

public function testNullConversionForPHPValue()
Expand Down
37 changes: 16 additions & 21 deletions src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function setUp(): void
$this->type = Type::getType('uuid');
}

public function testUuidConvertsToDatabaseValue(): void
public function testUuidConvertsToDatabaseValue()
{
$uuid = Uuid::fromString(self::DUMMY_UUID);

Expand All @@ -54,7 +54,7 @@ public function testUuidConvertsToDatabaseValue(): void
$this->assertEquals($expected, $actual);
}

public function testUuidInterfaceConvertsToDatabaseValue(): void
public function testUuidInterfaceConvertsToDatabaseValue()
{
$uuid = $this->createMock(AbstractUid::class);

Expand All @@ -68,76 +68,71 @@ public function testUuidInterfaceConvertsToDatabaseValue(): void
$this->assertEquals('foo', $actual);
}

public function testUuidStringConvertsToDatabaseValue(): void
{
$actual = $this->type->convertToDatabaseValue(self::DUMMY_UUID, $this->platform);

$this->assertEquals(self::DUMMY_UUID, $actual);
}

public function testInvalidUuidConversionForDatabaseValue(): void
public function testNotSupportedUuidStringConversionToDatabaseValue()
{
$this->expectException(ConversionException::class);

$this->type->convertToDatabaseValue('abcdefg', $this->platform);
$this->type->convertToDatabaseValue(self::DUMMY_UUID, $this->platform);
}

public function testNotSupportedTypeConversionForDatabaseValue()
{
$this->assertNull($this->type->convertToDatabaseValue(new \stdClass(), $this->platform));
$this->expectException(ConversionException::class);

$this->type->convertToDatabaseValue(new \stdClass(), $this->platform);
}

public function testNullConversionForDatabaseValue(): void
public function testNullConversionForDatabaseValue()
{
$this->assertNull($this->type->convertToDatabaseValue(null, $this->platform));
}

public function testUuidInterfaceConvertsToPHPValue(): void
public function testUuidInterfaceConvertsToPHPValue()
{
$uuid = $this->createMock(AbstractUid::class);
$actual = $this->type->convertToPHPValue($uuid, $this->platform);

$this->assertSame($uuid, $actual);
}

public function testUuidConvertsToPHPValue(): void
public function testUuidConvertsToPHPValue()
{
$uuid = $this->type->convertToPHPValue(self::DUMMY_UUID, $this->platform);

$this->assertInstanceOf(Uuid::class, $uuid);
$this->assertEquals(self::DUMMY_UUID, $uuid->__toString());
}

public function testInvalidUuidConversionForPHPValue(): void
public function testInvalidUuidConversionForPHPValue()
{
$this->expectException(ConversionException::class);

$this->type->convertToPHPValue('abcdefg', $this->platform);
}

public function testNullConversionForPHPValue(): void
public function testNullConversionForPHPValue()
{
$this->assertNull($this->type->convertToPHPValue(null, $this->platform));
}

public function testReturnValueIfUuidForPHPValue(): void
public function testReturnValueIfUuidForPHPValue()
{
$uuid = Uuid::v4();

$this->assertSame($uuid, $this->type->convertToPHPValue($uuid, $this->platform));
}

public function testGetName(): void
public function testGetName()
{
$this->assertEquals('uuid', $this->type->getName());
}

public function testGetGuidTypeDeclarationSQL(): void
public function testGetGuidTypeDeclarationSQL()
{
$this->assertEquals('DUMMYVARCHAR()', $this->type->getSqlDeclaration(['length' => 36], $this->platform));
}

public function testRequiresSQLCommentHint(): void
public function testRequiresSQLCommentHint()
{
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
}
Expand Down
40 changes: 14 additions & 26 deletions src/Symfony/Bridge/Doctrine/Types/AbstractBinaryUidType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\GuidType;
use Doctrine\DBAL\Types\Type;
use Symfony\Component\Uid\AbstractUid;

abstract class AbstractBinaryUidType extends GuidType
abstract class AbstractBinaryUidType extends Type
{
abstract protected function getUidClass(): string;

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
{
return $platform->getBinaryTypeDeclarationSQL(
[
'length' => '16',
'fixed' => true,
]
);
return $platform->getBinaryTypeDeclarationSQL([
'length' => '16',
'fixed' => true,
]);
}

/**
Expand All @@ -37,21 +35,19 @@ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $pla
*/
public function convertToPHPValue($value, AbstractPlatform $platform): ?AbstractUid
{
if (null === $value || '' === $value) {
return null;
if ($value instanceof AbstractUid || null === $value) {
return $value;
}

if ($value instanceof AbstractUid) {
return $value;
if (!\is_string($value)) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', AbstractUid::class]);
}

try {
$uuid = $this->getUidClass()::fromString($value);
return $this->getUidClass()::fromString($value);
} catch (\InvalidArgumentException $e) {
throw ConversionException::conversionFailed($value, $this->getName());
throw ConversionException::conversionFailed($value, $this->getName(), $e);
}

return $uuid;
}

/**
Expand All @@ -61,23 +57,15 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?Abstract
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if (null === $value || '' === $value) {
return null;
}

if ($value instanceof AbstractUid) {
return $value->toBinary();
}

if (!\is_string($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (null === $value) {
return null;
}

try {
return $this->getUidClass()::fromString((string) $value)->toBinary();
} catch (\InvalidArgumentException $e) {
throw ConversionException::conversionFailed($value, $this->getName());
}
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', AbstractUid::class]);
}

/**
Expand Down
Loading
0