Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitc7c4251

Browse files
[DoctrinBridge] make Uid types stricter
1 parent5ca43b8 commitc7c4251

File tree

6 files changed

+78
-141
lines changed

6 files changed

+78
-141
lines changed

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

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

55-
publicfunctiontestStringUlidConvertsToDatabaseValue()
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-
publicfunctiontestInvalidUlidConversionForDatabaseValue()
55+
publicfunctiontestNotSupportedStringUlidConversionToDatabaseValue()
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
publicfunctiontestNotSupportedTypeConversionForDatabaseValue()
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
publicfunctiontestNullConversionForDatabaseValue()
@@ -114,6 +108,6 @@ public function testGetGuidTypeDeclarationSQL()
114108

115109
publicfunctiontestRequiresSQLCommentHint()
116110
{
117-
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
111+
$this->assertFalse($this->type->requiresSQLCommentHint($this->platform));
118112
}
119113
}

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

Lines changed: 17 additions & 25 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-
publicfunctiontestUlidConvertsToDatabaseValue():void
47+
publicfunctiontestUlidConvertsToDatabaseValue()
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-
publicfunctiontestUlidInterfaceConvertsToDatabaseValue():void
57+
publicfunctiontestUlidInterfaceConvertsToDatabaseValue()
5858
{
5959
$ulid =$this->createMock(AbstractUid::class);
6060

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

71-
publicfunctiontestUlidStringConvertsToDatabaseValue():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-
publicfunctiontestInvalidUlidConversionForDatabaseValue():void
71+
publicfunctiontestNotSupportedUlidStringConversionToDatabaseValue()
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
publicfunctiontestNotSupportedTypeConversionForDatabaseValue()
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-
publicfunctiontestNullConversionForDatabaseValue():void
85+
publicfunctiontestNullConversionForDatabaseValue()
9486
{
9587
$this->assertNull($this->type->convertToDatabaseValue(null,$this->platform));
9688
}
9789

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

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

106-
publicfunctiontestUlidConvertsToPHPValue():void
98+
publicfunctiontestUlidConvertsToPHPValue()
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-
publicfunctiontestInvalidUlidConversionForPHPValue():void
106+
publicfunctiontestInvalidUlidConversionForPHPValue()
115107
{
116108
$this->expectException(ConversionException::class);
117109

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

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

126-
publicfunctiontestReturnValueIfUlidForPHPValue():void
118+
publicfunctiontestReturnValueIfUlidForPHPValue()
127119
{
128120
$ulid =newUlid();
129121

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

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

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

143-
publicfunctiontestRequiresSQLCommentHint():void
135+
publicfunctiontestRequiresSQLCommentHint()
144136
{
145-
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
137+
$this->assertFalse($this->type->requiresSQLCommentHint($this->platform));
146138
}
147139
}

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

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

55-
publicfunctiontestStringUuidConvertsToDatabaseValue()
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-
publicfunctiontestInvalidUuidConversionForDatabaseValue()
55+
publicfunctiontestNotSupportedStringUuidConversionToDatabaseValue()
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
publicfunctiontestNullConversionForDatabaseValue()
@@ -90,7 +80,9 @@ public function testInvalidUuidConversionForPHPValue()
9080

9181
publicfunctiontestNotSupportedTypeConversionForDatabaseValue()
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
publicfunctiontestNullConversionForPHPValue()
@@ -116,6 +108,6 @@ public function testGetGuidTypeDeclarationSQL()
116108

117109
publicfunctiontestRequiresSQLCommentHint()
118110
{
119-
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
111+
$this->assertFalse($this->type->requiresSQLCommentHint($this->platform));
120112
}
121113
}

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

Lines changed: 17 additions & 22 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-
publicfunctiontestUuidConvertsToDatabaseValue():void
47+
publicfunctiontestUuidConvertsToDatabaseValue()
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-
publicfunctiontestUuidInterfaceConvertsToDatabaseValue():void
57+
publicfunctiontestUuidInterfaceConvertsToDatabaseValue()
5858
{
5959
$uuid =$this->createMock(AbstractUid::class);
6060

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

71-
publicfunctiontestUuidStringConvertsToDatabaseValue():void
72-
{
73-
$actual =$this->type->convertToDatabaseValue(self::DUMMY_UUID,$this->platform);
74-
75-
$this->assertEquals(self::DUMMY_UUID,$actual);
76-
}
77-
78-
publicfunctiontestInvalidUuidConversionForDatabaseValue():void
71+
publicfunctiontestNotSupportedUuidStringConversionToDatabaseValue()
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
publicfunctiontestNotSupportedTypeConversionForDatabaseValue()
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-
publicfunctiontestNullConversionForDatabaseValue():void
85+
publicfunctiontestNullConversionForDatabaseValue()
9186
{
9287
$this->assertNull($this->type->convertToDatabaseValue(null,$this->platform));
9388
}
9489

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

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

103-
publicfunctiontestUuidConvertsToPHPValue():void
98+
publicfunctiontestUuidConvertsToPHPValue()
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-
publicfunctiontestInvalidUuidConversionForPHPValue():void
106+
publicfunctiontestInvalidUuidConversionForPHPValue()
112107
{
113108
$this->expectException(ConversionException::class);
114109

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

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

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

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

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

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

140-
publicfunctiontestRequiresSQLCommentHint():void
135+
publicfunctiontestRequiresSQLCommentHint()
141136
{
142-
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
137+
$this->assertFalse($this->type->requiresSQLCommentHint($this->platform));
143138
}
144139
}

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

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

1414
useDoctrine\DBAL\Platforms\AbstractPlatform;
1515
useDoctrine\DBAL\Types\ConversionException;
16-
useDoctrine\DBAL\Types\GuidType;
16+
useDoctrine\DBAL\Types\Type;
1717
useSymfony\Component\Uid\AbstractUid;
1818

19-
abstractclass AbstractBinaryUidTypeextendsGuidType
19+
abstractclass AbstractBinaryUidTypeextendsType
2020
{
2121
abstractprotectedfunctiongetUidClass():string;
2222

2323
publicfunctiongetSQLDeclaration(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
publicfunctionconvertToPHPValue($value,AbstractPlatform$platform): ?AbstractUid
3937
{
40-
if (null ===$value ||'' ===$value) {
41-
returnnull;
38+
if ($valueinstanceof AbstractUid||null ===$value) {
39+
return$value;
4240
}
4341

44-
if ($valueinstanceof 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,30 +57,14 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?Abstract
6157
*/
6258
publicfunctionconvertToDatabaseValue($value,AbstractPlatform$platform): ?string
6359
{
64-
if (null ===$value ||'' ===$value) {
65-
returnnull;
66-
}
67-
6860
if ($valueinstanceof AbstractUid) {
6961
return$value->toBinary();
7062
}
7163

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

76-
try {
77-
return$this->getUidClass()::fromString((string)$value)->toBinary();
78-
}catch (\InvalidArgumentException$e) {
79-
throw ConversionException::conversionFailed($value,$this->getName());
80-
}
81-
}
82-
83-
/**
84-
* {@inheritdoc}
85-
*/
86-
publicfunctionrequiresSQLCommentHint(AbstractPlatform$platform):bool
87-
{
88-
returntrue;
68+
throw ConversionException::conversionFailedInvalidType($value,$this->getName(), ['null', AbstractUid::class]);
8969
}
9070
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp