From d6a1237f6d16cb5b8cd57c7aca7096cd84925cce Mon Sep 17 00:00:00 2001 From: Andrej Hudec Date: Tue, 4 Jun 2019 12:46:23 +0200 Subject: [PATCH] Return result from commit() --- lib/Doctrine/DBAL/Connection.php | 8 ++-- tests/Doctrine/Tests/DBAL/ConnectionTest.php | 30 ++++++++++++++ .../Tests/DBAL/Functional/ConnectionTest.php | 2 +- .../Tests/DBAL/Functional/TransactionTest.php | 39 +++++++++++++++++++ 4 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 tests/Doctrine/Tests/DBAL/Functional/TransactionTest.php diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index d514557ff79..7fef4e8628a 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -1266,6 +1266,8 @@ public function commit() throw ConnectionException::commitFailedRollbackOnly(); } + $result = true; + $connection = $this->getWrappedConnection(); $logger = $this->_config->getSQLLogger(); @@ -1275,7 +1277,7 @@ public function commit() $logger->startQuery('"COMMIT"'); } - $connection->commit(); + $result = $connection->commit(); if ($logger) { $logger->stopQuery(); @@ -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; } /** diff --git a/tests/Doctrine/Tests/DBAL/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/ConnectionTest.php index dfd89f80dd6..09ee88349ec 100644 --- a/tests/Doctrine/Tests/DBAL/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/ConnectionTest.php @@ -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 */ diff --git a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php index 2c88a127e33..9218d35e0ab 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php @@ -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 diff --git a/tests/Doctrine/Tests/DBAL/Functional/TransactionTest.php b/tests/Doctrine/Tests/DBAL/Functional/TransactionTest.php new file mode 100644 index 00000000000..4d57874671d --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Functional/TransactionTest.php @@ -0,0 +1,39 @@ +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 + } +}