Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Database access.
*
* This file is part of CodeceptionDataSelector.
*
* CodeceptionDataSelector is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CodeceptionDataSelector is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CodeceptionDataSelector. If not, see
* <http://www.gnu.org/licenses/>.
*
* @package CodeceptionDataSelector
* @author Scott Weldon <open-source@scott-weldon.com>
* @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();
}
}

?>
34 changes: 34 additions & 0 deletions tests/unit/DatabaseAccessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Alpha1_501st\CodeceptionDataSelector\DatabaseAccessor;
use AspectMock\Test as test;

class DatabaseAccessorTest extends \Codeception\TestCase\Test {
/**
* @var \UnitTester
*/
protected $tester;

protected function _before() {
}

protected function _after() {
test::clean();
}

public function testQuery() {
$pdoStatementMock = test::double('PDOStatement',
['execute'=>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');
}
}
1 change: 1 addition & 0 deletions tests/unit/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
// Here you can initialize variables that will be available to your tests
require_once "src/Alpha1_501st/CodeceptionDataSelector/SqlBuilder.php";
require_once "src/Alpha1_501st/CodeceptionDataSelector/DatabaseAccessor.php";
require_once "src/Alpha1_501st/CodeceptionDataSelector/DatabaseSelector.php";
require_once "src/Alpha1_501st/CodeceptionDataSelector/DataFactory.php";

Expand Down