Codeception / module-db

DB module for Codeception
MIT License
23 stars 24 forks source link

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

Closed simonhammes closed 2 years ago

simonhammes commented 2 years ago

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: https://github.com/Codeception/Codeception/pull/5072

simonhammes commented 2 years ago

Thank you!

Naktibalda commented 2 years ago

Released.