Skip to content

feat: allow developer to define own connection #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Codeception/Lib/Driver/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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.");
}
Expand Down
11 changes: 9 additions & 2 deletions src/Codeception/Module/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -248,6 +249,7 @@ class Db extends Module implements DbInterface
'waitlock' => 0,
'dump' => null,
'populator' => null,
'connection_callback' => false,
'skip_cleanup_if_failed' => false,
];

Expand Down Expand Up @@ -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') {
Expand Down