Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always use the inTransaction() method when possible in TransactionHelper #1149

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/Doctrine/Migrations/Tools/TransactionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace Doctrine\Migrations\Tools;

use Doctrine\DBAL\Connection;
use PDO;

use function method_exists;

/**
* @internal
Expand Down Expand Up @@ -36,6 +37,10 @@ private static function inTransaction(Connection $connection): bool

/* 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();
if (method_exists($wrappedConnection, 'inTransaction')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only now do I notice that while PDOConnection extends PDO, it is deprecated in favor of PDO\Connection which no longer does in DBAL 3. That means your fix is needed regardless, and that implementing inTransaction in PDO\Connection will be necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but this lib does not support yet the newer version of the DBAL so for now this is a non-issue, and this code is future-proof if the method name is the one used in the implementation of doctrine/dbal#4616. A future iteration on this piece of code may of course change the check to use the more reliable instanceof TransactionAwareDriverConnection (or whatever name it will have)

return $wrappedConnection->inTransaction();
}

return true;
}
}
97 changes: 97 additions & 0 deletions tests/Doctrine/Migrations/Tests/Tools/TransactionHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Tests\Tools;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\PDO\Connection as PDOConnection;
use Doctrine\Migrations\Tools\TransactionHelper;
use PHPUnit\Framework\TestCase;

final class TransactionHelperTest extends TestCase
{
/**
* @param class-string $driverConnectionFqcn
ste93cry marked this conversation as resolved.
Show resolved Hide resolved
*
* @dataProvider getDriverConnectionClassesImplementingInTransactionMethod
*/
public function testCommitIfInTransactionWithConnectionImplementingInTransactionMethod(string $driverConnectionFqcn, bool $commitExpected): void
{
$wrappedConnection = $this->createMock($driverConnectionFqcn);
$wrappedConnection->expects(self::once())
->method('inTransaction')
->willReturn($commitExpected);

$connection = $this->createMock(Connection::class);
$connection->expects(self::once())
->method('getWrappedConnection')
->willReturn($wrappedConnection);

$connection->expects($commitExpected ? self::once() : self::never())
->method('commit');

TransactionHelper::commitIfInTransaction($connection);
}

public function testCommitIfInTransactionWithConnectionNotImplementingInTransactionMethod(): void
{
$connection = $this->createMock(Connection::class);
$connection->expects(self::once())
->method('getWrappedConnection')
->willReturn($this->createMock(DriverConnection::class));

$connection->expects(self::once())
->method('commit');

TransactionHelper::commitIfInTransaction($connection);
}

/**
* @param class-string $driverConnectionFqcn
ste93cry marked this conversation as resolved.
Show resolved Hide resolved
*
* @dataProvider getDriverConnectionClassesImplementingInTransactionMethod
*/
public function testRollbackIfInTransactionWithConnectionImplementingInTransactionMethod(string $driverConnectionFqcn, bool $commitExpected): void
{
$wrappedConnection = $this->createMock($driverConnectionFqcn);
$wrappedConnection->expects(self::once())
->method('inTransaction')
->willReturn($commitExpected);

$connection = $this->createMock(Connection::class);
$connection->expects(self::once())
->method('getWrappedConnection')
->willReturn($wrappedConnection);

$connection->expects($commitExpected ? self::once() : self::never())
->method('rollback');

TransactionHelper::rollbackIfInTransaction($connection);
}

public function testRollbackIfInTransactionWithConnectionNotImplementingInTransactionMethod(): void
{
$connection = $this->createMock(Connection::class);
$connection->expects(self::once())
->method('getWrappedConnection')
->willReturn($this->createMock(DriverConnection::class));

$connection->expects(self::once())
->method('rollback');

TransactionHelper::rollbackIfInTransaction($connection);
}

/**
* @return mixed[]
ste93cry marked this conversation as resolved.
Show resolved Hide resolved
*/
public function getDriverConnectionClassesImplementingInTransactionMethod(): array
{
return [
[PDOConnection::class, true],
[PDOConnection::class, false],
];
}
}