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

Add commit result bool #3588

Merged
merged 1 commit into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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
}
}