-
Notifications
You must be signed in to change notification settings - Fork 30
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
M1ke
wants to merge
10
commits into
Codeception:master
Choose a base branch
from
M1ke:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1eec70d
Add fake pdo option to driver
M1ke a47ec7c
ignore composer.phar in gitignore
M1ke d25537d
Ensure correct sub-driver is used when fake is chosen
M1ke d4e77bd
Spaces instead of tabs
M1ke de7413c
Spaces instead of tabs (again)
M1ke 322f5ab
Rebuild concept around pdo injection to allow class fqn to be provided
M1ke 762375a
Add asserts to ensure we get an instance of PDO
M1ke 36bea69
Changed config 'pdo' to 'pdo_class' to make it clearer that it is a c…
M1ke 36d39c6
Ensure phpunit tests pass by declaring optional config value, and als…
M1ke 4f15f4d
Another attempt to fix config issues, passes locally
M1ke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/.idea/ | ||
/vendor/ | ||
/composer.lock | ||
composer.phar | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,12 @@ | |
namespace Codeception\Lib\Driver; | ||
|
||
use Codeception\Exception\ModuleException; | ||
use Vimeo\MysqlEngine\FakePdo; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
*/ | ||
|
@@ -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; | ||
} | ||
|
@@ -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': | ||
|
@@ -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; | ||
|
@@ -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]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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