DriverManagerTest.php 15.2 KB
Newer Older
jwage's avatar
jwage committed
1 2 3 4
<?php

namespace Doctrine\Tests\DBAL;

Luís Cobucci's avatar
Luís Cobucci committed
5
use Doctrine\DBAL\DBALException;
Sergei Morozov's avatar
Sergei Morozov committed
6 7 8 9 10 11 12 13 14 15
use Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver as DrizzlePDOMySqlDriver;
use Doctrine\DBAL\Driver\PDOMySQL\Driver as PDOMySQLDriver;
use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSqliteDriver;
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver;
use Doctrine\DBAL\DriverManager;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
use Doctrine\Tests\DbalTestCase;
use Doctrine\Tests\Mocks\ConnectionMock;
use Doctrine\Tests\Mocks\DriverMock;
use stdClass;
16 17 18
use function extension_loaded;
use function in_array;
use function is_array;
Luís Cobucci's avatar
Luís Cobucci committed
19

Sergei Morozov's avatar
Sergei Morozov committed
20
class DriverManagerTest extends DbalTestCase
jwage's avatar
jwage committed
21 22
{
    /**
23
     * @requires extension pdo_sqlite
24
     * @expectedException \Doctrine\DBAL\DBALException
jwage's avatar
jwage committed
25 26 27
     */
    public function testInvalidPdoInstance()
    {
Sergei Morozov's avatar
Sergei Morozov committed
28
        DriverManager::getConnection(['pdo' => 'test']);
jwage's avatar
jwage committed
29 30
    }

31 32 33
    /**
     * @requires extension pdo_sqlite
     */
jwage's avatar
jwage committed
34 35
    public function testValidPdoInstance()
    {
Sergei Morozov's avatar
Sergei Morozov committed
36 37 38 39
        $conn = DriverManager::getConnection([
            'pdo' => new \PDO('sqlite::memory:'),
        ]);

40
        self::assertEquals('sqlite', $conn->getDatabasePlatform()->getName());
jwage's avatar
jwage committed
41 42
    }

43 44
    /**
     * @group DBAL-32
45
     * @requires extension pdo_sqlite
46 47 48 49 50
     */
    public function testPdoInstanceSetErrorMode()
    {
        $pdo = new \PDO('sqlite::memory:');
        $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
Sergei Morozov's avatar
Sergei Morozov committed
51
        $options = ['pdo' => $pdo];
52

Sergei Morozov's avatar
Sergei Morozov committed
53
        DriverManager::getConnection($options);
54
        self::assertEquals(\PDO::ERRMODE_EXCEPTION, $pdo->getAttribute(\PDO::ATTR_ERRMODE));
55 56
    }

jwage's avatar
jwage committed
57
    /**
58
     * @expectedException \Doctrine\DBAL\DBALException
jwage's avatar
jwage committed
59 60 61
     */
    public function testCheckParams()
    {
Sergei Morozov's avatar
Sergei Morozov committed
62
        DriverManager::getConnection([]);
jwage's avatar
jwage committed
63 64 65
    }

    /**
66
     * @expectedException \Doctrine\DBAL\DBALException
jwage's avatar
jwage committed
67 68 69
     */
    public function testInvalidDriver()
    {
Sergei Morozov's avatar
Sergei Morozov committed
70
        DriverManager::getConnection(['driver' => 'invalid_driver']);
jwage's avatar
jwage committed
71
    }
72

73 74 75
    /**
     * @requires extension pdo_sqlite
     */
76 77
    public function testCustomPlatform()
    {
Sergei Morozov's avatar
Sergei Morozov committed
78 79 80 81 82
        $mockPlatform = new MockPlatform();
        $options      = [
            'pdo'      => new \PDO('sqlite::memory:'),
            'platform' => $mockPlatform,
        ];
83

Sergei Morozov's avatar
Sergei Morozov committed
84
        $conn = DriverManager::getConnection($options);
85
        self::assertSame($mockPlatform, $conn->getDatabasePlatform());
86
    }
87

88 89 90
    /**
     * @requires extension pdo_sqlite
     */
91 92
    public function testCustomWrapper()
    {
Sergei Morozov's avatar
Sergei Morozov committed
93
        $wrapperClass = ConnectionMock::class;
94

Sergei Morozov's avatar
Sergei Morozov committed
95
        $options = [
96
            'pdo' => new \PDO('sqlite::memory:'),
97
            'wrapperClass' => $wrapperClass,
Sergei Morozov's avatar
Sergei Morozov committed
98
        ];
99

Sergei Morozov's avatar
Sergei Morozov committed
100
        $conn = DriverManager::getConnection($options);
101
        self::assertInstanceOf($wrapperClass, $conn);
102 103
    }

104 105 106
    /**
     * @requires extension pdo_sqlite
     */
107 108
    public function testInvalidWrapperClass()
    {
Luís Cobucci's avatar
Luís Cobucci committed
109
        $this->expectException(DBALException::class);
110

Sergei Morozov's avatar
Sergei Morozov committed
111
        $options = [
112
            'pdo' => new \PDO('sqlite::memory:'),
Sergei Morozov's avatar
Sergei Morozov committed
113 114
            'wrapperClass' => stdClass::class,
        ];
115

Sergei Morozov's avatar
Sergei Morozov committed
116
        DriverManager::getConnection($options);
117 118 119 120
    }

    public function testInvalidDriverClass()
    {
Luís Cobucci's avatar
Luís Cobucci committed
121
        $this->expectException(DBALException::class);
122

Sergei Morozov's avatar
Sergei Morozov committed
123
        $options = ['driverClass' => stdClass::class];
124

Sergei Morozov's avatar
Sergei Morozov committed
125
        DriverManager::getConnection($options);
126 127 128 129
    }

    public function testValidDriverClass()
    {
Sergei Morozov's avatar
Sergei Morozov committed
130
        $options = ['driverClass' => PDOMySQLDriver::class];
131

Sergei Morozov's avatar
Sergei Morozov committed
132 133
        $conn = DriverManager::getConnection($options);
        self::assertInstanceOf(PDOMySQLDriver::class, $conn->getDriver());
134
    }
135

136 137 138 139 140
    /**
     * @dataProvider databaseUrls
     */
    public function testDatabaseUrl($url, $expected)
    {
Sergei Morozov's avatar
Sergei Morozov committed
141
        $options = is_array($url) ? $url : ['url' => $url];
142

143
        if (isset($options['pdo'])) {
Sergei Morozov's avatar
Sergei Morozov committed
144
            if (! extension_loaded('pdo')) {
145 146 147 148 149 150
                $this->markTestSkipped('PDO is not installed');
            }

            $options['pdo'] = $this->createMock(\PDO::class);
        }

Sergei Morozov's avatar
Sergei Morozov committed
151
        $options = is_array($url) ? $url : ['url' => $url];
152

153
        if ($expected === false) {
Luís Cobucci's avatar
Luís Cobucci committed
154
            $this->expectException(DBALException::class);
155
        }
156

Sergei Morozov's avatar
Sergei Morozov committed
157
        $conn = DriverManager::getConnection($options);
158

159 160
        $params = $conn->getParams();
        foreach ($expected as $key => $value) {
Sergei Morozov's avatar
Sergei Morozov committed
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 170
    public function databaseUrls()
    {
Sergei Morozov's avatar
Sergei Morozov committed
171 172
        return [
            'simple URL' => [
173
                'mysql://foo:bar@localhost/baz',
Sergei Morozov's avatar
Sergei Morozov committed
174 175 176 177 178 179 180 181 182
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
            'simple URL with port' => [
183
                'mysql://foo:bar@localhost:11211/baz',
Sergei Morozov's avatar
Sergei Morozov committed
184 185 186 187 188 189 190 191 192 193
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'port'     => 11211,
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
            'sqlite relative URL with host' => [
194
                'sqlite://localhost/foo/dbname.sqlite',
Sergei Morozov's avatar
Sergei Morozov committed
195 196 197 198 199 200
                [
                    'path'   => 'foo/dbname.sqlite',
                    'driver' => PDOSqliteDriver::class,
                ],
            ],
            'sqlite absolute URL with host' => [
201
                'sqlite://localhost//tmp/dbname.sqlite',
Sergei Morozov's avatar
Sergei Morozov committed
202 203 204 205 206 207
                [
                    'path'   => '/tmp/dbname.sqlite',
                    'driver' => PDOSqliteDriver::class,
                ],
            ],
            'sqlite relative URL without host' => [
208
                'sqlite:///foo/dbname.sqlite',
Sergei Morozov's avatar
Sergei Morozov committed
209 210 211 212 213 214
                [
                    'path'   => 'foo/dbname.sqlite',
                    'driver' => PDOSqliteDriver::class,
                ],
            ],
            'sqlite absolute URL without host' => [
215
                'sqlite:////tmp/dbname.sqlite',
Sergei Morozov's avatar
Sergei Morozov committed
216 217 218 219 220 221
                [
                    'path'   => '/tmp/dbname.sqlite',
                    'driver' => PDOSqliteDriver::class,
                ],
            ],
            'sqlite memory' => [
222
                'sqlite:///:memory:',
Sergei Morozov's avatar
Sergei Morozov committed
223 224 225 226 227 228
                [
                    'memory' => true,
                    'driver' => PDOSqliteDriver::class,
                ],
            ],
            'sqlite memory with host' => [
229
                'sqlite://localhost/:memory:',
Sergei Morozov's avatar
Sergei Morozov committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
                [
                    'memory' => true,
                    'driver' => PDOSqliteDriver::class,
                ],
            ],
            'params parsed from URL override individual params' => [
                [
                    'url'      => 'mysql://foo:bar@localhost/baz',
                    'password' => 'lulz',
                ],
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
            'params not parsed from URL but individual params are preserved' => [
                [
                    'url'  => 'mysql://foo:bar@localhost/baz',
                    'port' => 1234,
                ],
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'port'     => 1234,
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
            'query params from URL are used as extra params' => [
263
                'url' => 'mysql://foo:bar@localhost/dbname?charset=UTF-8',
Sergei Morozov's avatar
Sergei Morozov committed
264 265 266
                ['charset' => 'UTF-8'],
            ],
            'simple URL with fallthrough scheme not defined in map' => [
267
                'sqlsrv://foo:bar@localhost/baz',
Sergei Morozov's avatar
Sergei Morozov committed
268 269 270 271 272 273 274 275 276
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => SQLSrvDriver::class,
                ],
            ],
            'simple URL with fallthrough scheme containing underscores fails' => [
277 278
                'drizzle_pdo_mysql://foo:bar@localhost/baz',
                false,
Sergei Morozov's avatar
Sergei Morozov committed
279 280
            ],
            'simple URL with fallthrough scheme containing dashes works' => [
281
                'drizzle-pdo-mysql://foo:bar@localhost/baz',
Sergei Morozov's avatar
Sergei Morozov committed
282 283 284 285 286 287 288 289 290
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => DrizzlePDOMySqlDriver::class,
                ],
            ],
            'simple URL with percent encoding' => [
291
                'mysql://foo%3A:bar%2F@localhost/baz+baz%40',
Sergei Morozov's avatar
Sergei Morozov committed
292 293 294 295 296 297 298 299 300
                [
                    'user'     => 'foo:',
                    'password' => 'bar/',
                    'host'     => 'localhost',
                    'dbname'   => 'baz+baz@',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
            'simple URL with percent sign in password' => [
301
                'mysql://foo:bar%25bar@localhost/baz',
Sergei Morozov's avatar
Sergei Morozov committed
302 303 304 305 306 307 308 309
                [
                    'user'     => 'foo',
                    'password' => 'bar%bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
310 311

            // DBAL-1234
Sergei Morozov's avatar
Sergei Morozov committed
312 313
            'URL without scheme and without any driver information' => [
                ['url' => '//foo:bar@localhost/baz'],
314
                false,
Sergei Morozov's avatar
Sergei Morozov committed
315 316 317 318 319 320
            ],
            'URL without scheme but default PDO driver' => [
                [
                    'url' => '//foo:bar@localhost/baz',
                    'pdo' => true,
                ],
321
                false,
Sergei Morozov's avatar
Sergei Morozov committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
            ],
            'URL without scheme but default driver' => [
                [
                    'url'    => '//foo:bar@localhost/baz',
                    'driver' => 'pdo_mysql',
                ],
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
            'URL without scheme but custom driver' => [
                [
                    'url'         => '//foo:bar@localhost/baz',
                    'driverClass' => DriverMock::class,
                ],
                [
                    'user'        => 'foo',
                    'password'    => 'bar',
                    'host'        => 'localhost',
                    'dbname'      => 'baz',
                    'driverClass' => DriverMock::class,
                ],
            ],
            'URL without scheme but default PDO driver and default driver' => [
                [
                    'url'    => '//foo:bar@localhost/baz',
                    'pdo'    => true,
                    'driver' => 'pdo_mysql',
                ],
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
            'URL without scheme but driver and custom driver' => [
                [
                    'url'         => '//foo:bar@localhost/baz',
                    'driver'      => 'pdo_mysql',
                    'driverClass' => DriverMock::class,
                ],
                [
                    'user'        => 'foo',
                    'password'    => 'bar',
                    'host'        => 'localhost',
                    'dbname'      => 'baz',
                    'driverClass' => DriverMock::class,
                ],
            ],
            'URL with default PDO driver' => [
                [
                    'url' => 'mysql://foo:bar@localhost/baz',
                    'pdo' => true,
                ],
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
            'URL with default driver' => [
                [
                    'url'    => 'mysql://foo:bar@localhost/baz',
                    'driver' => 'sqlite',
                ],
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
            'URL with default custom driver' => [
                [
                    'url'         => 'mysql://foo:bar@localhost/baz',
                    'driverClass' => DriverMock::class,
                ],
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
            'URL with default PDO driver and default driver' => [
                [
                    'url'    => 'mysql://foo:bar@localhost/baz',
                    'pdo'    => true,
                    'driver' => 'sqlite',
                ],
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
            'URL with default driver and default custom driver' => [
                [
                    'url'         => 'mysql://foo:bar@localhost/baz',
                    'driver'      => 'sqlite',
                    'driverClass' => DriverMock::class,
                ],
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
            'URL with default PDO driver and default driver and default custom driver' => [
                [
                    'url'         => 'mysql://foo:bar@localhost/baz',
                    'pdo'         => true,
                    'driver'      => 'sqlite',
                    'driverClass' => DriverMock::class,
                ],
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
        ];
460
    }
461
}