Skip to content

Commit 16e6957

Browse files
committed
Merge pull request #3548 from morozov/issues/3545
Remove user provided PDO functionality
2 parents b6ab8df + 708ff44 commit 16e6957

File tree

10 files changed

+29
-245
lines changed

10 files changed

+29
-245
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 PostgreSqlPlatform ForeignKeyConstraint support for `feferred` misspelling removed
48

59
`PostgreSqlPlatform::getAdvancedForeignKeyOptionsSQL()` had a typo in it in 2.x. Both the option name

lib/Doctrine/DBAL/Connection.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,6 @@ public function __construct(
197197
$this->_driver = $driver;
198198
$this->params = $params;
199199

200-
if (isset($params['pdo'])) {
201-
$this->_conn = $params['pdo'];
202-
$this->isConnected = true;
203-
unset($this->params['pdo']);
204-
}
205-
206200
if (isset($params['platform'])) {
207201
if (! $params['platform'] instanceof Platforms\AbstractPlatform) {
208202
throw InvalidPlatformType::new($params['platform']);

lib/Doctrine/DBAL/DriverManager.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver;
1818
use Doctrine\DBAL\Exception\DriverRequired;
1919
use Doctrine\DBAL\Exception\InvalidDriverClass;
20-
use Doctrine\DBAL\Exception\InvalidPdoInstance;
2120
use Doctrine\DBAL\Exception\InvalidWrapperClass;
2221
use Doctrine\DBAL\Exception\UnknownDriver;
23-
use PDO;
2422
use function array_keys;
2523
use function array_map;
2624
use function array_merge;
@@ -175,17 +173,7 @@ public static function getConnection(
175173
}
176174
}
177175

178-
// check for existing pdo object
179-
if (isset($params['pdo']) && ! $params['pdo'] instanceof PDO) {
180-
throw InvalidPdoInstance::new();
181-
}
182-
183-
if (isset($params['pdo'])) {
184-
$params['pdo']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
185-
$params['driver'] = 'pdo_' . $params['pdo']->getAttribute(PDO::ATTR_DRIVER_NAME);
186-
} else {
187-
self::_checkParams($params);
188-
}
176+
self::_checkParams($params);
189177

190178
$className = $params['driverClass'] ?? self::$_driverMap[$params['driver']];
191179

@@ -281,10 +269,6 @@ private static function parseDatabaseUrl(array $params) : array
281269

282270
$url = array_map('rawurldecode', $url);
283271

284-
// If we have a connection URL, we have to unset the default PDO instance connection parameter (if any)
285-
// as we cannot merge connection details from the URL into the PDO instance (URL takes precedence).
286-
unset($params['pdo']);
287-
288272
$params = self::parseDatabaseUrlScheme($url, $params);
289273

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

lib/Doctrine/DBAL/Exception/InvalidPdoInstance.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/Doctrine/Tests/DBAL/ConnectionTest.php

Lines changed: 11 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use Exception;
3030
use PHPUnit\Framework\MockObject\MockObject;
3131
use stdClass;
32-
use function call_user_func_array;
3332

3433
/**
3534
* @requires extension pdo_mysql
@@ -664,82 +663,28 @@ public function testFetchAll() : void
664663
self::assertSame($result, $conn->fetchAll($statement, $params, $types));
665664
}
666665

667-
public function testConnectionDoesNotMaintainTwoReferencesToExternalPDO() : void
668-
{
669-
$params['pdo'] = new stdClass();
670-
671-
$driverMock = $this->createMock(Driver::class);
672-
673-
$conn = new Connection($params, $driverMock);
674-
675-
self::assertArrayNotHasKey('pdo', $conn->getParams(), 'Connection is maintaining additional reference to the PDO connection');
676-
}
677-
678-
public function testPassingExternalPDOMeansConnectionIsConnected() : void
679-
{
680-
$params['pdo'] = new stdClass();
681-
682-
$driverMock = $this->createMock(Driver::class);
683-
684-
$conn = new Connection($params, $driverMock);
685-
686-
self::assertTrue($conn->isConnected(), 'Connection is not connected after passing external PDO');
687-
}
688-
689666
public function testCallingDeleteWithNoDeletionCriteriaResultsInInvalidArgumentException() : void
690667
{
691668
/** @var Driver $driver */
692-
$driver = $this->createMock(Driver::class);
693-
$pdoMock = $this->createMock(\Doctrine\DBAL\Driver\Connection::class);
694-
695-
// should never execute queries with invalid arguments
696-
$pdoMock->expects($this->never())->method('exec');
697-
$pdoMock->expects($this->never())->method('prepare');
698-
699-
$conn = new Connection(['pdo' => $pdoMock], $driver);
669+
$driver = $this->createMock(Driver::class);
670+
$conn = new Connection([], $driver);
700671

701672
$this->expectException(InvalidArgumentException::class);
702673
$conn->delete('kittens', []);
703674
}
704675

705-
/**
706-
* @return array<int, array<int, mixed>>
707-
*/
708-
public static function dataCallConnectOnce() : iterable
709-
{
710-
return [
711-
['delete', ['tbl', ['id' => 12345]]],
712-
['insert', ['tbl', ['data' => 'foo']]],
713-
['update', ['tbl', ['data' => 'bar'], ['id' => 12345]]],
714-
['prepare', ['select * from dual']],
715-
['executeUpdate', ['insert into tbl (id) values (?)'], [123]],
716-
];
717-
}
718-
719-
/**
720-
* @param array<int, mixed> $params
721-
*
722-
* @dataProvider dataCallConnectOnce
723-
*/
724-
public function testCallConnectOnce(string $method, array $params) : void
676+
public function testCallConnectOnce() : void
725677
{
726-
$driverMock = $this->createMock(Driver::class);
727-
$pdoMock = $this->createMock(Connection::class);
728-
$platformMock = $this->createMock(AbstractPlatform::class);
729-
$stmtMock = $this->createMock(Statement::class);
730-
731-
$pdoMock->expects($this->any())
732-
->method('prepare')
733-
->will($this->returnValue($stmtMock));
734-
735-
$conn = $this->getMockBuilder(Connection::class)
736-
->setConstructorArgs([['pdo' => $pdoMock, 'platform' => $platformMock], $driverMock])
737-
->setMethods(['connect'])
738-
->getMock();
678+
/** @var Driver|MockObject $driver */
679+
$driver = $this->createMock(Driver::class);
680+
$driver->expects($this->once())
681+
->method('connect');
739682

740-
$conn->expects($this->once())->method('connect');
683+
$platform = $this->createMock(AbstractPlatform::class);
741684

742-
call_user_func_array([$conn, $method], $params);
685+
$conn = new Connection(['platform' => $platform], $driver);
686+
$conn->connect();
687+
$conn->connect();
743688
}
744689

745690
/**

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

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

11+
/**
12+
* @requires extension pdo
13+
*/
1214
class PDOExceptionTest extends DbalTestCase
1315
{
1416
public const ERROR_CODE = 666;
@@ -33,10 +35,6 @@ class PDOExceptionTest extends DbalTestCase
3335

3436
protected function setUp() : void
3537
{
36-
if (! extension_loaded('PDO')) {
37-
$this->markTestSkipped('PDO is not installed.');
38-
}
39-
4038
parent::setUp();
4139

4240
$this->wrappedException = new \PDOException(self::MESSAGE);

0 commit comments

Comments
 (0)