Skip to content

Commit

Permalink
Merge pull request #2390 from fprochazka/php7-throwable
Browse files Browse the repository at this point in the history
Catch Throwable in Connection::transactional()
  • Loading branch information
deeky666 committed May 26, 2016
2 parents affe2f4 + db06fce commit 8aae77e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Doctrine\DBAL\Cache\ArrayStatement;
use Doctrine\DBAL\Cache\CacheException;
use Doctrine\DBAL\Driver\PingableConnection;
use Throwable;

/**
* A wrapper around a Doctrine\DBAL\Driver\Connection that adds features like
Expand Down Expand Up @@ -773,7 +774,7 @@ public function prepare($statement)
{
try {
$stmt = new Statement($statement, $this);
} catch (\Exception $ex) {
} catch (Exception $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
}

Expand Down Expand Up @@ -824,7 +825,7 @@ public function executeQuery($query, array $params = array(), $types = array(),
} else {
$stmt = $this->_conn->query($query);
}
} catch (\Exception $ex) {
} catch (Exception $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types));
}

Expand Down Expand Up @@ -933,7 +934,7 @@ public function query()
$statement = call_user_func_array(array($this->_conn, 'query'), $args);
break;
}
} catch (\Exception $ex) {
} catch (Exception $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $args[0]);
}

Expand Down Expand Up @@ -984,7 +985,7 @@ public function executeUpdate($query, array $params = array(), array $types = ar
} else {
$result = $this->_conn->exec($query);
}
} catch (\Exception $ex) {
} catch (Exception $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types));
}

Expand Down Expand Up @@ -1015,7 +1016,7 @@ public function exec($statement)

try {
$result = $this->_conn->exec($statement);
} catch (\Exception $ex) {
} catch (Exception $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
}

Expand Down Expand Up @@ -1103,6 +1104,9 @@ public function transactional(Closure $func)
} catch (Exception $e) {
$this->rollBack();
throw $e;
} catch (Throwable $e) {
$this->rollBack();
throw $e;
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,30 @@ public function testTransactionalWithException()
$conn->executeQuery($conn->getDatabasePlatform()->getDummySelectSQL());
throw new \RuntimeException("Ooops!");
});
$this->fail('Expected exception');
} catch (\RuntimeException $expected) {
$this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
}
}

public function testTransactionalWithThrowable()
{
if (version_compare(PHP_VERSION, '7.0', '<')) {
$this->markTestSkipped('Only for PHP 7.0 and above.');
}

try {
$this->_conn->transactional(function($conn) {
/* @var $conn \Doctrine\DBAL\Connection */
$conn->executeQuery($conn->getDatabasePlatform()->getDummySelectSQL());
throw new \Error("Ooops!");
});
$this->fail('Expected exception');
} catch (\Error $expected) {
$this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
}
}

public function testTransactional()
{
$this->_conn->transactional(function($conn) {
Expand Down

0 comments on commit 8aae77e

Please sign in to comment.