-
-
Notifications
You must be signed in to change notification settings - Fork 389
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
tests/Doctrine/Migrations/Tests/Tools/TransactionHelperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 list<array{class-string<DriverConnection>, bool}> | ||
*/ | ||
public function getDriverConnectionClassesImplementingInTransactionMethod(): array | ||
{ | ||
return [ | ||
[PDOConnection::class, true], | ||
[PDOConnection::class, false], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
extendsPDO
, it is deprecated in favor ofPDO\Connection
which no longer does in DBAL 3. That means your fix is needed regardless, and that implementinginTransaction
inPDO\Connection
will be necessary.There was a problem hiding this comment.
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)