Skip to content

Potential improvement: add support for custom PDO implementation #20

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 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea/
/vendor/
/composer.lock
composer.phar
Copy link
Author

Choose a reason for hiding this comment

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

I can remove this if needed but seemed sensible to include it

45 changes: 39 additions & 6 deletions src/Codeception/Lib/Driver/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Codeception\Lib\Driver;

use Codeception\Exception\ModuleException;
use Vimeo\MysqlEngine\FakePdo;
Copy link
Author

Choose a reason for hiding this comment

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

As far as I am aware this won't break even if the package isn't installed

Copy link
Author

Choose a reason for hiding this comment

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

This change is no longer present


class Db
{
const FAKE_PDO = 'fake:';

/**
* @var \PDO
*/
Expand Down Expand Up @@ -35,8 +38,14 @@ class Db

public static function connect($dsn, $user, $password, $options = null)
{
$dbh = new \PDO($dsn, $user, $password, $options);
$dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
list($real_dsn, $fake) = self::checkForFakePdo($dsn);
if ($fake){
$dbh = new FakePdo($real_dsn, $user, $password, $options);
}
else {
$dbh = new \PDO($dsn, $user, $password, $options);
$dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}

return $dbh;
}
Expand All @@ -56,7 +65,8 @@ public static function connect($dsn, $user, $password, $options = null)
*/
public static function create($dsn, $user, $password, $options = null)
{
$provider = self::getProvider($dsn);
list($real_dsn) = self::checkForFakePdo($dsn);
$provider = self::getProvider($real_dsn);

switch ($provider) {
case 'sqlite':
Expand Down Expand Up @@ -92,10 +102,17 @@ public static function getProvider($dsn)
*/
public function __construct($dsn, $user, $password, $options = null)
{
$this->dbh = new \PDO($dsn, $user, $password, $options);
$this->dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
list($real_dsn, $fake) = self::checkForFakePdo($dsn);

if ($fake){
$this->dbh = new FakePdo($real_dsn, $user, $password, $options);
}
else {
$this->dbh = new \PDO($dsn, $user, $password, $options);
$this->dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}

$this->dsn = $dsn;
$this->dsn = $real_dsn;
$this->user = $user;
$this->password = $password;
$this->options = $options;
Expand Down Expand Up @@ -353,4 +370,20 @@ public function getOptions()
{
return $this->options;
}

/**
* @param string $dsn
*
* @return array{string, bool}
*/
private static function checkForFakePdo($dsn)
{
if (strpos($dsn, self::FAKE_PDO)===0){
$real_dsn = substr($dsn, strlen(self::FAKE_PDO));

return [$real_dsn, true];
}

return [$dsn, false];
}
}