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