Skip to content

Commit

Permalink
Add commit and roolback result bool plus tests
Browse files Browse the repository at this point in the history
Add commit result bool

update roolback result bool and unit test PDO commit and rollback return bool

add assert for commit and rollback on tests

update tests for dbal connection

[WIP] Add return type hint and sort use pdo

[WIP] Fix functional tests
  • Loading branch information
otazniksk committed Jun 19, 2019
1 parent 97b7305 commit 9134cd4
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 10 deletions.
20 changes: 16 additions & 4 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,8 @@ public function beginTransaction()
/**
* {@inheritDoc}
*
* @return bool
*
* @throws ConnectionException If the commit failed due to no active transaction or
* because the transaction was marked for rollback only.
*/
Expand All @@ -1266,6 +1268,8 @@ public function commit()
throw ConnectionException::commitFailedRollbackOnly();
}

$result = true;

$connection = $this->getWrappedConnection();

$logger = $this->_config->getSQLLogger();
Expand All @@ -1275,7 +1279,7 @@ public function commit()
$logger->startQuery('"COMMIT"');
}

$connection->commit();
$result = $connection->commit();

if ($logger) {
$logger->stopQuery();
Expand All @@ -1293,12 +1297,12 @@ public function commit()
--$this->transactionNestingLevel;

if ($this->autoCommit !== false || $this->transactionNestingLevel !== 0) {
return true;
return $result;
}

$this->beginTransaction();

return true;
return $result;
}

/**
Expand All @@ -1322,6 +1326,8 @@ private function commitAll()
/**
* Cancels any database changes done during the current transaction.
*
* @return bool
*
* @throws ConnectionException If the rollback operation failed.
*/
public function rollBack()
Expand All @@ -1330,6 +1336,8 @@ public function rollBack()
throw ConnectionException::noActiveTransaction();
}

$result = true;

$connection = $this->getWrappedConnection();

$logger = $this->_config->getSQLLogger();
Expand All @@ -1339,7 +1347,9 @@ public function rollBack()
$logger->startQuery('"ROLLBACK"');
}
$this->transactionNestingLevel = 0;
$connection->rollBack();

$result = $connection->rollBack();

$this->isRollbackOnly = false;
if ($logger) {
$logger->stopQuery();
Expand All @@ -1361,6 +1371,8 @@ public function rollBack()
$this->isRollbackOnly = true;
--$this->transactionNestingLevel;
}

return $result;
}

/**
Expand Down
93 changes: 93 additions & 0 deletions tests/Doctrine/Tests/DBAL/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Doctrine\DBAL\VersionAwarePlatformDriver;
use Doctrine\Tests\DbalTestCase;
use Exception;
use PDO;
use PHPUnit\Framework\MockObject\MockObject;
use stdClass;
use function call_user_func_array;
Expand Down Expand Up @@ -297,6 +298,98 @@ public function testCommitStartsTransactionInNoAutoCommitMode() : void
self::assertTrue($conn->isTransactionActive());
}

/**
* @group DBAL-81
*/
public function testCommitReturnTrue() : void
{
$driverMock = $this->createMock(Driver::class);
$driverMock->expects($this->any())
->method('connect')
->will($this->returnValue(
$this->createMock(DriverConnection::class)
));
$pdo = $this->createMock(PDO::class);
$pdo->expects($this->once())
->method('commit')->willReturn(true);

$conn = new Connection(['pdo' => $pdo], $driverMock);

$conn->connect();
$conn->beginTransaction();

self::assertTrue($conn->commit());
}

/**
* @group DBAL-81
*/
public function testCommitReturnFalse() : void
{
$driverMock = $this->createMock(Driver::class);
$driverMock->expects($this->any())
->method('connect')
->will($this->returnValue(
$this->createMock(DriverConnection::class)
));
$pdo = $this->createMock(PDO::class);
$pdo->expects($this->once())
->method('commit')->willReturn(false);

$conn = new Connection(['pdo' => $pdo], $driverMock);

$conn->connect();
$conn->beginTransaction();

self::assertFalse($conn->commit());
}

/**
* @group DBAL-81
*/
public function testRollackReturnTrue() : void
{
$driverMock = $this->createMock(Driver::class);
$driverMock->expects($this->any())
->method('connect')
->will($this->returnValue(
$this->createMock(DriverConnection::class)
));
$pdo = $this->createMock(PDO::class);
$pdo->expects($this->once())
->method('rollback')->willReturn(true);

$conn = new Connection(['pdo' => $pdo], $driverMock);

$conn->connect();
$conn->beginTransaction();

self::assertTrue($conn->rollback());
}

/**
* @group DBAL-81
*/
public function testRollackReturnFalse() : void
{
$driverMock = $this->createMock(Driver::class);
$driverMock->expects($this->any())
->method('connect')
->will($this->returnValue(
$this->createMock(DriverConnection::class)
));
$pdo = $this->createMock(PDO::class);
$pdo->expects($this->once())
->method('rollback')->willReturn(false);

$conn = new Connection(['pdo' => $pdo], $driverMock);

$conn->connect();
$conn->beginTransaction();

self::assertFalse($conn->rollback());
}

/**
* @group DBAL-81
*/
Expand Down
11 changes: 5 additions & 6 deletions tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testTransactionNestingBehavior() : void
}
self::assertTrue($this->connection->isRollbackOnly());

$this->connection->commit(); // should throw exception
self::assertTrue($this->connection->commit()); // should throw exception
$this->fail('Transaction commit after failed nested transaction should fail.');
} catch (ConnectionException $e) {
self::assertEquals(1, $this->connection->getTransactionNestingLevel());
Expand All @@ -88,12 +88,12 @@ public function testTransactionNestingBehaviorWithSavepoints() : void
self::assertEquals(2, $this->connection->getTransactionNestingLevel());
$this->connection->beginTransaction();
self::assertEquals(3, $this->connection->getTransactionNestingLevel());
$this->connection->commit();
self::assertTrue($this->connection->commit());
self::assertEquals(2, $this->connection->getTransactionNestingLevel());
throw new Exception();
$this->connection->commit(); // never reached
} catch (Throwable $e) {
$this->connection->rollBack();
self::assertTrue($this->connection->rollBack());
self::assertEquals(1, $this->connection->getTransactionNestingLevel());
//no rethrow
}
Expand All @@ -107,7 +107,7 @@ public function testTransactionNestingBehaviorWithSavepoints() : void
$this->connection->commit(); // should not throw exception
} catch (ConnectionException $e) {
$this->fail('Transaction commit after failed nested transaction should not fail when using savepoints.');
$this->connection->rollBack();
self::assertTrue($this->connection->rollBack());
}
}

Expand Down Expand Up @@ -191,9 +191,8 @@ public function testTransactionBehaviour() : void
try {
$this->connection->beginTransaction();
self::assertEquals(1, $this->connection->getTransactionNestingLevel());
$this->connection->commit();
self::assertTrue($this->connection->commit());
} catch (Throwable $e) {
$this->connection->rollBack();
self::assertEquals(0, $this->connection->getTransactionNestingLevel());
}

Expand Down

0 comments on commit 9134cd4

Please sign in to comment.