8000 [DoctrinBridge] make Uid types stricter · symfony/symfony@ba31d0e · GitHub
[go: up one dir, main page]

Skip to content

Commit ba31d0e

Browse files
[DoctrinBridge] make Uid types stricter
1 parent 5ca43b8 commit ba31d0e

File tree

6 files changed

+74
-121
lines changed

6 files changed

+74
-121
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,18 @@ public function testUlidConvertsToDatabaseValue()
5252
$this->assertEquals($expected, $actual);
5353
}
5454

55-
public function testStringUlidConvertsToDatabaseValue()
56-
{
57-
$expected = Ulid::fromString(self::DUMMY_ULID)->toBinary();
58-
$actual = $this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);
59-
60-
$this->assertEquals($expected, $actual);
61-
}
62-
63-
public function testInvalidUlidConversionForDatabaseValue()
55+
public function testNotSupportedStringUlidConversionToDatabaseValue()
6456
{
6557
$this->expectException(ConversionException::class);
6658

67-
$this->type->convertToDatabaseValue('abcdefg', $this->platform);
59+
$this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);
6860
}
6961

7062
public function testNotSupportedTypeConversionForDatabaseValue()
7163
{
72-
$this->assertNull($this->type->convertToDatabaseValue(new \stdClass(), $this->platform));
64+
$this->expectException(ConversionException::class);
65+
66+
$this->type->convertToDatabaseValue(new \stdClass(), $this->platform);
7367
}
7468

7569
public function testNullConversionForDatabaseValue()

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

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected function setUp(): void
4444
$this->type = Type::getType('ulid');
4545
}
4646

47-
public function testUlidConvertsToDatabaseValue(): void
47+
public function testUlidConvertsToDatabaseValue()
4848
{
4949
$ulid = Ulid::fromString(self::DUMMY_ULID);
5050

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

57-
public function testUlidInterfaceConvertsToDatabaseValue(): void
57+
public function testUlidInterfaceConvertsToDatabaseValue()
5858
{
5959
$ulid = $this->createMock(AbstractUid::class);
6060

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

71-
public function testUlidStringConvertsToDatabaseValue(): void
72-
{
73-
$actual = $this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);
74-
$ulid = Ulid::fromString(self::DUMMY_ULID);
75-
76-
$expected = $ulid->toRfc4122();
77-
78-
$this->assertEquals($expected, $actual);
79-
}
80-
81-
public function testInvalidUlidConversionForDatabaseValue(): void
71+
public function testNotSupportedUlidStringConversionToDatabaseValue()
8272
{
8373
$this->expectException(ConversionException::class);
8474

85-
$this->type->convertToDatabaseValue('abcdefg', $this->platform);
75+
$this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);
8676
}
8777

8878
public function testNotSupportedTypeConversionForDatabaseValue()
8979
{
90-
$this->assertNull($this->type->convertToDatabaseValue(new \stdClass(), $this->platform));
80+
$this->expectException(ConversionException::class);
81+
82+
$this->type->convertToDatabaseValue(new \stdClass(), $this->platform);
9183
}
9284

93-
public function testNullConversionForDatabaseValue(): void
85+
public function testNullConversionForDatabaseValue()
9486
{
9587
$this->assertNull($this->type->convertToDatabaseValue(null, $this->platform));
9688
}
9789

98-
public function testUlidInterfaceConvertsToPHPValue(): void
90+
public function testUlidInterfaceConvertsToPHPValue()
9991
{
10092
$ulid = $this->createMock(AbstractUid::class);
10193
$actual = $this->type->convertToPHPValue($ulid, $this->platform);
10294

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

106-
public function testUlidConvertsToPHPValue(): void
98+
public function testUlidConvertsToPHPValue()
10799
{
108100
$ulid = $this->type->convertToPHPValue(self::DUMMY_ULID, $this->platform);
109101

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

114-
public function testInvalidUlidConversionForPHPValue(): void
106+
public function testInvalidUlidConversionForPHPValue()
115107
{
116108
$this->expectException(ConversionException::class);
117109

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

121-
public function testNullConversionForPHPValue(): void
113+
public function testNullConversionForPHPValue()
122114
{
123115
$this->assertNull($this->type->convertToPHPValue(null, $this->platform));
124116
}
125117

126-
public function testReturnValueIfUlidForPHPValue(): void
118+
public function testReturnValueIfUlidForPHPValue()
127119
{
128120
$ulid = new Ulid();
129121

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

133-
public function testGetName(): void
125+
public function testGetName()
134126
{
135127
$this->assertEquals('ulid', $this->type->getName());
136128
}
137129

138-
public function testGetGuidTypeDeclarationSQL(): void
130+
public function testGetGuidTypeDeclarationSQL()
139131
{
140132
$this->assertEquals('DUMMYVARCHAR()', $this->type->getSqlDeclaration(['length' => 36], $this->platform));
141133
}
142134

143-
public function testRequiresSQLCommentHint(): void
135+
public function testRequiresSQLCommentHint()
144136
{
145137
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
146138
}

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,11 @@ public function testUuidConvertsToDatabaseValue()
5252
$this->assertEquals($expected, $actual);
5353
}
5454

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

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

7262
public function testNullConversionForDatabaseValue()
@@ -90,7 +80,9 @@ public function testInvalidUuidConversionForPHPValue()
9080

9181
public function testNotSupportedTypeConversionForDatabaseValue()
9282
{
93-
$this->assertNull($this->type->convertToDatabaseValue(new \stdClass(), $this->platform));
83+
$this->expectException(ConversionException::class);
84+
85+
$this->type->convertToDatabaseValue(new \stdClass(), $this->platform);
9486
}
9587

9688
public function testNullConversionForPHPValue()

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected function setUp(): void
4444
$this->type = Type::getType('uuid');
4545
}
4646

47-
public function testUuidConvertsToDatabaseValue(): void
47+
public function testUuidConvertsToDatabaseValue()
4848
{
4949
$uuid = Uuid::fromString(self::DUMMY_UUID);
5050

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

57-
public function testUuidInterfaceConvertsToDatabaseValue(): void
57+
public function testUuidInterfaceConvertsToDatabaseValue()
5858
{
5959
$uuid = $this->createMock(AbstractUid::class);
6060

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

71-
public function testUuidStringConvertsToDatabaseValue(): void
72-
{
73-
$actual = $this->type->convertToDatabaseValue(self::DUMMY_UUID, $this->platform);
74-
75-
$this->assertEquals(self::DUMMY_UUID, $actual);
76-
}
77-
78-
public function testInvalidUuidConversionForDatabaseValue(): void
71+
public function testNotSupportedUuidStringConversionToDatabaseValue()
7972
{
8073
$this->expectException(ConversionException::class);
8174

82-
$this->type->convertToDatabaseValue('abcdefg', $this->platform);
75+
$this->type->convertToDatabaseValue(self::DUMMY_UUID, $this->platform);
8376
}
8477

8578
public function testNotSupportedTypeConversionForDatabaseValue()
8679
{
87-
$this->assertNull($this->type->convertToDatabaseValue(new \stdClass(), $this->platform));
80+
$this->expectException(ConversionException::class);
81+
82+
$this->type->convertToDatabaseValue(new \stdClass(), $this->platform);
8883
}
8984

90-
public function testNullConversionForDatabaseValue(): void
85+
public function testNullConversionForDatabaseValue()
9186
{
9287
$this->assertNull($this->type->convertToDatabaseValue(null, $this->platform));
9388
}
9489

95-
public function testUuidInterfaceConvertsToPHPValue(): void
90+
public function testUuidInterfaceConvertsToPHPValue()
9691
{
9792
$uuid = $this->createMock(AbstractUid::class);
9893
$actual = $this->type->convertToPHPValue($uuid, $this->platform);
9994

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

103-
public function testUuidConvertsToPHPValue(): void
98+
public function testUuidConvertsToPHPValue()
10499
{
105100
$uuid = $this->type->convertToPHPValue(self::DUMMY_UUID, $this->platform);
106101

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

111-
public function testInvalidUuidConversionForPHPValue(): void
106+
public function testInvalidUuidConversionForPHPValue()
112107
{
113108
$this->expectException(ConversionException::class);
114109

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

118-
public function testNullConversionForPHPValue(): void
113+
public function testNullConversionForPHPValue()
119114
{
120115
$this->assertNull($this->type->convertToPHPValue(null, $this->platform));
121116
}
122117

123-
public function testReturnValueIfUuidForPHPValue(): void
118+
public function testReturnValueIfUuidForPHPValue()
124119
{
125120
$uuid = Uuid::v4();
126121

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

130-
public function testGetName(): void
125+
public function testGetName()
131126
{
132127
$this->assertEquals('uuid', $this->type->getName());
133128
}
134129

135-
public function testGetGuidTypeDeclarationSQL(): void
130+
public function testGetGuidTypeDeclarationSQL()
136131
{
137132
$this->assertEquals('DUMMYVARCHAR()', $this->type->getSqlDeclaration(['length' => 36], $this->platform));
138133
}
139134

140-
public function testRequiresSQLCommentHint(): void
135+
public function testRequiresSQLCommentHint()
141136
{
142137
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
143138
}

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

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,19 @@
1313

1414
use Doctrine\DBAL\Platforms\AbstractPlatform;
1515
use Doctrine\DBAL\Types\ConversionException;
16-
use Doctrine\DBAL\Types\GuidType;
16+
use Doctrine\DBAL\Types\Type;
1717
use Symfony\Component\Uid\AbstractUid;
1818

19-
abstract class AbstractBinaryUidType extends GuidType
19+
abstract class AbstractBinaryUidType extends Type
2020
{
2121
abstract protected function getUidClass(): string;
2222

2323
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
2424
{
25-
return $platform->getBinaryTypeDeclarationSQL(
26-
[
27-
'length' => '16',
28-
'fixed' => true,
29-
]
30-
);
25+
return $platform->getBinaryTypeDeclarationSQL([
26+
'length' => '16',
27+
'fixed' => true,
28+
]);
3129
}
3230

3331
/**
@@ -37,21 +35,19 @@ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $pla
3735
*/
3836
public function convertToPHPValue($value, AbstractPlatform $platform): ?AbstractUid
3937
{
40-
if (null === $value || '' === $value) {
41-
return null;
38+
if ($value instanceof AbstractUid || null === $value) {
39+
return $value;
4240
}
4341

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

4846
try {
49-
$uuid = $this->getUidClass()::fromString($value);
47+
return $this->getUidClass()::fromString($value);
5048
} catch (\InvalidArgumentException $e) {
51-
throw ConversionException::conversionFailed($value, $this->getName());
49+
throw ConversionException::conversionFailed($value, $this->getName(), $e);
5250
}
53-
54-
return $uuid;
5551
}
5652

5753
/**
@@ -61,23 +57,15 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?Abstract
6157
*/
6258
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
6359
{
64-
if (null === $value || '' === $value) {
65-
return null;
66-
}
67-
6860
if ($value instanceof AbstractUid) {
6961
return $value->toBinary();
7062
}
7163

72-
if (!\is_string($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
64+
if (null === $value) {
7365
return null;
7466
}
7567

76-
try {
77-
return $this->getUidClass()::fromString((string) $value)->toBinary();
78-
} catch (\InvalidArgumentException $e) {
79-
throw ConversionException::conversionFailed($value, $this->getName());
80-
}
68+
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', AbstractUid::class]);
8169
}
8270

8371
/**

0 commit comments

Comments
 (0)
0