Codeception/module-db

[3.x] Check if `Db::$dbh` is not null before calling `inTransaction()`

simonhammes opened this issue · 2 comments

Error message:

Error: Call to a member function inTransaction() on null in <PROJECT_DIR>/vendor/codeception/module-db/src/Codeception/Lib/Driver/Db.php on line 94

The error did not occur when using Codeception 4.
Maybe it has to do with a different order of PHP internally calling __destruct() methods on objects.

I should note that the errors occur when running wp-browsers own test suite against Codeception 5 (Alpha). wp-browser extends the native Db class, but does never call __destruct() directly or set $dbh to null.

The following patch fixes that behavior:

diff --git a/src/Codeception/Lib/Driver/Db.php b/src/Codeception/Lib/Driver/Db.php
index b69017e..c83203c 100755
--- a/src/Codeception/Lib/Driver/Db.php
+++ b/src/Codeception/Lib/Driver/Db.php
@@ -91,7 +91,7 @@ class Db
 
     public function __destruct()
     {
-        if ($this->dbh->inTransaction()) {
+        if ($this->dbh && $this->dbh->inTransaction()) {
             $this->dbh->rollBack();
         }

Might be related: Codeception/Codeception#5072

Thank you!

Released.