diff --git a/src/Codeception/Lib/Driver/Db.php b/src/Codeception/Lib/Driver/Db.php index e8041ecf..3a77c9fe 100755 --- a/src/Codeception/Lib/Driver/Db.php +++ b/src/Codeception/Lib/Driver/Db.php @@ -91,8 +91,8 @@ public function __construct(string $dsn, ?string $user = null, ?string $password public function __destruct() { - if ($this->dbh !== null && $this->dbh->inTransaction()) { - $this->dbh->rollBack(); + if ($this->getDbh() !== null && $this->getDbh()->inTransaction()) { + $this->getDbh()->rollBack(); } $this->dbh = null; @@ -271,7 +271,7 @@ protected function sqlLine(string $sql): bool protected function sqlQuery(string $query): void { try { - $this->dbh->exec($query); + $this->getDbh()->exec($query); } catch (PDOException $exception) { throw new ModuleException( \Codeception\Module\Db::class, @@ -282,7 +282,7 @@ protected function sqlQuery(string $query): void public function executeQuery($query, array $params): PDOStatement { - $pdoStatement = $this->dbh->prepare($query); + $pdoStatement = $this->getDbh()->prepare($query); if (!$pdoStatement) { throw new Exception("Query '{$query}' can't be prepared."); } diff --git a/src/Codeception/Module/Db.php b/src/Codeception/Module/Db.php index 17aa3f28..7f93b355 100644 --- a/src/Codeception/Module/Db.php +++ b/src/Codeception/Module/Db.php @@ -56,6 +56,7 @@ * populate: true # whether the dump should be loaded before the test suite is started * cleanup: true # whether the dump should be reloaded before each test * reconnect: true # whether the module should reconnect to the database before each test + * connection_callback: false # use connection from some other module or create connection on your own way; Example: ['MyDb', 'getConnection'] * waitlock: 10 # wait lock (in seconds) that the database session should use for DDL statements * databases: # include more database configs and switch between them in tests. * skip_cleanup_if_failed: true # Do not perform the cleanup if the tests failed. If this is used, manual cleanup might be required when re-running @@ -248,6 +249,7 @@ class Db extends Module implements DbInterface 'waitlock' => 0, 'dump' => null, 'populator' => null, + 'connection_callback' => false, 'skip_cleanup_if_failed' => false, ]; @@ -592,8 +594,13 @@ private function connect($databaseKey, $databaseConfig): void } try { - $this->debugSection('Connecting To Db', ['config' => $databaseConfig, 'options' => $options]); - $this->drivers[$databaseKey] = Driver::create($databaseConfig['dsn'], $databaseConfig['user'], $databaseConfig['password'], $options); + if (is_callable($databaseConfig['connection_callback'])) { + $this->debugSection('Using connection callback', []); + $this->drivers[$databaseKey] = call_user_func_array($databaseConfig['connection_callback'], [$databaseKey, $databaseConfig, $this]); + } else { + $this->debugSection('Connecting To Db', ['config' => $databaseConfig, 'options' => $options]); + $this->drivers[$databaseKey] = Driver::create($databaseConfig['dsn'], $databaseConfig['user'], $databaseConfig['password'], $options); + } } catch (PDOException $exception) { $message = $exception->getMessage(); if ($message === 'could not find driver') {