From f22ea0637f579b26a92a461c5ebf4e016e30e3b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Thu, 11 Nov 2021 16:42:38 +0100 Subject: [PATCH 1/2] Drop useless currentWorkingDirectory variable It is not like this could work from another directory than the root of the project. --- phpstan-common.neon.dist | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/phpstan-common.neon.dist b/phpstan-common.neon.dist index 7a41be19d..464e03490 100644 --- a/phpstan-common.neon.dist +++ b/phpstan-common.neon.dist @@ -1,28 +1,28 @@ parameters: level: 7 paths: - - %currentWorkingDirectory%/lib - - %currentWorkingDirectory%/tests + - lib + - tests excludePaths: - - %currentWorkingDirectory%/tests/Doctrine/Migrations/Tests/Configuration/ConfigurationTestSource/Migrations/Version123.php + - tests/Doctrine/Migrations/Tests/Configuration/ConfigurationTestSource/Migrations/Version123.php ignoreErrors: - '~Variable method call on Doctrine\\Migrations\\AbstractMigration~' - message: '~^Call to function in_array\(\) requires parameter #3 to be true\.$~' - path: %currentWorkingDirectory%/lib/Doctrine/Migrations/Version/SortedMigrationPlanCalculator.php + path: lib/Doctrine/Migrations/Version/SortedMigrationPlanCalculator.php - message: '~^Variable property access on SimpleXMLElement\.$~' - path: %currentWorkingDirectory%/lib/Doctrine/Migrations/Configuration/Migration/XmlFile.php + path: lib/Doctrine/Migrations/Configuration/Migration/XmlFile.php - message: '~^Call to function is_bool\(\) with bool will always evaluate to true\.$~' - path: %currentWorkingDirectory%/lib/Doctrine/Migrations/InlineParameterFormatter.php + path: lib/Doctrine/Migrations/InlineParameterFormatter.php - message: '~^Call to an undefined method Symfony\\Component\\Console\\Output\\OutputInterface\:\:getErrorOutput\(\)\.$~' - path: %currentWorkingDirectory%/lib/Doctrine/Migrations/Tools/Console/ConsoleLogger.php + path: lib/Doctrine/Migrations/Tools/Console/ConsoleLogger.php - message: '~^Method Doctrine\\Migrations\\Tests\\Stub\\DoctrineRegistry::getService\(\) should return Doctrine\\Persistence\\ObjectManager but returns Doctrine\\DBAL\\Connection\|Doctrine\\ORM\\EntityManager~' - path: %currentWorkingDirectory%/tests/Doctrine/Migrations/Tests/Stub/DoctrineRegistry.php + path: tests/Doctrine/Migrations/Tests/Stub/DoctrineRegistry.php - '~Call to method getVersion\(\) of deprecated class PackageVersions\\Versions\:.*~' # Requires PHPUnit 9 From c9e0386e6f074539bc310a7c38cd39a17518eb36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Thu, 11 Nov 2021 16:33:50 +0100 Subject: [PATCH 2/2] Look harder for a native connection 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 --- lib/Doctrine/Migrations/Tools/TransactionHelper.php | 9 +++++++-- phpstan-common.neon.dist | 5 +++++ .../Tests/Event/Listeners/AutoCommitListenerTest.php | 7 ++++--- tests/Doctrine/Migrations/Tests/MigratorTest.php | 6 +++++- tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php | 7 +++++-- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/Doctrine/Migrations/Tools/TransactionHelper.php b/lib/Doctrine/Migrations/Tools/TransactionHelper.php index bd6752458..ebd9007d8 100644 --- a/lib/Doctrine/Migrations/Tools/TransactionHelper.php +++ b/lib/Doctrine/Migrations/Tools/TransactionHelper.php @@ -8,6 +8,8 @@ use Doctrine\Deprecations\Deprecation; use PDO; +use function method_exists; + /** * @internal */ @@ -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(); } } diff --git a/phpstan-common.neon.dist b/phpstan-common.neon.dist index 464e03490..92380af70 100644 --- a/phpstan-common.neon.dist +++ b/phpstan-common.neon.dist @@ -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~' diff --git a/tests/Doctrine/Migrations/Tests/Event/Listeners/AutoCommitListenerTest.php b/tests/Doctrine/Migrations/Tests/Event/Listeners/AutoCommitListenerTest.php index 1f285c0e5..169079604 100644 --- a/tests/Doctrine/Migrations/Tests/Event/Listeners/AutoCommitListenerTest.php +++ b/tests/Doctrine/Migrations/Tests/Event/Listeners/AutoCommitListenerTest.php @@ -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; @@ -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(); } diff --git a/tests/Doctrine/Migrations/Tests/MigratorTest.php b/tests/Doctrine/Migrations/Tests/MigratorTest.php index ce8a9f6c5..f4d0685c3 100644 --- a/tests/Doctrine/Migrations/Tests/MigratorTest.php +++ b/tests/Doctrine/Migrations/Tests/MigratorTest.php @@ -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; @@ -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(); diff --git a/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php b/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php index b6715860e..a35862f21 100644 --- a/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php +++ b/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php @@ -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; @@ -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);