diff options
-rw-r--r-- | tests/unit/UnitTestCase.php | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/tests/unit/UnitTestCase.php b/tests/unit/UnitTestCase.php index 4f2233042..afdaf1c9a 100644 --- a/tests/unit/UnitTestCase.php +++ b/tests/unit/UnitTestCase.php @@ -41,6 +41,8 @@ require_once('include/dba/dba_driver.php'); * @author Klaus Weidenbach */ class UnitTestCase extends TestCase { + private bool $in_transaction = false; + public static function setUpBeforeClass() : void { if ( !\DBA::$dba ) { \DBA::dba_factory( @@ -54,7 +56,9 @@ class UnitTestCase extends TestCase { false); if ( !\DBA::$dba->connected ) { - throw new \Exception("Unable to connect to db!"); + throw new \Exception( + "Unable to connect to db! Error is: " + . \DBA::$dba->db->errorInfo()); } } } @@ -63,15 +67,21 @@ class UnitTestCase extends TestCase { if ( \DBA::$dba->connected ) { // Create a transaction, so that any actions taken by the // tests does not change the actual contents of the database. - \DBA::$dba->beginTransaction(); + $this->in_transaction = \DBA::$dba->db->beginTransaction(); } } protected function tearDown() : void { - if ( \DBA::$dba->connected ) { + if ( \DBA::$dba->connected && $this->in_transaction ) { // Roll back the transaction, restoring the db to the // state it was before the test was run. - \DBA::$dba->rollBack(); + if ( \DBA::$dba->db->rollBack() ) { + $this->in_transaction = false; + } else { + throw new \Exception( + "Transaction rollback failed! Error is: " + . \DBA::$dba->db->errorInfo()); + } } } } |