Skip to content

Commit

Permalink
Return result from commit()
Browse files Browse the repository at this point in the history
  • Loading branch information
pulzarraider committed Jun 26, 2019
1 parent 0152fbf commit d6a1237
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
8 changes: 5 additions & 3 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,8 @@ public function commit()
throw ConnectionException::commitFailedRollbackOnly();
}

$result = true;

$connection = $this->getWrappedConnection();

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

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

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

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

$this->beginTransaction();

return true;
return $result;
}

/**
Expand Down
30 changes: 30 additions & 0 deletions tests/Doctrine/Tests/DBAL/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,36 @@ public function testCommitStartsTransactionInNoAutoCommitMode() : void
self::assertTrue($conn->isTransactionActive());
}

/**
* @dataProvider resultProvider
*/
public function testCommitReturn(bool $expectedResult) : void
{
$driverConnection = $this->createMock(DriverConnection::class);
$driverConnection->expects($this->once())
->method('commit')->willReturn($expectedResult);

$driverMock = $this->createMock(Driver::class);
$driverMock->expects($this->any())
->method('connect')
->will($this->returnValue($driverConnection));

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

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

self::assertSame($expectedResult, $conn->commit());
}

/**
* @return bool[][]
*/
public function resultProvider() : array
{
return [[true], [false]];
}

/**
* @group DBAL-81
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ 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
Expand Down
39 changes: 39 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/TransactionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Doctrine\Tests\DBAL\Functional;

use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\Tests\DbalFunctionalTestCase;
use function sleep;

class TransactionTest extends DbalFunctionalTestCase
{
protected function setUp() : void
{
parent::setUp();

if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
return;
}

$this->markTestSkipped('Restricted to MySQL.');
}

protected function tearDown() : void
{
$this->resetSharedConn();

parent::tearDown();
}

public function testCommitFalse() : void
{
$this->connection->query('SET SESSION wait_timeout=1');

$this->assertTrue($this->connection->beginTransaction());

sleep(2); // during the sleep mysql will close the connection

$this->assertFalse(@$this->connection->commit()); // we will ignore `MySQL server has gone away` warnings
}
}

0 comments on commit d6a1237

Please sign in to comment.