1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\DBAL; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\Connections\MasterSlaveConnection; |
9
|
|
|
use Doctrine\DBAL\DBALException; |
10
|
|
|
use Doctrine\DBAL\Driver; |
11
|
|
|
use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySQLDriver; |
12
|
|
|
use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSqliteDriver; |
13
|
|
|
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver; |
14
|
|
|
use Doctrine\DBAL\DriverManager; |
15
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
16
|
|
|
use Doctrine\Tests\DbalTestCase; |
17
|
|
|
use stdClass; |
18
|
|
|
use function get_class; |
19
|
|
|
use function in_array; |
20
|
|
|
use function is_array; |
21
|
|
|
|
22
|
|
|
class DriverManagerTest extends DbalTestCase |
23
|
|
|
{ |
24
|
|
|
public function testCheckParams() : void |
25
|
|
|
{ |
26
|
|
|
$this->expectException(DBALException::class); |
27
|
|
|
|
28
|
|
|
DriverManager::getConnection([]); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testInvalidDriver() : void |
32
|
|
|
{ |
33
|
|
|
$this->expectException(DBALException::class); |
34
|
|
|
|
35
|
|
|
DriverManager::getConnection(['driver' => 'invalid_driver']); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @requires extension pdo_sqlite |
40
|
|
|
*/ |
41
|
|
|
public function testCustomPlatform() : void |
42
|
|
|
{ |
43
|
|
|
$platform = $this->createMock(AbstractPlatform::class); |
44
|
|
|
$options = [ |
45
|
|
|
'url' => 'sqlite::memory:', |
46
|
|
|
'platform' => $platform, |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
$conn = DriverManager::getConnection($options); |
50
|
|
|
self::assertSame($platform, $conn->getDatabasePlatform()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @requires extension pdo_sqlite |
55
|
|
|
*/ |
56
|
|
|
public function testCustomWrapper() : void |
57
|
|
|
{ |
58
|
|
|
$wrapper = $this->createMock(Connection::class); |
59
|
|
|
$wrapperClass = get_class($wrapper); |
60
|
|
|
|
61
|
|
|
$options = [ |
62
|
|
|
'url' => 'sqlite::memory:', |
63
|
|
|
'wrapperClass' => $wrapperClass, |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
$conn = DriverManager::getConnection($options); |
67
|
|
|
self::assertInstanceOf($wrapperClass, $conn); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @requires extension pdo_sqlite |
72
|
|
|
*/ |
73
|
|
|
public function testInvalidWrapperClass() : void |
74
|
|
|
{ |
75
|
|
|
$this->expectException(DBALException::class); |
76
|
|
|
|
77
|
|
|
$options = [ |
78
|
|
|
'url' => 'sqlite::memory:', |
79
|
|
|
'wrapperClass' => stdClass::class, |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
DriverManager::getConnection($options); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testInvalidDriverClass() : void |
86
|
|
|
{ |
87
|
|
|
$this->expectException(DBALException::class); |
88
|
|
|
|
89
|
|
|
$options = ['driverClass' => stdClass::class]; |
90
|
|
|
|
91
|
|
|
DriverManager::getConnection($options); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testValidDriverClass() : void |
95
|
|
|
{ |
96
|
|
|
$options = ['driverClass' => PDOMySQLDriver::class]; |
97
|
|
|
|
98
|
|
|
$conn = DriverManager::getConnection($options); |
99
|
|
|
self::assertInstanceOf(PDOMySQLDriver::class, $conn->getDriver()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testDatabaseUrlMasterSlave() : void |
103
|
|
|
{ |
104
|
|
|
$options = [ |
105
|
|
|
'driver' => 'pdo_mysql', |
106
|
|
|
'master' => ['url' => 'mysql://foo:bar@localhost:11211/baz'], |
107
|
|
|
'slaves' => [ |
108
|
|
|
'slave1' => ['url' => 'mysql://foo:bar@localhost:11211/baz_slave'], |
109
|
|
|
], |
110
|
|
|
'wrapperClass' => MasterSlaveConnection::class, |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
$conn = DriverManager::getConnection($options); |
114
|
|
|
|
115
|
|
|
$params = $conn->getParams(); |
116
|
|
|
self::assertInstanceOf(PDOMySQLDriver::class, $conn->getDriver()); |
117
|
|
|
|
118
|
|
|
$expected = [ |
119
|
|
|
'user' => 'foo', |
120
|
|
|
'password' => 'bar', |
121
|
|
|
'host' => 'localhost', |
122
|
|
|
'port' => 11211, |
123
|
|
|
]; |
124
|
|
|
|
125
|
|
|
foreach ($expected as $key => $value) { |
126
|
|
|
self::assertEquals($value, $params['master'][$key]); |
127
|
|
|
self::assertEquals($value, $params['slaves']['slave1'][$key]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
self::assertEquals('baz', $params['master']['dbname']); |
131
|
|
|
self::assertEquals('baz_slave', $params['slaves']['slave1']['dbname']); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param mixed $url |
136
|
|
|
* @param mixed $expected |
137
|
|
|
* |
138
|
|
|
* @dataProvider databaseUrls |
139
|
|
|
*/ |
140
|
|
|
public function testDatabaseUrl($url, $expected) : void |
141
|
|
|
{ |
142
|
|
|
$options = is_array($url) ? $url : ['url' => $url]; |
143
|
|
|
|
144
|
|
|
if ($expected === false) { |
145
|
|
|
$this->expectException(DBALException::class); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$conn = DriverManager::getConnection($options); |
149
|
|
|
|
150
|
|
|
$params = $conn->getParams(); |
151
|
|
|
foreach ($expected as $key => $value) { |
152
|
|
|
if (in_array($key, ['driver', 'driverClass'], true)) { |
153
|
|
|
self::assertInstanceOf($value, $conn->getDriver()); |
154
|
|
|
} else { |
155
|
|
|
self::assertEquals($value, $params[$key]); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return array<string, mixed> |
162
|
|
|
*/ |
163
|
|
|
public function databaseUrls() : iterable |
164
|
|
|
{ |
165
|
|
|
$driver = $this->createMock(Driver::class); |
166
|
|
|
$driverClass = get_class($driver); |
167
|
|
|
|
168
|
|
|
return [ |
169
|
|
|
'simple URL' => [ |
170
|
|
|
'mysql://foo:bar@localhost/baz', |
171
|
|
|
[ |
172
|
|
|
'user' => 'foo', |
173
|
|
|
'password' => 'bar', |
174
|
|
|
'host' => 'localhost', |
175
|
|
|
'dbname' => 'baz', |
176
|
|
|
'driver' => PDOMySQLDriver::class, |
177
|
|
|
], |
178
|
|
|
], |
179
|
|
|
'simple URL with port' => [ |
180
|
|
|
'mysql://foo:bar@localhost:11211/baz', |
181
|
|
|
[ |
182
|
|
|
'user' => 'foo', |
183
|
|
|
'password' => 'bar', |
184
|
|
|
'host' => 'localhost', |
185
|
|
|
'port' => 11211, |
186
|
|
|
'dbname' => 'baz', |
187
|
|
|
'driver' => PDOMySQLDriver::class, |
188
|
|
|
], |
189
|
|
|
], |
190
|
|
|
'sqlite relative URL with host' => [ |
191
|
|
|
'sqlite://localhost/foo/dbname.sqlite', |
192
|
|
|
[ |
193
|
|
|
'path' => 'foo/dbname.sqlite', |
194
|
|
|
'driver' => PDOSqliteDriver::class, |
195
|
|
|
], |
196
|
|
|
], |
197
|
|
|
'sqlite absolute URL with host' => [ |
198
|
|
|
'sqlite://localhost//tmp/dbname.sqlite', |
199
|
|
|
[ |
200
|
|
|
'path' => '/tmp/dbname.sqlite', |
201
|
|
|
'driver' => PDOSqliteDriver::class, |
202
|
|
|
], |
203
|
|
|
], |
204
|
|
|
'sqlite relative URL without host' => [ |
205
|
|
|
'sqlite:///foo/dbname.sqlite', |
206
|
|
|
[ |
207
|
|
|
'path' => 'foo/dbname.sqlite', |
208
|
|
|
'driver' => PDOSqliteDriver::class, |
209
|
|
|
], |
210
|
|
|
], |
211
|
|
|
'sqlite absolute URL without host' => [ |
212
|
|
|
'sqlite:////tmp/dbname.sqlite', |
213
|
|
|
[ |
214
|
|
|
'path' => '/tmp/dbname.sqlite', |
215
|
|
|
'driver' => PDOSqliteDriver::class, |
216
|
|
|
], |
217
|
|
|
], |
218
|
|
|
'sqlite memory' => [ |
219
|
|
|
'sqlite:///:memory:', |
220
|
|
|
[ |
221
|
|
|
'memory' => true, |
222
|
|
|
'driver' => PDOSqliteDriver::class, |
223
|
|
|
], |
224
|
|
|
], |
225
|
|
|
'sqlite memory with host' => [ |
226
|
|
|
'sqlite://localhost/:memory:', |
227
|
|
|
[ |
228
|
|
|
'memory' => true, |
229
|
|
|
'driver' => PDOSqliteDriver::class, |
230
|
|
|
], |
231
|
|
|
], |
232
|
|
|
'params parsed from URL override individual params' => [ |
233
|
|
|
[ |
234
|
|
|
'url' => 'mysql://foo:bar@localhost/baz', |
235
|
|
|
'password' => 'lulz', |
236
|
|
|
], |
237
|
|
|
[ |
238
|
|
|
'user' => 'foo', |
239
|
|
|
'password' => 'bar', |
240
|
|
|
'host' => 'localhost', |
241
|
|
|
'dbname' => 'baz', |
242
|
|
|
'driver' => PDOMySQLDriver::class, |
243
|
|
|
], |
244
|
|
|
], |
245
|
|
|
'params not parsed from URL but individual params are preserved' => [ |
246
|
|
|
[ |
247
|
|
|
'url' => 'mysql://foo:bar@localhost/baz', |
248
|
|
|
'port' => 1234, |
249
|
|
|
], |
250
|
|
|
[ |
251
|
|
|
'user' => 'foo', |
252
|
|
|
'password' => 'bar', |
253
|
|
|
'host' => 'localhost', |
254
|
|
|
'port' => 1234, |
255
|
|
|
'dbname' => 'baz', |
256
|
|
|
'driver' => PDOMySQLDriver::class, |
257
|
|
|
], |
258
|
|
|
], |
259
|
|
|
'query params from URL are used as extra params' => [ |
260
|
|
|
'url' => 'mysql://foo:bar@localhost/dbname?charset=UTF-8', |
261
|
|
|
['charset' => 'UTF-8'], |
262
|
|
|
], |
263
|
|
|
'simple URL with fallthrough scheme not defined in map' => [ |
264
|
|
|
'sqlsrv://foo:bar@localhost/baz', |
265
|
|
|
[ |
266
|
|
|
'user' => 'foo', |
267
|
|
|
'password' => 'bar', |
268
|
|
|
'host' => 'localhost', |
269
|
|
|
'dbname' => 'baz', |
270
|
|
|
'driver' => SQLSrvDriver::class, |
271
|
|
|
], |
272
|
|
|
], |
273
|
|
|
'simple URL with fallthrough scheme containing underscores fails' => [ |
274
|
|
|
'pdo_mysql://foo:bar@localhost/baz', |
275
|
|
|
false, |
276
|
|
|
], |
277
|
|
|
'simple URL with fallthrough scheme containing dashes works' => [ |
278
|
|
|
'pdo-mysql://foo:bar@localhost/baz', |
279
|
|
|
[ |
280
|
|
|
'user' => 'foo', |
281
|
|
|
'password' => 'bar', |
282
|
|
|
'host' => 'localhost', |
283
|
|
|
'dbname' => 'baz', |
284
|
|
|
'driver' => PDOMySQLDriver::class, |
285
|
|
|
], |
286
|
|
|
], |
287
|
|
|
'simple URL with percent encoding' => [ |
288
|
|
|
'mysql://foo%3A:bar%2F@localhost/baz+baz%40', |
289
|
|
|
[ |
290
|
|
|
'user' => 'foo:', |
291
|
|
|
'password' => 'bar/', |
292
|
|
|
'host' => 'localhost', |
293
|
|
|
'dbname' => 'baz+baz@', |
294
|
|
|
'driver' => PDOMySQLDriver::class, |
295
|
|
|
], |
296
|
|
|
], |
297
|
|
|
'simple URL with percent sign in password' => [ |
298
|
|
|
'mysql://foo:bar%25bar@localhost/baz', |
299
|
|
|
[ |
300
|
|
|
'user' => 'foo', |
301
|
|
|
'password' => 'bar%bar', |
302
|
|
|
'host' => 'localhost', |
303
|
|
|
'dbname' => 'baz', |
304
|
|
|
'driver' => PDOMySQLDriver::class, |
305
|
|
|
], |
306
|
|
|
], |
307
|
|
|
|
308
|
|
|
// DBAL-1234 |
309
|
|
|
'URL without scheme and without any driver information' => [ |
310
|
|
|
['url' => '//foo:bar@localhost/baz'], |
311
|
|
|
false, |
312
|
|
|
], |
313
|
|
|
'URL without scheme but default driver' => [ |
314
|
|
|
[ |
315
|
|
|
'url' => '//foo:bar@localhost/baz', |
316
|
|
|
'driver' => 'pdo_mysql', |
317
|
|
|
], |
318
|
|
|
[ |
319
|
|
|
'user' => 'foo', |
320
|
|
|
'password' => 'bar', |
321
|
|
|
'host' => 'localhost', |
322
|
|
|
'dbname' => 'baz', |
323
|
|
|
'driver' => PDOMySQLDriver::class, |
324
|
|
|
], |
325
|
|
|
], |
326
|
|
|
'URL without scheme but custom driver' => [ |
327
|
|
|
[ |
328
|
|
|
'url' => '//foo:bar@localhost/baz', |
329
|
|
|
'driverClass' => $driverClass, |
330
|
|
|
], |
331
|
|
|
[ |
332
|
|
|
'user' => 'foo', |
333
|
|
|
'password' => 'bar', |
334
|
|
|
'host' => 'localhost', |
335
|
|
|
'dbname' => 'baz', |
336
|
|
|
'driverClass' => $driverClass, |
337
|
|
|
], |
338
|
|
|
], |
339
|
|
|
'URL without scheme but driver and custom driver' => [ |
340
|
|
|
[ |
341
|
|
|
'url' => '//foo:bar@localhost/baz', |
342
|
|
|
'driver' => 'pdo_mysql', |
343
|
|
|
'driverClass' => $driverClass, |
344
|
|
|
], |
345
|
|
|
[ |
346
|
|
|
'user' => 'foo', |
347
|
|
|
'password' => 'bar', |
348
|
|
|
'host' => 'localhost', |
349
|
|
|
'dbname' => 'baz', |
350
|
|
|
'driverClass' => $driverClass, |
351
|
|
|
], |
352
|
|
|
], |
353
|
|
|
'URL with default driver' => [ |
354
|
|
|
[ |
355
|
|
|
'url' => 'mysql://foo:bar@localhost/baz', |
356
|
|
|
'driver' => 'sqlite', |
357
|
|
|
], |
358
|
|
|
[ |
359
|
|
|
'user' => 'foo', |
360
|
|
|
'password' => 'bar', |
361
|
|
|
'host' => 'localhost', |
362
|
|
|
'dbname' => 'baz', |
363
|
|
|
'driver' => PDOMySQLDriver::class, |
364
|
|
|
], |
365
|
|
|
], |
366
|
|
|
'URL with default custom driver' => [ |
367
|
|
|
[ |
368
|
|
|
'url' => 'mysql://foo:bar@localhost/baz', |
369
|
|
|
'driverClass' => $driverClass, |
370
|
|
|
], |
371
|
|
|
[ |
372
|
|
|
'user' => 'foo', |
373
|
|
|
'password' => 'bar', |
374
|
|
|
'host' => 'localhost', |
375
|
|
|
'dbname' => 'baz', |
376
|
|
|
'driver' => PDOMySQLDriver::class, |
377
|
|
|
], |
378
|
|
|
], |
379
|
|
|
'URL with default driver and default custom driver' => [ |
380
|
|
|
[ |
381
|
|
|
'url' => 'mysql://foo:bar@localhost/baz', |
382
|
|
|
'driver' => 'sqlite', |
383
|
|
|
'driverClass' => $driverClass, |
384
|
|
|
], |
385
|
|
|
[ |
386
|
|
|
'user' => 'foo', |
387
|
|
|
'password' => 'bar', |
388
|
|
|
'host' => 'localhost', |
389
|
|
|
'dbname' => 'baz', |
390
|
|
|
'driver' => PDOMySQLDriver::class, |
391
|
|
|
], |
392
|
|
|
], |
393
|
|
|
]; |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|