Skip to content

Commit

Permalink
Look harder for a native connection
Browse files Browse the repository at this point in the history
doctrine/dbal 3 switches from inheritance to composition but still
provides an accessor for PDO, and it is named like the method in
Connection.
Unwrapping until that method no longer exists should give the innermost
connection regardless of the DBAL version

Fixes #1209
  • Loading branch information
greg0ire committed Nov 11, 2021
1 parent f22ea06 commit c9e0386
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
9 changes: 7 additions & 2 deletions lib/Doctrine/Migrations/Tools/TransactionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Doctrine\Deprecations\Deprecation;
use PDO;

use function method_exists;

/**
* @internal
*/
Expand Down Expand Up @@ -57,10 +59,13 @@ public static function rollbackIfInTransaction(Connection $connection): void

private static function inTransaction(Connection $connection): bool
{
$wrappedConnection = $connection->getWrappedConnection();
$innermostConnection = $connection;
while (method_exists($innermostConnection, 'getWrappedConnection')) {
$innermostConnection = $innermostConnection->getWrappedConnection();
}

/* Attempt to commit or rollback while no transaction is running
results in an exception since PHP 8 + pdo_mysql combination */
return ! $wrappedConnection instanceof PDO || $wrappedConnection->inTransaction();
return ! $innermostConnection instanceof PDO || $innermostConnection->inTransaction();
}
}
5 changes: 5 additions & 0 deletions phpstan-common.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ parameters:
path: tests/Doctrine/Migrations/Tests/Stub/DoctrineRegistry.php
- '~Call to method getVersion\(\) of deprecated class PackageVersions\\Versions\:.*~'

# https://github.com/phpstan/phpstan/issues/5982
-
message: '~^Cannot call method getWrappedConnection\(\) on class-string\|object\.~'
path: lib/Doctrine/Migrations/Tools/TransactionHelper.php

# Requires PHPUnit 9
-
message: '~assert.*Reg~'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\Migrations\Tests\Event\Listeners;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\Migrations\Event\Listeners\AutoCommitListener;
use Doctrine\Migrations\Event\MigrationsEventArgs;
use Doctrine\Migrations\Metadata\MigrationPlanList;
Expand Down Expand Up @@ -51,9 +52,9 @@ public function testListenerDoesFinalCommitWhenAutoCommitIsOff(): void

protected function setUp(): void
{
$this->conn = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->getMock();
$this->conn = $this->createStub(Connection::class);
$driverConnection = $this->createStub(DriverConnection::class);
$this->conn->method('getWrappedConnection')->willReturn($driverConnection);

$this->listener = new AutoCommitListener();
}
Expand Down
6 changes: 5 additions & 1 deletion tests/Doctrine/Migrations/Tests/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\DbalMigrator;
use Doctrine\Migrations\EventDispatcher;
Expand Down Expand Up @@ -54,7 +55,10 @@ class MigratorTest extends MigrationTestCase

protected function setUp(): void
{
$this->conn = $this->createMock(Connection::class);
$this->conn = $this->createMock(Connection::class);
$driverConnection = $this->createStub(DriverConnection::class);
$this->conn->method('getWrappedConnection')->willReturn($driverConnection);

$this->config = new Configuration();

$this->migratorConfiguration = new MigratorConfiguration();
Expand Down
7 changes: 5 additions & 2 deletions tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\Migrations\EventDispatcher;
use Doctrine\Migrations\Events;
use Doctrine\Migrations\Metadata\MigrationPlan;
Expand Down Expand Up @@ -518,8 +519,10 @@ public function executeDownShouldAppendDescriptionWhenItIsNotEmpty(): void

protected function setUp(): void
{
$this->metadataStorage = $this->createMock(MetadataStorage::class);
$this->connection = $this->createMock(Connection::class);
$this->metadataStorage = $this->createMock(MetadataStorage::class);
$this->connection = $this->createMock(Connection::class);
$driverConnection = $this->createStub(DriverConnection::class);
$this->connection->method('getWrappedConnection')->willReturn($driverConnection);
$this->schemaDiffProvider = $this->createMock(SchemaDiffProvider::class);
$this->parameterFormatter = $this->createMock(ParameterFormatter::class);

Expand Down

0 comments on commit c9e0386

Please sign in to comment.