From fd1c1d954a3b5fcf9121cdbc1c35ff7a06f71c5e Mon Sep 17 00:00:00 2001 From: m1ke Date: Wed, 3 Feb 2021 09:29:05 +0000 Subject: [PATCH 1/5] Add Exec and Query methods to FakePDO, resolve some type issues by adding docblocks --- src/FakePdo.php | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/src/FakePdo.php b/src/FakePdo.php index 9fc9ae56..74f04898 100644 --- a/src/FakePdo.php +++ b/src/FakePdo.php @@ -1,6 +1,8 @@ server; } - /** - * @param string $statement - */ + /** + * @param string $statement + * @return Php7\FakePdoStatement|Php8\FakePdoStatement + */ public function prepare($statement, $options = null) { if (\PHP_MAJOR_VERSION === 8) { @@ -97,20 +100,59 @@ public function lastInsertId($seqname = null) : string return $this->lastInsertId; } + /** + * @return bool + */ public function beginTransaction() { Server::snapshot('transaction'); return true; } + /** + * @return bool + */ public function commit() { return Server::deleteSnapshot('transaction'); } + /** + * @return bool + * @throws Processor\ProcessorException + */ public function rollback() { Server::restoreSnapshot('transaction'); return true; } + + /** + * @param string $statement + * @return bool + */ + public function exec($statement) + { + $statement = trim($statement); + if (strpos($statement, 'SET ')===0){ + return false; + } + + $sth = $this->prepare($statement); + return $sth->execute(); + } + + /** + * @param string $statement + * @param int $mode + * @param null $arg3 + * @param array $ctorargs + * @return Php7\FakePdoStatement|Php8\FakePdoStatement + */ + public function query($statement, $mode = PDO::ATTR_DEFAULT_FETCH_MODE, $arg3 = null, array $ctorargs = []) + { + $sth = $this->prepare($statement); + $sth->execute(); + return $sth; + } } From 8e93cb651b2782b7e98701e1343635106966fdd3 Mon Sep 17 00:00:00 2001 From: m1ke Date: Wed, 3 Feb 2021 11:17:40 +0000 Subject: [PATCH 2/5] Update exec to return correct values --- src/FakePdo.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/FakePdo.php b/src/FakePdo.php index 74f04898..0566e2bc 100644 --- a/src/FakePdo.php +++ b/src/FakePdo.php @@ -129,7 +129,7 @@ public function rollback() /** * @param string $statement - * @return bool + * @return int|false */ public function exec($statement) { @@ -139,7 +139,10 @@ public function exec($statement) } $sth = $this->prepare($statement); - return $sth->execute(); + if ($sth->execute()){ + return $sth->rowCount(); + } + return false; } /** From 45c2f1fcb099716a5578674168ecac6a80fe2564 Mon Sep 17 00:00:00 2001 From: m1ke Date: Wed, 3 Feb 2021 17:54:56 +0000 Subject: [PATCH 3/5] Implement new query and exec methods in separated php7/8 classes, and fix type on options param in prepare() --- src/Php7/FakePdo.php | 42 +++++++++++++++++++++++++++++++++++++++--- src/Php8/FakePdo.php | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/src/Php7/FakePdo.php b/src/Php7/FakePdo.php index a9e2b15c..9f646e9a 100644 --- a/src/Php7/FakePdo.php +++ b/src/Php7/FakePdo.php @@ -1,16 +1,52 @@ real); } + + /** + * @param string $statement + * @return int|false + */ + public function exec($statement) + { + $statement = trim($statement); + if (strpos($statement, 'SET ')===0){ + return false; + } + + $sth = $this->prepare($statement); + if ($sth->execute()){ + return $sth->rowCount(); + } + return false; + } + + /** + * @param string $statement + * @param int $mode + * @param null $arg3 + * @param array $ctorargs + * @return FakePdoStatement + */ + public function query($statement, $mode = PDO::ATTR_DEFAULT_FETCH_MODE, $arg3 = null, array $ctorargs = []) + { + $sth = $this->prepare($statement); + $sth->execute(); + return $sth; + } } diff --git a/src/Php8/FakePdo.php b/src/Php8/FakePdo.php index 668c6e8c..2a00dc55 100644 --- a/src/Php8/FakePdo.php +++ b/src/Php8/FakePdo.php @@ -1,16 +1,51 @@ real); } + + /** + * @param string $statement + * @return int|false + */ + public function exec($statement) + { + $statement = trim($statement); + if (str_starts_with($statement, 'SET ')){ + return false; + } + + $sth = $this->prepare($statement); + if ($sth->execute()){ + return $sth->rowCount(); + } + return false; + } + + /** + * @param string $statement + * @param int|null $mode + * @param mixed ...$fetchModeArgs + * @return FakePdoStatement + */ + public function query(string $statement, ?int $mode = PDO::ATTR_DEFAULT_FETCH_MODE, mixed ...$fetchModeArgs) + { + $sth = $this->prepare($statement); + $sth->execute(); + return $sth; + } } From b9cb7e1cd579a96c039f61b9bc8361d14a95f465 Mon Sep 17 00:00:00 2001 From: m1ke Date: Wed, 3 Feb 2021 17:57:10 +0000 Subject: [PATCH 4/5] Resolve psalm errors --- src/Php7/FakePdo.php | 9 +++++---- src/Php8/FakePdo.php | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Php7/FakePdo.php b/src/Php7/FakePdo.php index 9f646e9a..bbd72b09 100644 --- a/src/Php7/FakePdo.php +++ b/src/Php7/FakePdo.php @@ -9,10 +9,11 @@ class FakePdo extends PDO implements FakePdoInterface { use FakePdoTrait; - /** - * @param string $statement - * @param ?array $options - */ + /** + * @param string $statement + * @param array $options + * @return FakePdoStatement + */ public function prepare($statement, array $options = []) { return new FakePdoStatement($this, $statement, $this->real); diff --git a/src/Php8/FakePdo.php b/src/Php8/FakePdo.php index 2a00dc55..a7636886 100644 --- a/src/Php8/FakePdo.php +++ b/src/Php8/FakePdo.php @@ -9,10 +9,11 @@ class FakePdo extends \PDO implements FakePdoInterface { use FakePdoTrait; - /** - * @param string $statement - * @param array $options - */ + /** + * @param string $statement + * @param array $options + * @return FakePdoStatement + */ public function prepare($statement, array $options = []) { return new FakePdoStatement($this, $statement, $this->real); From 0c3db9394fdaaa1a14e9485c1be86a56c5800560 Mon Sep 17 00:00:00 2001 From: m1ke Date: Wed, 3 Feb 2021 17:59:01 +0000 Subject: [PATCH 5/5] Added some new files to gitignore, mainly to make github try building again --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 1363dc79..19001c5d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ www.pid *~ composer.lock .vscode +/.idea +composer.phar +.phpunit.result.cache