From b59bb9116c72fa27ee58a75ac0cabea992053d1b Mon Sep 17 00:00:00 2001 From: Scott Weldon Date: Thu, 10 Dec 2015 13:43:46 -0800 Subject: [PATCH 1/8] Add new class for DB access --- .../DatabaseAccessor.php | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php diff --git a/src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php b/src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php new file mode 100644 index 0000000..55a3acb --- /dev/null +++ b/src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php @@ -0,0 +1,46 @@ +. + * + * @package CodeceptionDataSelector + * @author Scott Weldon + * @copyright 2015 Scott Weldon + */ +namespace Alpha1_501st\CodeceptionDataSelector; + +use PDO; + +/** + * Provides access to database. + */ +class DatabaseAccessor { + /** + * @var PDO $pdo The PDO object for database access. + */ + protected $pdo; + + /** + * Constructor + */ + public function __construct(PDO $pdo) { + $this->pdo = $pdo; + } +} + +?> From bb3fb995828a986d30cb3be6043d4f5b2e8b7ef2 Mon Sep 17 00:00:00 2001 From: Scott Weldon Date: Thu, 10 Dec 2015 16:49:50 -0800 Subject: [PATCH 2/8] Add function to run a query --- .../CodeceptionDataSelector/DatabaseAccessor.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php b/src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php index 55a3acb..7cfd511 100644 --- a/src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php +++ b/src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php @@ -41,6 +41,21 @@ class DatabaseAccessor { public function __construct(PDO $pdo) { $this->pdo = $pdo; } + + /** + * Run the given query and return any results. + * + * @param string $query The query to run. + * + * @return array|bool Returns either an array of the results, or `false` on + * error. + */ + public function runQuery($query) { + $pdoStatement = $this->pdo->prepare($query); + $pdoStatement->execute([]); + + return $pdoStatement->fetchAll(); + } } ?> From fee0e357169d0cad21ed48973abc763e36fe2d9b Mon Sep 17 00:00:00 2001 From: Scott Weldon Date: Thu, 10 Dec 2015 16:49:58 -0800 Subject: [PATCH 3/8] Add test for db accessor (WIP) --- tests/unit/DatabaseAccessorTest.php | 27 +++++++++++++++++++++++++++ tests/unit/_bootstrap.php | 1 + 2 files changed, 28 insertions(+) create mode 100644 tests/unit/DatabaseAccessorTest.php diff --git a/tests/unit/DatabaseAccessorTest.php b/tests/unit/DatabaseAccessorTest.php new file mode 100644 index 0000000..12ed4fd --- /dev/null +++ b/tests/unit/DatabaseAccessorTest.php @@ -0,0 +1,27 @@ +null, 'fetch'=>null]); + $pdoMock = test::double('PDO', ['__construct'=>null, 'prepare'=>$pdoStatementMock]); + + $pdo = new PDO(""); + + $db = new DatabaseAccessor(); + } +} diff --git a/tests/unit/_bootstrap.php b/tests/unit/_bootstrap.php index 4b8ef0b..62a7f52 100644 --- a/tests/unit/_bootstrap.php +++ b/tests/unit/_bootstrap.php @@ -1,6 +1,7 @@ Date: Sat, 12 Dec 2015 19:44:00 -0800 Subject: [PATCH 4/8] Remove type hinting from constructor --- src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php b/src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php index 7cfd511..be29304 100644 --- a/src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php +++ b/src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php @@ -38,7 +38,7 @@ class DatabaseAccessor { /** * Constructor */ - public function __construct(PDO $pdo) { + public function __construct($pdo) { $this->pdo = $pdo; } From a27b1258e8bebef6d5aadd26db6eb69c492b1d29 Mon Sep 17 00:00:00 2001 From: Scott Weldon Date: Sat, 12 Dec 2015 19:44:19 -0800 Subject: [PATCH 5/8] Update test --- tests/unit/DatabaseAccessorTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/unit/DatabaseAccessorTest.php b/tests/unit/DatabaseAccessorTest.php index 12ed4fd..4e24fb3 100644 --- a/tests/unit/DatabaseAccessorTest.php +++ b/tests/unit/DatabaseAccessorTest.php @@ -17,11 +17,13 @@ protected function _after() { } public function testQuery() { - $pdoStatementMock = test::double('PDOStatement', ['execute'=>null, 'fetch'=>null]); - $pdoMock = test::double('PDO', ['__construct'=>null, 'prepare'=>$pdoStatementMock]); + $pdoStatementMock = test::double('PDOStatement', + ['execute'=>null, 'fetch'=>null]); + $pdoMock = test::spec('MyPDO', + ['__construct'=>null, 'prepare'=>$pdoStatementMock]); - $pdo = new PDO(""); + $pdo = $pdoMock->construct(); - $db = new DatabaseAccessor(); + $db = new DatabaseAccessor($pdo); } } From c8270d8a70384171560509bd94d36205f5e36c3b Mon Sep 17 00:00:00 2001 From: Scott Weldon Date: Sat, 12 Dec 2015 20:00:06 -0800 Subject: [PATCH 6/8] Add runQuery to test --- tests/unit/DatabaseAccessorTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit/DatabaseAccessorTest.php b/tests/unit/DatabaseAccessorTest.php index 4e24fb3..ca13bf5 100644 --- a/tests/unit/DatabaseAccessorTest.php +++ b/tests/unit/DatabaseAccessorTest.php @@ -21,9 +21,11 @@ public function testQuery() { ['execute'=>null, 'fetch'=>null]); $pdoMock = test::spec('MyPDO', ['__construct'=>null, 'prepare'=>$pdoStatementMock]); + $query = "SELECT * FROM users"; $pdo = $pdoMock->construct(); $db = new DatabaseAccessor($pdo); + $db->runQuery($query); } } From 18f064273a88844c67414daa415722630774a51e Mon Sep 17 00:00:00 2001 From: Scott Weldon Date: Sat, 12 Dec 2015 20:00:26 -0800 Subject: [PATCH 7/8] Remove unnecessary function def --- tests/unit/DatabaseAccessorTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/unit/DatabaseAccessorTest.php b/tests/unit/DatabaseAccessorTest.php index ca13bf5..7497828 100644 --- a/tests/unit/DatabaseAccessorTest.php +++ b/tests/unit/DatabaseAccessorTest.php @@ -19,8 +19,7 @@ protected function _after() { public function testQuery() { $pdoStatementMock = test::double('PDOStatement', ['execute'=>null, 'fetch'=>null]); - $pdoMock = test::spec('MyPDO', - ['__construct'=>null, 'prepare'=>$pdoStatementMock]); + $pdoMock = test::spec('MyPDO', ['prepare'=>$pdoStatementMock]); $query = "SELECT * FROM users"; $pdo = $pdoMock->construct(); From 69fe1630b66bcfe99d71947cc9a8d58c2a69f9f0 Mon Sep 17 00:00:00 2001 From: Scott Weldon Date: Sat, 12 Dec 2015 20:15:23 -0800 Subject: [PATCH 8/8] Add assertions (currently failing) --- tests/unit/DatabaseAccessorTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/unit/DatabaseAccessorTest.php b/tests/unit/DatabaseAccessorTest.php index 7497828..889f0f5 100644 --- a/tests/unit/DatabaseAccessorTest.php +++ b/tests/unit/DatabaseAccessorTest.php @@ -26,5 +26,9 @@ public function testQuery() { $db = new DatabaseAccessor($pdo); $db->runQuery($query); + + $pdoMock->verifyInvoked('prepare', [$query]); + $pdoStatementMock->verifyInvoked('execute'); + $pdoStatementMock->verifyInvoked('fetchAll'); } }