Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DoctrineBridge] Avoid calling AbstractPlatform::hasNativeGuidType() #46985

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
Jul 20, 2022
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
55 changes: 31 additions & 24 deletions 55 src/Symfony/Bridge/Doctrine/Tests/Types/UlidTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@
namespace Symfony\Bridge\Doctrine\Tests\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Types\UlidType;
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\Ulid;

// DBAL 2 compatibility
class_exists('Doctrine\DBAL\Platforms\PostgreSqlPlatform');

final class UlidTypeTest extends TestCase
{
private const DUMMY_ULID = '01EEDQEK6ZAZE93J8KG5B4MBJC';

/** @var AbstractPlatform */
private $platform;

/** @var UlidType */
private $type;

Expand All @@ -40,14 +43,6 @@ public static function setUpBeforeClass(): void

protected function setUp(): void
{
$this->platform = $this->createMock(AbstractPlatform::class);
$this->platform
->method('hasNativeGuidType')
->willReturn(true);
$this->platform
->method('getGuidTypeDeclarationSQL')
->willReturn('DUMMYVARCHAR()');

$this->type = Type::getType('ulid');
}

Expand All @@ -56,7 +51,7 @@ public function testUlidConvertsToDatabaseValue()
$ulid = Ulid::fromString(self::DUMMY_ULID);

$expected = $ulid->toRfc4122();
$actual = $this->type->convertToDatabaseValue($ulid, $this->platform);
$actual = $this->type->convertToDatabaseValue($ulid, new PostgreSQLPlatform());

$this->assertEquals($expected, $actual);
}
Expand All @@ -70,14 +65,14 @@ public function testUlidInterfaceConvertsToDatabaseValue()
->method('toRfc4122')
->willReturn('foo');

$actual = $this->type->convertToDatabaseValue($ulid, $this->platform);
$actual = $this->type->convertToDatabaseValue($ulid, new PostgreSQLPlatform());

$this->assertEquals('foo', $actual);
}

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

$expected = $ulid->toRfc4122();
Expand All @@ -89,25 +84,25 @@ public function testNotSupportedTypeConversionForDatabaseValue()
{
$this->expectException(ConversionException::class);

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

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

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

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

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

$this->assertInstanceOf(Ulid::class, $ulid);
$this->assertEquals(self::DUMMY_ULID, $ulid->__toString());
Expand All @@ -117,33 +112,45 @@ public function testInvalidUlidConversionForPHPValue()
{
$this->expectException(ConversionException::class);

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

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

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

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

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

public function testGetGuidTypeDeclarationSQL()
/**
* @dataProvider provideSqlDeclarations
*/
public function testGetGuidTypeDeclarationSQL(AbstractPlatform $platform, string $expectedDeclaration)
{
$this->assertEquals($expectedDeclaration, $this->type->getSqlDeclaration(['length' => 36], $platform));
}

public function provideSqlDeclarations(): array
{
$this->assertEquals('DUMMYVARCHAR()', $this->type->getSqlDeclaration(['length' => 36], $this->platform));
return [
[new PostgreSQLPlatform(), 'UUID'],
[new SqlitePlatform(), 'BLOB'],
[new MySQLPlatform(), 'BINARY(16)'],
];
}

public function testRequiresSQLCommentHint()
{
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
$this->assertTrue($this->type->requiresSQLCommentHint(new SqlitePlatform()));
}
}
72 changes: 47 additions & 25 deletions 72 src/Symfony/Bridge/Doctrine/Tests/Types/UuidTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@
namespace Symfony\Bridge\Doctrine\Tests\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\Uuid;

// DBAL 2 compatibility
class_exists('Doctrine\DBAL\Platforms\MySqlPlatform');
class_exists('Doctrine\DBAL\Platforms\PostgreSqlPlatform');

final class UuidTypeTest extends TestCase
{
private const DUMMY_UUID = '9f755235-5a2d-4aba-9605-e9962b312e50';

/** @var AbstractPlatform */
private $platform;

/** @var UuidType */
private $type;

Expand All @@ -40,14 +44,6 @@ public static function setUpBeforeClass(): void

protected function setUp(): void
{
$this->platform = $this->createMock(AbstractPlatform::class);
$this->platform
->method('hasNativeGuidType')
->willReturn(true);
$this->platform
->method('getGuidTypeDeclarationSQL')
->willReturn('DUMMYVARCHAR()');

$this->type = Type::getType('uuid');
}

Expand All @@ -56,12 +52,12 @@ public function testUuidConvertsToDatabaseValue()
$uuid = Uuid::fromString(self::DUMMY_UUID);

$expected = $uuid->__toString();
$actual = $this->type->convertToDatabaseValue($uuid, $this->platform);
$actual = $this->type->convertToDatabaseValue($uuid, new PostgreSQLPlatform());

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

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

Expand All @@ -70,14 +66,28 @@ public function testUuidInterfaceConvertsToDatabaseValue()
->method('toRfc4122')
->willReturn('foo');

$actual = $this->type->convertToDatabaseValue($uuid, $this->platform);
$actual = $this->type->convertToDatabaseValue($uuid, new PostgreSQLPlatform());

$this->assertEquals('foo', $actual);
}

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

$uuid
->expects($this->once())
->method('toBinary')
->willReturn('foo');

$actual = $this->type->convertToDatabaseValue($uuid, new MySQLPlatform());

$this->assertEquals('foo', $actual);
}

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

$this->assertEquals(self::DUMMY_UUID, $actual);
}
Expand All @@ -86,25 +96,25 @@ public function testNotSupportedTypeConversionForDatabaseValue()
{
$this->expectException(ConversionException::class);

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

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

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

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

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

$this->assertInstanceOf(Uuid::class, $uuid);
$this->assertEquals(self::DUMMY_UUID, $uuid->__toString());
Expand All @@ -114,33 +124,45 @@ public function testInvalidUuidConversionForPHPValue()
{
$this->expectException(ConversionException::class);

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

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

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

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

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

public function testGetGuidTypeDeclarationSQL()
/**
* @dataProvider provideSqlDeclarations
*/
public function testGetGuidTypeDeclarationSQL(AbstractPlatform $platform, string $expectedDeclaration)
{
$this->assertEquals($expectedDeclaration, $this->type->getSqlDeclaration(['length' => 36], $platform));
}

public function provideSqlDeclarations(): array
{
$this->assertEquals('DUMMYVARCHAR()', $this->type->getSqlDeclaration(['length' => 36], $this->platform));
return [
[new PostgreSQLPlatform(), 'UUID'],
[new SqlitePlatform(), 'BLOB'],
[new MySQLPlatform(), 'BINARY(16)'],
];
}

public function testRequiresSQLCommentHint()
{
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
$this->assertTrue($this->type->requiresSQLCommentHint(new SqlitePlatform()));
}
}
17 changes: 15 additions & 2 deletions 17 src/Symfony/Bridge/Doctrine/Types/AbstractUidType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@

abstract class AbstractUidType extends Type
{
/**
* @return class-string<AbstractUid>
*/
abstract protected function getUidClass(): string;

/**
* {@inheritdoc}
*/
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
if ($platform->hasNativeGuidType()) {
if ($this->hasNativeGuidType($platform)) {
return $platform->getGuidTypeDeclarationSQL($column);
}

Expand Down Expand Up @@ -64,7 +67,7 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?Abstract
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
$toString = $platform->hasNativeGuidType() ? 'toRfc4122' : 'toBinary';
$toString = $this->hasNativeGuidType($platform) ? 'toRfc4122' : 'toBinary';

if ($value instanceof AbstractUid) {
return $value->$toString();
Expand Down Expand Up @@ -92,4 +95,14 @@ public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}

private function hasNativeGuidType(AbstractPlatform $platform): bool
fabpot marked this conversation as resolved.
Show resolved Hide resolved
{
// Compatibility with DBAL < 3.4
$method = \method_exists($platform, 'getStringTypeDeclarationSQL')
? 'getStringTypeDeclarationSQL'
: 'getVarcharTypeDeclarationSQL';

return $platform->getGuidTypeDeclarationSQL([]) !== $platform->$method(['fixed' => true, 'length' => 36]);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.