diff --git a/src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php b/src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php new file mode 100644 index 0000000..be29304 --- /dev/null +++ b/src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php @@ -0,0 +1,61 @@ +. + * + * @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) { + $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(); + } +} + +?> diff --git a/tests/unit/DatabaseAccessorTest.php b/tests/unit/DatabaseAccessorTest.php new file mode 100644 index 0000000..889f0f5 --- /dev/null +++ b/tests/unit/DatabaseAccessorTest.php @@ -0,0 +1,34 @@ +null, 'fetch'=>null]); + $pdoMock = test::spec('MyPDO', ['prepare'=>$pdoStatementMock]); + $query = "SELECT * FROM users"; + + $pdo = $pdoMock->construct(); + + $db = new DatabaseAccessor($pdo); + $db->runQuery($query); + + $pdoMock->verifyInvoked('prepare', [$query]); + $pdoStatementMock->verifyInvoked('execute'); + $pdoStatementMock->verifyInvoked('fetchAll'); + } +} 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 @@