12
12
namespace Symfony \Bridge \Doctrine \Tests \Types ;
13
13
14
14
use Doctrine \DBAL \Platforms \AbstractPlatform ;
15
+ use Doctrine \DBAL \Platforms \MySQLPlatform ;
16
+ use Doctrine \DBAL \Platforms \PostgreSQLPlatform ;
17
+ use Doctrine \DBAL \Platforms \SqlitePlatform ;
15
18
use Doctrine \DBAL \Types \ConversionException ;
16
19
use Doctrine \DBAL \Types \Type ;
17
20
use PHPUnit \Framework \TestCase ;
18
21
use Symfony \Bridge \Doctrine \Types \UuidType ;
19
22
use Symfony \Component \Uid \AbstractUid ;
20
23
use Symfony \Component \Uid \Uuid ;
21
24
25
+ // DBAL 2 compatibility
26
+ class_exists ('Doctrine\DBAL\Platforms\MySqlPlatform ' );
27
+ class_exists ('Doctrine\DBAL\Platforms\PostgreSqlPlatform ' );
28
+
22
29
final class UuidTypeTest extends TestCase
23
30
{
24
31
private const DUMMY_UUID = '9f755235-5a2d-4aba-9605-e9962b312e50 ' ;
25
32
26
- /** @var AbstractPlatform */
27
- private $ platform ;
28
-
29
33
/** @var UuidType */
30
34
private $ type ;
31
35
@@ -40,14 +44,6 @@ public static function setUpBeforeClass(): void
40
44
41
45
protected function setUp (): void
42
46
{
43
- $ this ->platform = $ this ->createMock (AbstractPlatform::class);
44
- $ this ->platform
45
- ->method ('hasNativeGuidType ' )
46
- ->willReturn (true );
47
- $ this ->platform
48
- ->method ('getGuidTypeDeclarationSQL ' )
49
- ->willReturn ('DUMMYVARCHAR() ' );
50
-
51
47
$ this ->type = Type::getType ('uuid ' );
52
48
}
53
49
@@ -56,12 +52,12 @@ public function testUuidConvertsToDatabaseValue()
56
52
$ uuid = Uuid::fromString (self ::DUMMY_UUID );
57
53
58
54
$ expected = $ uuid ->__toString ();
59
- $ actual = $ this ->type ->convertToDatabaseValue ($ uuid , $ this -> platform );
55
+ $ actual = $ this ->type ->convertToDatabaseValue ($ uuid , new PostgreSQLPlatform () );
60
56
61
57
$ this ->assertEquals ($ expected , $ actual );
62
58
}
63
59
64
- public function testUuidInterfaceConvertsToDatabaseValue ()
60
+ public function testUuidInterfaceConvertsToNativeUidDatabaseValue ()
65
61
{
66
62
$ uuid = $ this ->createMock (AbstractUid::class);
67
63
@@ -70,14 +66,28 @@ public function testUuidInterfaceConvertsToDatabaseValue()
70
66
->method ('toRfc4122 ' )
71
67
->willReturn ('foo ' );
72
68
73
- $ actual = $ this ->type ->convertToDatabaseValue ($ uuid , $ this ->platform );
69
+ $ actual = $ this ->type ->convertToDatabaseValue ($ uuid , new PostgreSQLPlatform ());
70
+
71
+ $ this ->assertEquals ('foo ' , $ actual );
72
+ }
73
+
74
+ public function testUuidInterfaceConvertsToBinaryDatabaseValue ()
75
+ {
76
+ $ uuid = $ this ->createMock (AbstractUid::class);
77
+
78
+ $ uuid
79
+ ->expects ($ this ->once ())
80
+ ->method ('toBinary ' )
81
+ ->willReturn ('foo ' );
82
+
83
+ $ actual = $ this ->type ->convertToDatabaseValue ($ uuid , new MySQLPlatform ());
74
84
75
85
$ this ->assertEquals ('foo ' , $ actual );
76
86
}
77
87
78
88
public function testUuidStringConvertsToDatabaseValue ()
79
89
{
80
- $ actual = $ this ->type ->convertToDatabaseValue (self ::DUMMY_UUID , $ this -> platform );
90
+ $ actual = $ this ->type ->convertToDatabaseValue (self ::DUMMY_UUID , new PostgreSQLPlatform () );
81
91
82
92
$ this ->assertEquals (self ::DUMMY_UUID , $ actual );
83
93
}
@@ -86,25 +96,25 @@ public function testNotSupportedTypeConversionForDatabaseValue()
86
96
{
87
97
$ this ->expectException (ConversionException::class);
88
98
89
- $ this ->type ->convertToDatabaseValue (new \stdClass (), $ this -> platform );
99
+ $ this ->type ->convertToDatabaseValue (new \stdClass (), new SqlitePlatform () );
90
100
}
91
101
92
102
public function testNullConversionForDatabaseValue ()
93
103
{
94
- $ this ->assertNull ($ this ->type ->convertToDatabaseValue (null , $ this -> platform ));
104
+ $ this ->assertNull ($ this ->type ->convertToDatabaseValue (null , new SqlitePlatform () ));
95
105
}
96
106
97
107
public function testUuidInterfaceConvertsToPHPValue ()
98
108
{
99
109
$ uuid = $ this ->createMock (AbstractUid::class);
100
- $ actual = $ this ->type ->convertToPHPValue ($ uuid , $ this -> platform );
110
+ $ actual = $ this ->type ->convertToPHPValue ($ uuid , new SqlitePlatform () );
101
111
102
112
$ this ->assertSame ($ uuid , $ actual );
103
113
}
104
114
105
115
public function testUuidConvertsToPHPValue ()
106
116
{
107
- $ uuid = $ this ->type ->convertToPHPValue (self ::DUMMY_UUID , $ this -> platform );
117
+ $ uuid = $ this ->type ->convertToPHPValue (self ::DUMMY_UUID , new SqlitePlatform () );
108
118
109
119
$ this ->assertInstanceOf (Uuid::class, $ uuid );
110
120
$ this ->assertEquals (self ::DUMMY_UUID , $ uuid ->__toString ());
@@ -114,33 +124,45 @@ public function testInvalidUuidConversionForPHPValue()
114
124
{
115
125
$ this ->expectException (ConversionException::class);
116
126
117
- $ this ->type ->convertToPHPValue ('abcdefg ' , $ this -> platform );
127
+ $ this ->type ->convertToPHPValue ('abcdefg ' , new SqlitePlatform () );
118
128
}
119
129
120
130
public function testNullConversionForPHPValue ()
121
131
{
122
- $ this ->assertNull ($ this ->type ->convertToPHPValue (null , $ this -> platform ));
132
+ $ this ->assertNull ($ this ->type ->convertToPHPValue (null , new SqlitePlatform () ));
123
133
}
124
134
125
135
public function testReturnValueIfUuidForPHPValue ()
126
136
{
127
137
$ uuid = Uuid::v4 ();
128
138
129
- $ this ->assertSame ($ uuid , $ this ->type ->convertToPHPValue ($ uuid , $ this -> platform ));
139
+ $ this ->assertSame ($ uuid , $ this ->type ->convertToPHPValue ($ uuid , new SqlitePlatform () ));
130
140
}
131
141
132
142
public function testGetName ()
133
143
{
134
144
$ this ->assertEquals ('uuid ' , $ this ->type ->getName ());
135
145
}
136
146
137
- public function testGetGuidTypeDeclarationSQL ()
147
+ /**
148
+ * @dataProvider provideSqlDeclarations
149
+ */
150
+ public function testGetGuidTypeDeclarationSQL (AbstractPlatform $ platform , string $ expectedDeclaration )
151
+ {
152
+ $ this ->assertEquals ($ expectedDeclaration , $ this ->type ->getSqlDeclaration (['length ' => 36 ], $ platform ));
153
+ }
154
+
155
+ public function provideSqlDeclarations (): array
138
156
{
139
- $ this ->assertEquals ('DUMMYVARCHAR() ' , $ this ->type ->getSqlDeclaration (['length ' => 36 ], $ this ->platform ));
157
+ return [
158
+ [new PostgreSQLPlatform (), 'UUID ' ],
159
+ [new SqlitePlatform (), 'BLOB ' ],
160
+ [new MySQLPlatform (), 'BINARY(16) ' ],
161
+ ];
140
162
}
141
163
142
164
public function testRequiresSQLCommentHint ()
143
165
{
144
- $ this ->assertTrue ($ this ->type ->requiresSQLCommentHint ($ this -> platform ));
166
+ $ this ->assertTrue ($ this ->type ->requiresSQLCommentHint (new SqlitePlatform () ));
145
167
}
146
168
}
0 commit comments