1212namespace Symfony \Bridge \Doctrine \Tests \Types ;
1313
1414use Doctrine \DBAL \Platforms \AbstractPlatform ;
15+ use Doctrine \DBAL \Platforms \MySQLPlatform ;
16+ use Doctrine \DBAL \Platforms \PostgreSQLPlatform ;
17+ use Doctrine \DBAL \Platforms \SqlitePlatform ;
1518use Doctrine \DBAL \Types \ConversionException ;
1619use Doctrine \DBAL \Types \Type ;
1720use PHPUnit \Framework \TestCase ;
1821use Symfony \Bridge \Doctrine \Types \UuidType ;
1922use Symfony \Component \Uid \AbstractUid ;
2023use Symfony \Component \Uid \Uuid ;
2124
25+ // DBAL 2 compatibility
26+ class_exists ('Doctrine\DBAL\Platforms\MySqlPlatform ' );
27+ class_exists ('Doctrine\DBAL\Platforms\PostgreSqlPlatform ' );
28+
2229final class UuidTypeTest extends TestCase
2330{
2431 private const DUMMY_UUID = '9f755235-5a2d-4aba-9605-e9962b312e50 ' ;
2532
26- /** @var AbstractPlatform */
27- private $ platform ;
28-
2933 /** @var UuidType */
3034 private $ type ;
3135
@@ -40,14 +44,6 @@ public static function setUpBeforeClass(): void
4044
4145 protected function setUp (): void
4246 {
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-
5147 $ this ->type = Type::getType ('uuid ' );
5248 }
5349
@@ -56,12 +52,12 @@ public function testUuidConvertsToDatabaseValue()
5652 $ uuid = Uuid::fromString (self ::DUMMY_UUID );
5753
5854 $ expected = $ uuid ->__toString ();
59- $ actual = $ this ->type ->convertToDatabaseValue ($ uuid , $ this -> platform );
55+ $ actual = $ this ->type ->convertToDatabaseValue ($ uuid , new PostgreSQLPlatform () );
6056
6157 $ this ->assertEquals ($ expected , $ actual );
6258 }
6359
64- public function testUuidInterfaceConvertsToDatabaseValue ()
60+ public function testUuidInterfaceConvertsToNativeUidDatabaseValue ()
6561 {
6662 $ uuid = $ this ->createMock (AbstractUid::class);
6763
@@ -70,14 +66,28 @@ public function testUuidInterfaceConvertsToDatabaseValue()
7066 ->method ('toRfc4122 ' )
7167 ->willReturn ('foo ' );
7268
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 ());
7484
7585 $ this ->assertEquals ('foo ' , $ actual );
7686 }
7787
7888 public function testUuidStringConvertsToDatabaseValue ()
7989 {
80- $ actual = $ this ->type ->convertToDatabaseValue (self ::DUMMY_UUID , $ this -> platform );
90+ $ actual = $ this ->type ->convertToDatabaseValue (self ::DUMMY_UUID , new PostgreSQLPlatform () );
8191
8292 $ this ->assertEquals (self ::DUMMY_UUID , $ actual );
8393 }
@@ -86,25 +96,25 @@ public function testNotSupportedTypeConversionForDatabaseValue()
8696 {
8797 $ this ->expectException (ConversionException::class);
8898
89- $ this ->type ->convertToDatabaseValue (new \stdClass (), $ this -> platform );
99+ $ this ->type ->convertToDatabaseValue (new \stdClass (), new SqlitePlatform () );
90100 }
91101
92102 public function testNullConversionForDatabaseValue ()
93103 {
94- $ this ->assertNull ($ this ->type ->convertToDatabaseValue (null , $ this -> platform ));
104+ $ this ->assertNull ($ this ->type ->convertToDatabaseValue (null , new SqlitePlatform () ));
95105 }
96106
97107 public function testUuidInterfaceConvertsToPHPValue ()
98108 {
99109 $ uuid = $ this ->createMock (AbstractUid::class);
100- $ actual = $ this ->type ->convertToPHPValue ($ uuid , $ this -> platform );
110+ $ actual = $ this ->type ->convertToPHPValue ($ uuid , new SqlitePlatform () );
101111
102112 $ this ->assertSame ($ uuid , $ actual );
103113 }
104114
105115 public function testUuidConvertsToPHPValue ()
106116 {
107- $ uuid = $ this ->type ->convertToPHPValue (self ::DUMMY_UUID , $ this -> platform );
117+ $ uuid = $ this ->type ->convertToPHPValue (self ::DUMMY_UUID , new SqlitePlatform () );
108118
109119 $ this ->assertInstanceOf (Uuid::class, $ uuid );
110120 $ this ->assertEquals (self ::DUMMY_UUID , $ uuid ->__toString ());
@@ -114,33 +124,45 @@ public function testInvalidUuidConversionForPHPValue()
114124 {
115125 $ this ->expectException (ConversionException::class);
116126
117- $ this ->type ->convertToPHPValue ('abcdefg ' , $ this -> platform );
127+ $ this ->type ->convertToPHPValue ('abcdefg ' , new SqlitePlatform () );
118128 }
119129
120130 public function testNullConversionForPHPValue ()
121131 {
122- $ this ->assertNull ($ this ->type ->convertToPHPValue (null , $ this -> platform ));
132+ $ this ->assertNull ($ this ->type ->convertToPHPValue (null , new SqlitePlatform () ));
123133 }
124134
125135 public function testReturnValueIfUuidForPHPValue ()
126136 {
127137 $ uuid = Uuid::v4 ();
128138
129- $ this ->assertSame ($ uuid , $ this ->type ->convertToPHPValue ($ uuid , $ this -> platform ));
139+ $ this ->assertSame ($ uuid , $ this ->type ->convertToPHPValue ($ uuid , new SqlitePlatform () ));
130140 }
131141
132142 public function testGetName ()
133143 {
134144 $ this ->assertEquals ('uuid ' , $ this ->type ->getName ());
135145 }
136146
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
138156 {
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+ ];
140162 }
141163
142164 public function testRequiresSQLCommentHint ()
143165 {
144- $ this ->assertTrue ($ this ->type ->requiresSQLCommentHint ($ this -> platform ));
166+ $ this ->assertTrue ($ this ->type ->requiresSQLCommentHint (new SqlitePlatform () ));
145167 }
146168}
0 commit comments