Skip to content

Commit

Permalink
merge 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Nov 15, 2019
2 parents 52ed260 + b485fe6 commit 51c6f11
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ var
vendor
.php_cs.cache
php-cs-fixer.phar
.phpunit.result.cache
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"require": {
"php": "^7.2",
"symfony/framework-bundle": "^3.4 || ^4.3 || ^5.0",
"doctrine/dbal": "^2.9",
"doctrine/dbal": "^2.9,>=2.9.3",
"doctrine/doctrine-bundle": "^1.11 || ^2.0"
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace DAMA\DoctrineTestBundle\Doctrine\DBAL;

use Doctrine\DBAL\Event\ConnectionEventArgs;

class PostConnectEventListener
{
public function postConnect(ConnectionEventArgs $args): void
{
// The underlying connection already has a transaction started.
// We start a transaction on the connection as well
// so the internal state ($_transactionNestingLevel) is in sync with the underlying connection.
$args->getConnection()->beginTransaction();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Events;

class StaticConnectionFactory extends ConnectionFactory
{
Expand Down Expand Up @@ -37,15 +38,12 @@ public function createConnection(array $params, Configuration $config = null, Ev
);

if (StaticDriver::isKeepStaticConnections()) {
// The underlying connection already has a transaction started.
$connection->getEventManager()->addEventListener(Events::postConnect, new PostConnectEventListener());

// Make sure we use savepoints to be able to easily roll-back nested transactions
if ($connection->getDriver()->getDatabasePlatform()->supportsSavepoints()) {
$connection->setNestTransactionsWithSavepoints(true);
}

// We start a transaction on the connection as well
// so the internal state ($_transactionNestingLevel) is in sync with the underlying connection.
$connection->beginTransaction();
}

return $connection;
Expand Down
2 changes: 1 addition & 1 deletion src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace DAMA\DoctrineTestBundle\Doctrine\DBAL;

use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\DriverException;
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function testCreateConnection(bool $keepStaticConnections, int $expectedN
]);

$this->assertInstanceOf(StaticDriver::class, $connection->getDriver());
$this->assertSame(0, $connection->getTransactionNestingLevel());

$connection->connect();
$this->assertSame($expectedNestingLevel, $connection->getTransactionNestingLevel());
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@ public function testPreviousChangesAreRolledBackAfterUsingSavePoint(): void
{
$this->assertRowCount(0);
}

public function testRollBackChangesWithReOpenedConnection(): void
{
$this->connection->close();
$this->connection->beginTransaction();
$this->connection->commit();
$this->assertRowCount(0);
}
}

0 comments on commit 51c6f11

Please sign in to comment.