Skip to content

Commit dcc3f2d

Browse files
committed
[DBAL-3548] Remove user provided PDO functionality
1 parent d9c4e3d commit dcc3f2d

File tree

8 files changed

+26
-224
lines changed

8 files changed

+26
-224
lines changed

UPGRADE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Upgrade to 3.0
22

3+
## BC BREAK User-provided `PDO` instance is no longer supported
4+
5+
In order to share the same `PDO` instances between DBAL and other components, initialize the connection in DBAL and access it using `Connection::getWrappedConnection()->getWrappedConnection()`.
6+
37
## BC BREAK: the PDO symbols are no longer part of the DBAL API
48

59
1. The support of `PDO::PARAM_*`, `PDO::FETCH_*`, `PDO::CASE_*` and `PDO::PARAM_INPUT_OUTPUT` constants in the DBAL API is removed.

lib/Doctrine/DBAL/Connection.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,6 @@ public function __construct(
187187
$this->_driver = $driver;
188188
$this->params = $params;
189189

190-
if (isset($params['pdo'])) {
191-
$this->_conn = $params['pdo'];
192-
$this->isConnected = true;
193-
unset($this->params['pdo']);
194-
}
195-
196190
if (isset($params['platform'])) {
197191
if (! $params['platform'] instanceof Platforms\AbstractPlatform) {
198192
throw DBALException::invalidPlatformType($params['platform']);

lib/Doctrine/DBAL/DriverManager.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as PDOSQLSrvDriver;
1515
use Doctrine\DBAL\Driver\SQLAnywhere\Driver as SQLAnywhereDriver;
1616
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver;
17-
use PDO;
1817
use function array_keys;
1918
use function array_map;
2019
use function array_merge;
@@ -171,17 +170,7 @@ public static function getConnection(
171170
}
172171
}
173172

174-
// check for existing pdo object
175-
if (isset($params['pdo']) && ! $params['pdo'] instanceof PDO) {
176-
throw DBALException::invalidPdoInstance();
177-
}
178-
179-
if (isset($params['pdo'])) {
180-
$params['pdo']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
181-
$params['driver'] = 'pdo_' . $params['pdo']->getAttribute(PDO::ATTR_DRIVER_NAME);
182-
} else {
183-
self::_checkParams($params);
184-
}
173+
self::_checkParams($params);
185174

186175
$className = $params['driverClass'] ?? self::$_driverMap[$params['driver']];
187176

@@ -277,10 +266,6 @@ private static function parseDatabaseUrl(array $params) : array
277266

278267
$url = array_map('rawurldecode', $url);
279268

280-
// If we have a connection URL, we have to unset the default PDO instance connection parameter (if any)
281-
// as we cannot merge connection details from the URL into the PDO instance (URL takes precedence).
282-
unset($params['pdo']);
283-
284269
$params = self::parseDatabaseUrlScheme($url, $params);
285270

286271
if (isset($url['host'])) {

tests/Doctrine/Tests/DBAL/ConnectionTest.php

Lines changed: 11 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use Exception;
2828
use PHPUnit\Framework\MockObject\MockObject;
2929
use stdClass;
30-
use function call_user_func_array;
3130

3231
/**
3332
* @requires extension pdo_mysql
@@ -686,82 +685,28 @@ public function testFetchAll() : void
686685
self::assertSame($result, $conn->fetchAll($statement, $params, $types));
687686
}
688687

689-
public function testConnectionDoesNotMaintainTwoReferencesToExternalPDO() : void
690-
{
691-
$params['pdo'] = new stdClass();
692-
693-
$driverMock = $this->createMock(Driver::class);
694-
695-
$conn = new Connection($params, $driverMock);
696-
697-
self::assertArrayNotHasKey('pdo', $conn->getParams(), 'Connection is maintaining additional reference to the PDO connection');
698-
}
699-
700-
public function testPassingExternalPDOMeansConnectionIsConnected() : void
701-
{
702-
$params['pdo'] = new stdClass();
703-
704-
$driverMock = $this->createMock(Driver::class);
705-
706-
$conn = new Connection($params, $driverMock);
707-
708-
self::assertTrue($conn->isConnected(), 'Connection is not connected after passing external PDO');
709-
}
710-
711688
public function testCallingDeleteWithNoDeletionCriteriaResultsInInvalidArgumentException() : void
712689
{
713690
/** @var Driver $driver */
714-
$driver = $this->createMock(Driver::class);
715-
$pdoMock = $this->createMock(\Doctrine\DBAL\Driver\Connection::class);
716-
717-
// should never execute queries with invalid arguments
718-
$pdoMock->expects($this->never())->method('exec');
719-
$pdoMock->expects($this->never())->method('prepare');
720-
721-
$conn = new Connection(['pdo' => $pdoMock], $driver);
691+
$driver = $this->createMock(Driver::class);
692+
$conn = new Connection([], $driver);
722693

723694
$this->expectException(InvalidArgumentException::class);
724695
$conn->delete('kittens', []);
725696
}
726697

727-
/**
728-
* @return array<int, array<int, mixed>>
729-
*/
730-
public static function dataCallConnectOnce() : iterable
731-
{
732-
return [
733-
['delete', ['tbl', ['id' => 12345]]],
734-
['insert', ['tbl', ['data' => 'foo']]],
735-
['update', ['tbl', ['data' => 'bar'], ['id' => 12345]]],
736-
['prepare', ['select * from dual']],
737-
['executeUpdate', ['insert into tbl (id) values (?)'], [123]],
738-
];
739-
}
740-
741-
/**
742-
* @param array<int, mixed> $params
743-
*
744-
* @dataProvider dataCallConnectOnce
745-
*/
746-
public function testCallConnectOnce(string $method, array $params) : void
698+
public function testCallConnectOnce() : void
747699
{
748-
$driverMock = $this->createMock(Driver::class);
749-
$pdoMock = $this->createMock(Connection::class);
750-
$platformMock = $this->createMock(AbstractPlatform::class);
751-
$stmtMock = $this->createMock(Statement::class);
752-
753-
$pdoMock->expects($this->any())
754-
->method('prepare')
755-
->will($this->returnValue($stmtMock));
756-
757-
$conn = $this->getMockBuilder(Connection::class)
758-
->setConstructorArgs([['pdo' => $pdoMock, 'platform' => $platformMock], $driverMock])
759-
->onlyMethods(['connect'])
760-
->getMock();
700+
/** @var Driver|MockObject $driver */
701+
$driver = $this->createMock(Driver::class);
702+
$driver->expects($this->once())
703+
->method('connect');
761704

762-
$conn->expects($this->once())->method('connect');
705+
$platform = $this->createMock(AbstractPlatform::class);
763706

764-
call_user_func_array([$conn, $method], $params);
707+
$conn = new Connection(['platform' => $platform], $driver);
708+
$conn->connect();
709+
$conn->connect();
765710
}
766711

767712
/**

tests/Doctrine/Tests/DBAL/Driver/PDOExceptionTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use Doctrine\DBAL\Driver\PDOException;
66
use Doctrine\Tests\DbalTestCase;
77
use PHPUnit\Framework\MockObject\MockObject;
8-
use function extension_loaded;
98

9+
/**
10+
* @requires extension pdo
11+
*/
1012
class PDOExceptionTest extends DbalTestCase
1113
{
1214
public const ERROR_CODE = 666;
@@ -31,10 +33,6 @@ class PDOExceptionTest extends DbalTestCase
3133

3234
protected function setUp() : void
3335
{
34-
if (! extension_loaded('PDO')) {
35-
$this->markTestSkipped('PDO is not installed.');
36-
}
37-
3836
parent::setUp();
3937

4038
$this->wrappedException = new \PDOException(self::MESSAGE, self::SQLSTATE);

tests/Doctrine/Tests/DBAL/DriverManagerTest.php

Lines changed: 4 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,13 @@
1515
use Doctrine\DBAL\Sharding\PoolingShardConnection;
1616
use Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser;
1717
use Doctrine\Tests\DbalTestCase;
18-
use PDO;
1918
use stdClass;
20-
use function extension_loaded;
2119
use function get_class;
2220
use function in_array;
2321
use function is_array;
2422

2523
class DriverManagerTest extends DbalTestCase
2624
{
27-
/**
28-
* @requires extension pdo_sqlite
29-
*/
30-
public function testInvalidPdoInstance() : void
31-
{
32-
$this->expectException(DBALException::class);
33-
DriverManager::getConnection(['pdo' => 'test']);
34-
}
35-
36-
/**
37-
* @requires extension pdo_sqlite
38-
*/
39-
public function testValidPdoInstance() : void
40-
{
41-
$conn = DriverManager::getConnection([
42-
'pdo' => new PDO('sqlite::memory:'),
43-
]);
44-
45-
self::assertEquals('sqlite', $conn->getDatabasePlatform()->getName());
46-
}
47-
48-
/**
49-
* @group DBAL-32
50-
* @requires extension pdo_sqlite
51-
*/
52-
public function testPdoInstanceSetErrorMode() : void
53-
{
54-
$pdo = new PDO('sqlite::memory:');
55-
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
56-
$options = ['pdo' => $pdo];
57-
58-
DriverManager::getConnection($options);
59-
self::assertEquals(PDO::ERRMODE_EXCEPTION, $pdo->getAttribute(PDO::ATTR_ERRMODE));
60-
}
61-
6225
public function testCheckParams() : void
6326
{
6427
$this->expectException(DBALException::class);
@@ -80,7 +43,7 @@ public function testCustomPlatform() : void
8043
{
8144
$platform = $this->createMock(AbstractPlatform::class);
8245
$options = [
83-
'pdo' => new PDO('sqlite::memory:'),
46+
'url' => 'sqlite::memory:',
8447
'platform' => $platform,
8548
];
8649

@@ -97,7 +60,7 @@ public function testCustomWrapper() : void
9760
$wrapperClass = get_class($wrapper);
9861

9962
$options = [
100-
'pdo' => new PDO('sqlite::memory:'),
63+
'url' => 'sqlite::memory:',
10164
'wrapperClass' => $wrapperClass,
10265
];
10366

@@ -113,7 +76,7 @@ public function testInvalidWrapperClass() : void
11376
$this->expectException(DBALException::class);
11477

11578
$options = [
116-
'pdo' => new PDO('sqlite::memory:'),
79+
'url' => 'sqlite::memory:',
11780
'wrapperClass' => stdClass::class,
11881
];
11982

@@ -215,16 +178,6 @@ public function testDatabaseUrl($url, $expected) : void
215178
{
216179
$options = is_array($url) ? $url : ['url' => $url];
217180

218-
if (isset($options['pdo'])) {
219-
if (! extension_loaded('pdo')) {
220-
$this->markTestSkipped('PDO is not installed');
221-
}
222-
223-
$options['pdo'] = $this->createMock(PDO::class);
224-
}
225-
226-
$options = is_array($url) ? $url : ['url' => $url];
227-
228181
if ($expected === false) {
229182
$this->expectException(DBALException::class);
230183
}
@@ -233,7 +186,7 @@ public function testDatabaseUrl($url, $expected) : void
233186

234187
$params = $conn->getParams();
235188
foreach ($expected as $key => $value) {
236-
if (in_array($key, ['pdo', 'driver', 'driverClass'], true)) {
189+
if (in_array($key, ['driver', 'driverClass'], true)) {
237190
self::assertInstanceOf($value, $conn->getDriver());
238191
} else {
239192
self::assertEquals($value, $params[$key]);
@@ -394,13 +347,6 @@ public function databaseUrls() : iterable
394347
['url' => '//foo:bar@localhost/baz'],
395348
false,
396349
],
397-
'URL without scheme but default PDO driver' => [
398-
[
399-
'url' => '//foo:bar@localhost/baz',
400-
'pdo' => true,
401-
],
402-
false,
403-
],
404350
'URL without scheme but default driver' => [
405351
[
406352
'url' => '//foo:bar@localhost/baz',
@@ -427,20 +373,6 @@ public function databaseUrls() : iterable
427373
'driverClass' => $driverClass,
428374
],
429375
],
430-
'URL without scheme but default PDO driver and default driver' => [
431-
[
432-
'url' => '//foo:bar@localhost/baz',
433-
'pdo' => true,
434-
'driver' => 'pdo_mysql',
435-
],
436-
[
437-
'user' => 'foo',
438-
'password' => 'bar',
439-
'host' => 'localhost',
440-
'dbname' => 'baz',
441-
'driver' => PDOMySQLDriver::class,
442-
],
443-
],
444376
'URL without scheme but driver and custom driver' => [
445377
[
446378
'url' => '//foo:bar@localhost/baz',
@@ -455,19 +387,6 @@ public function databaseUrls() : iterable
455387
'driverClass' => $driverClass,
456388
],
457389
],
458-
'URL with default PDO driver' => [
459-
[
460-
'url' => 'mysql://foo:bar@localhost/baz',
461-
'pdo' => true,
462-
],
463-
[
464-
'user' => 'foo',
465-
'password' => 'bar',
466-
'host' => 'localhost',
467-
'dbname' => 'baz',
468-
'driver' => PDOMySQLDriver::class,
469-
],
470-
],
471390
'URL with default driver' => [
472391
[
473392
'url' => 'mysql://foo:bar@localhost/baz',
@@ -494,20 +413,6 @@ public function databaseUrls() : iterable
494413
'driver' => PDOMySQLDriver::class,
495414
],
496415
],
497-
'URL with default PDO driver and default driver' => [
498-
[
499-
'url' => 'mysql://foo:bar@localhost/baz',
500-
'pdo' => true,
501-
'driver' => 'sqlite',
502-
],
503-
[
504-
'user' => 'foo',
505-
'password' => 'bar',
506-
'host' => 'localhost',
507-
'dbname' => 'baz',
508-
'driver' => PDOMySQLDriver::class,
509-
],
510-
],
511416
'URL with default driver and default custom driver' => [
512417
[
513418
'url' => 'mysql://foo:bar@localhost/baz',
@@ -522,21 +427,6 @@ public function databaseUrls() : iterable
522427
'driver' => PDOMySQLDriver::class,
523428
],
524429
],
525-
'URL with default PDO driver and default driver and default custom driver' => [
526-
[
527-
'url' => 'mysql://foo:bar@localhost/baz',
528-
'pdo' => true,
529-
'driver' => 'sqlite',
530-
'driverClass' => $driverClass,
531-
],
532-
[
533-
'user' => 'foo',
534-
'password' => 'bar',
535-
'host' => 'localhost',
536-
'dbname' => 'baz',
537-
'driver' => PDOMySQLDriver::class,
538-
],
539-
],
540430
];
541431
}
542432
}

0 commit comments

Comments
 (0)