DriverManagerTest.php 15.1 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;
Luís Cobucci's avatar
Luís Cobucci committed
16

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

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

37
        self::assertEquals('sqlite', $conn->getDatabasePlatform()->getName());
jwage's avatar
jwage committed
38 39
    }

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

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

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

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

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

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

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

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

Sergei Morozov's avatar
Sergei Morozov committed
97
        $conn = DriverManager::getConnection($options);
98
        self::assertInstanceOf($wrapperClass, $conn);
99 100
    }

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

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

Sergei Morozov's avatar
Sergei Morozov committed
113
        DriverManager::getConnection($options);
114 115 116 117
    }

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

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

Sergei Morozov's avatar
Sergei Morozov committed
122
        DriverManager::getConnection($options);
123 124 125 126
    }

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

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

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

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

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

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

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

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

156 157
        $params = $conn->getParams();
        foreach ($expected as $key => $value) {
Sergei Morozov's avatar
Sergei Morozov committed
158
            if (in_array($key, ['pdo', 'driver', 'driverClass'], true)) {
159
                self::assertInstanceOf($value, $conn->getDriver());
160
            } else {
161
                self::assertEquals($value, $params[$key]);
162 163 164
            }
        }
    }
165

166 167
    public function databaseUrls()
    {
Sergei Morozov's avatar
Sergei Morozov committed
168 169
        return [
            'simple URL' => [
170
                'mysql://foo:bar@localhost/baz',
Sergei Morozov's avatar
Sergei Morozov committed
171 172 173 174 175 176 177 178 179
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
            'simple URL with port' => [
180
                'mysql://foo:bar@localhost:11211/baz',
Sergei Morozov's avatar
Sergei Morozov committed
181 182 183 184 185 186 187 188 189 190
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'port'     => 11211,
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
            'sqlite relative URL with host' => [
191
                'sqlite://localhost/foo/dbname.sqlite',
Sergei Morozov's avatar
Sergei Morozov committed
192 193 194 195 196 197
                [
                    'path'   => 'foo/dbname.sqlite',
                    'driver' => PDOSqliteDriver::class,
                ],
            ],
            'sqlite absolute URL with host' => [
198
                'sqlite://localhost//tmp/dbname.sqlite',
Sergei Morozov's avatar
Sergei Morozov committed
199 200 201 202 203 204
                [
                    'path'   => '/tmp/dbname.sqlite',
                    'driver' => PDOSqliteDriver::class,
                ],
            ],
            'sqlite relative URL without host' => [
205
                'sqlite:///foo/dbname.sqlite',
Sergei Morozov's avatar
Sergei Morozov committed
206 207 208 209 210 211
                [
                    'path'   => 'foo/dbname.sqlite',
                    'driver' => PDOSqliteDriver::class,
                ],
            ],
            'sqlite absolute URL without host' => [
212
                'sqlite:////tmp/dbname.sqlite',
Sergei Morozov's avatar
Sergei Morozov committed
213 214 215 216 217 218
                [
                    'path'   => '/tmp/dbname.sqlite',
                    'driver' => PDOSqliteDriver::class,
                ],
            ],
            'sqlite memory' => [
219
                'sqlite:///:memory:',
Sergei Morozov's avatar
Sergei Morozov committed
220 221 222 223 224 225
                [
                    'memory' => true,
                    'driver' => PDOSqliteDriver::class,
                ],
            ],
            'sqlite memory with host' => [
226
                'sqlite://localhost/:memory:',
Sergei Morozov's avatar
Sergei Morozov committed
227 228 229 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
                [
                    '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' => [
260
                'url' => 'mysql://foo:bar@localhost/dbname?charset=UTF-8',
Sergei Morozov's avatar
Sergei Morozov committed
261 262 263
                ['charset' => 'UTF-8'],
            ],
            'simple URL with fallthrough scheme not defined in map' => [
264
                'sqlsrv://foo:bar@localhost/baz',
Sergei Morozov's avatar
Sergei Morozov committed
265 266 267 268 269 270 271 272 273
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => SQLSrvDriver::class,
                ],
            ],
            'simple URL with fallthrough scheme containing underscores fails' => [
274 275
                'drizzle_pdo_mysql://foo:bar@localhost/baz',
                false,
Sergei Morozov's avatar
Sergei Morozov committed
276 277
            ],
            'simple URL with fallthrough scheme containing dashes works' => [
278
                'drizzle-pdo-mysql://foo:bar@localhost/baz',
Sergei Morozov's avatar
Sergei Morozov committed
279 280 281 282 283 284 285 286 287
                [
                    'user'     => 'foo',
                    'password' => 'bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => DrizzlePDOMySqlDriver::class,
                ],
            ],
            'simple URL with percent encoding' => [
288
                'mysql://foo%3A:bar%2F@localhost/baz+baz%40',
Sergei Morozov's avatar
Sergei Morozov committed
289 290 291 292 293 294 295 296 297
                [
                    'user'     => 'foo:',
                    'password' => 'bar/',
                    'host'     => 'localhost',
                    'dbname'   => 'baz+baz@',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
            'simple URL with percent sign in password' => [
298
                'mysql://foo:bar%25bar@localhost/baz',
Sergei Morozov's avatar
Sergei Morozov committed
299 300 301 302 303 304 305 306
                [
                    'user'     => 'foo',
                    'password' => 'bar%bar',
                    'host'     => 'localhost',
                    'dbname'   => 'baz',
                    'driver'   => PDOMySQLDriver::class,
                ],
            ],
307 308

            // DBAL-1234
Sergei Morozov's avatar
Sergei Morozov committed
309 310
            'URL without scheme and without any driver information' => [
                ['url' => '//foo:bar@localhost/baz'],
311
                false,
Sergei Morozov's avatar
Sergei Morozov committed
312 313 314 315 316 317
            ],
            'URL without scheme but default PDO driver' => [
                [
                    'url' => '//foo:bar@localhost/baz',
                    'pdo' => true,
                ],
318
                false,
Sergei Morozov's avatar
Sergei Morozov committed
319 320 321 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
            ],
            '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,
                ],
            ],
        ];
457
    }
458
}