Completed
Push — develop ( c7cb25...56c936 )
by Sergei
20s queued 14s
created

DriverManagerTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 399
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 224
dl 0
loc 399
rs 10
c 0
b 0
f 0

11 Methods

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