Skip to content
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

Potential improvement: add support for custom PDO implementation #20

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Add asserts to ensure we get an instance of PDO
  • Loading branch information
M1ke committed Feb 4, 2021
commit 762375a0edee1a24e9eadf5ac06b28d321458e9a
16 changes: 16 additions & 0 deletions src/Codeception/Lib/Driver/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static function connect($dsn, $user, $password, $options = null, $pdo_cla
{
$class_name = self::pdoClass($pdo_class);
$dbh = new $class_name($dsn, $user, $password, $options);
self::assertIsPdo($dbh, $pdo_class);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is necessary so we don't get something that accepts the right arguments but doesn't provided expected methods and/or fails expected typehints down the line.

$dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

return $dbh;
Expand Down Expand Up @@ -118,6 +119,7 @@ public function __construct($dsn, $user, $password, $options = null, $pdo_class
{
$class_name = self::pdoClass($pdo_class);
$this->dbh = new $class_name($dsn, $user, $password, $options);
self::assertIsPdo($this->dbh, $pdo_class);
$this->dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

$this->dsn = $dsn;
Expand All @@ -127,6 +129,20 @@ public function __construct($dsn, $user, $password, $options = null, $pdo_class
$this->pdo_class = $pdo_class;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Storing this the same as the rest so it can be passed back in if necessary (e.g. sqllite reconnection)

}

/**
* @param $dbh
* @param string|null $pdo_class
*/
private static function assertIsPdo($dbh, $pdo_class)
{
if (!$dbh instanceof \PDO){
throw new ModuleException(
'Codeception\Module\Db',
"The provided config value 'pdo' ($pdo_class) did not resolve to a class that implements \\PDO"
);
}
}

public function __destruct()
{
if ($this->dbh->inTransaction()) {
Expand Down