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
14 changes: 10 additions & 4 deletions src/Pseudo/ParsedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@ class ParsedQuery
private $parsedQuery;
private $rawQuery;
private $hash;
private $params;

/**
* @param string $query
*/
public function __construct($query)
public function __construct($query, $params = null)
{
$parser = new \PHPSQLParser();
$this->parsedQuery = $parser->parse($query);
$this->rawQuery = $query;
$this->hash = sha1(serialize($this->parsedQuery));
$this->params = $params;
$serializedParams = "";
if ($this->params != null) {
$serializedParams = serialize($this->params);
}
$this->hash = sha1(serialize($this->parsedQuery) . $serializedParams);
}

public function isEqualTo($query)
public function isEqualTo($query, $params = null)
{
if (!($query instanceof self)) {
$query = new self($query);
$query = new self($query, $params);
}
return $this->hash === $query->getHash();
}
Expand Down
7 changes: 3 additions & 4 deletions src/Pseudo/Pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class Pdo extends \PDO

public function prepare($statement, $driver_options = null)
{
$result = $this->mockedQueries->getResult($statement);
return new PdoStatement($result, $this->queryLog, $statement);
return new PdoStatement($this->mockedQueries, $this->queryLog, $statement);
}

public function beginTransaction()
Expand Down Expand Up @@ -71,7 +70,7 @@ public function query($statement)
$result = $this->mockedQueries->getResult($statement);
if ($result) {
$this->queryLog->addQuery($statement);
$statement = new PdoStatement();
$statement = new PdoStatement($this->mockedQueries);
$statement->setResult($result);
return $statement;
}
Expand Down Expand Up @@ -124,7 +123,7 @@ public function quote($string, $parameter_type = PDO::PARAM_STR)
/**
* @param ResultCollection $collection
*/
public function __construct(ResultCollection $collection = null)
public function __construct(ResultCollection $collection = null)
{
$this->mockedQueries = $collection ?: new ResultCollection();
$this->queryLog = new QueryLog();
Expand Down
11 changes: 5 additions & 6 deletions src/Pseudo/PdoStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class PdoStatement extends \PDOStatement
/**
* @var Result;
*/
private $mockedQueries;
private $result;
private $fetchMode = \PDO::FETCH_BOTH; //DEFAULT FETCHMODE
private $boundParams = [];
Expand All @@ -27,12 +28,9 @@ class PdoStatement extends \PDOStatement
* @param string $statement
* @param Result|null $result
*/
public function __construct($result = null, QueryLog $queryLog = null, $statement = null)
public function __construct($mockedQueries = null, QueryLog $queryLog = null, $statement = null)
{
if (!($result instanceof Result)) {
$result = new Result();
}
$this->result = $result;
$this->mockedQueries = $mockedQueries;
if (!($queryLog instanceof QueryLog)) {
$queryLog = new QueryLog();
}
Expand All @@ -52,6 +50,7 @@ public function setResult(Result $result)
public function execute($input_parameters = null)
{
$input_parameters = array_merge((array)$input_parameters, $this->boundParams);
$this->result = $this->mockedQueries->getResult($this->statement, $input_parameters);
try {
$this->result->setParams($input_parameters, !empty($this->boundParams));
$success = (bool) $this->result->getRows($input_parameters ?: []);
Expand All @@ -62,7 +61,7 @@ public function execute($input_parameters = null)
}
}

public function fetch($fetch_style = null, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0)
public function fetch($fetch_style = null, $cursor_orientation = \PDO::FETCH_ORI_NEXT, $cursor_offset = 0)
{
// scrolling cursors not implemented
$row = $this->result->nextRow();
Expand Down
4 changes: 2 additions & 2 deletions src/Pseudo/QueryLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function offsetUnset($offset)
unset($this->queries[$offset]);
}

public function addQuery($sql)
public function addQuery($sql, $params = null)
{
$this->queries[] = new ParsedQuery($sql);
$this->queries[] = new ParsedQuery($sql, $params);
}

public function getQueries()
Expand Down
12 changes: 6 additions & 6 deletions src/Pseudo/ResultCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
class ResultCollection implements \Countable
{
private $queries = [];

public function count()
{
return count($this->queries);
}

public function addQuery($sql, $results, $params = null)
{
$query = new ParsedQuery($sql);
$query = new ParsedQuery($sql, $params);

if (is_array($results)) {
$storedResults = new Result($results, $params);
Expand All @@ -25,16 +25,16 @@ public function addQuery($sql, $results, $params = null)
$this->queries[$query->getHash()] = $storedResults;
}

public function exists($sql)
public function exists($sql, $params = null)
{
$query = new ParsedQuery($sql);
$query = new ParsedQuery($sql, $params);
return isset($this->queries[$query->getHash()]);
}

public function getResult($query)
public function getResult($query, $params = null)
{
if (!($query instanceof ParsedQuery)) {
$query = new ParsedQuery($query);
$query = new ParsedQuery($query, $params);
}
$result = (isset($this->queries[$query->getHash()])) ? $this->queries[$query->getHash()] : null;
if ($result instanceof Result) {
Expand Down
11 changes: 11 additions & 0 deletions tests/ParsedQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ public function testQueryHashing()
$this->assertEquals($hashed, $q->getHash());
}

public function testQueryHashingWithParams()
{
$sql = "SELECT foo FROM bar WHERE baz = ?";
$params = ["foo"];
$q = new \Pseudo\ParsedQuery($sql, $params);
$p = new \PHPSQLParser();
$parsed = $p->parse($sql);
$hashed = sha1(serialize($parsed) . serialize($params));
$this->assertEquals($hashed, $q->getHash());
}

public function testIsEquals()
{
$sql = "SELECT foo FROM bar WHERE baz";
Expand Down
76 changes: 65 additions & 11 deletions tests/PdoStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,17 @@ public function testErrorCode()
{
$r = new Pseudo\Result();
$r->setErrorCode("HY000");
$p = new Pseudo\PdoStatement($r);
$p = new Pseudo\PdoStatement();
$p->setResult($r);
$this->assertEquals("HY000", $p->errorCode());
}

public function testErrorInfo()
{
$r = new Pseudo\Result();
$r->setErrorInfo("Storage engine error");
$p = new Pseudo\PdoStatement($r);
$p = new Pseudo\PdoStatement();
$p->setResult($r);
$this->assertEquals("Storage engine error", $p->errorInfo());
}

Expand All @@ -108,7 +110,8 @@ public function testColumnCount()
'foo' => 'bar'
]
);
$p = new Pseudo\PdoStatement($r);
$p = new Pseudo\PdoStatement();
$p->setResult($r);
$this->assertEquals(2, $p->columnCount());
}

Expand All @@ -133,7 +136,8 @@ public function testFetch()
];

$r = new Pseudo\Result();
$p = new Pseudo\PdoStatement($r);
$p = new Pseudo\PdoStatement();
$p->setResult($r);

$data = $p->fetch();
$this->assertEquals(false, $data);
Expand Down Expand Up @@ -161,7 +165,8 @@ public function testFetchCol()
];

$r = new Pseudo\Result();
$p = new Pseudo\PdoStatement($r);
$p = new Pseudo\PdoStatement();
$p->setResult($r);

$r->addRow($row1);
$r->addRow($row2);
Expand All @@ -179,14 +184,16 @@ public function testFetchWithBoundColumns()
];
$r = new Pseudo\Result();
$r->addRow($row1);
$p = new Pseudo\PdoStatement($r);
$p = new Pseudo\PdoStatement();
$p->setResult($r);
$p->bindColumn(2, $test);
$p->fetch(PDO::FETCH_BOUND);
$this->assertEquals('bar', $test);
unset($test);

$r->reset();
$p = new Pseudo\PdoStatement($r);
$p = new Pseudo\PdoStatement();
$p->setResult($r);
$p->bindColumn('foo', $test);
$p->fetch(PDO::FETCH_BOUND);
$this->assertEquals('bar', $test);
Expand All @@ -202,7 +209,8 @@ public function testFetchObject()
$testObject = (object) $row1;
$r = new Pseudo\Result();
$r->addRow($row1);
$s = new Pseudo\PdoStatement($r);
$s = new Pseudo\PdoStatement();
$s->setResult($r);
$this->assertEquals($testObject, $s->fetchObject());
}

Expand All @@ -216,13 +224,58 @@ public function testExecute()
'bar'
];

$p = new Pseudo\Pdo();
$p->mock('SELECT * FROM test where foo = ?', $row1, $params1);
$queries = $p->getMockedQueries();

$r = new Pseudo\Result();
$r->addRow($row1, $params1);
$queryLog = new Pseudo\QueryLog();
$s = new Pseudo\PdoStatement($r, $queryLog, 'SELECT * FROM test');
$s = new Pseudo\PdoStatement($queries, $queryLog, 'SELECT * FROM test where foo = ?');

$this->assertEquals(true, $s->execute($params1));
$this->assertEquals(false, $s->execute());

// this should no longer just return since we're now mocking with params
$caughtException = false;
try {
$this->assertEquals(false, $s->execute());
} catch(\Exception $e) {
$this->assertEquals('Attempting an operation on an un-mocked query is not allowed, the raw query: SELECT * FROM test where foo = ?', $e->getMessage());
$caughtException = true;
}
$this->assertTrue($caughtException);
}

public function testExecuteWithSameQueryDifferentParams()
{
$row1 = [
'id' => 1,
'foo' => 'bar',
];
$params1 = [
'bar'
];

$row2 = [
'id' => 2,
'foo' => 'bar2',
];
$params2 = [
'bar2'
];

$p = new Pseudo\Pdo();
$p->mock('SELECT * FROM test where foo = ?', $row1, $params1);
$p->mock('SELECT * FROM test where foo = ?', $row2, $params2);
$queries = $p->getMockedQueries();

$r = new Pseudo\Result();
$r->addRow($row1, $params1);
$queryLog = new Pseudo\QueryLog();
$s = new Pseudo\PdoStatement($queries, $queryLog, 'SELECT * FROM test where foo = ?');

$this->assertEquals(true, $s->execute($params1));
$this->assertEquals(true, $s->execute($params2));
}

public function testBindParam()
Expand Down Expand Up @@ -252,7 +305,8 @@ public function testFetchColumn()

$r = new Pseudo\Result();
$r->addRow($row1);
$s = new Pseudo\PdoStatement($r);
$s = new Pseudo\PdoStatement();
$s->setResult($r);

$this->assertEquals('bar', $s->fetchColumn(1));
$this->assertEquals(false, $s->fetchColumn(0));
Expand Down
7 changes: 4 additions & 3 deletions tests/PdoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testMock()
$p->mock($sql3, $result1, $params3);
$p->mock($sql3, $result2, $params4);

$this->assertEquals(3, count($p->getMockedQueries()));
$this->assertEquals(4, count($p->getMockedQueries()));


}
Expand Down Expand Up @@ -174,13 +174,14 @@ public function testSave()
$this->assertEquals($r, $queries);
unlink('testsave');
}

public function testDebuggingRawQueries()
{
$message = null;
$p = new Pseudo\Pdo();
try {
$p->prepare('SELECT 123');
$sth = $p->prepare('SELECT 123');
$sth->execute();
} catch (Exception $e) {
$message = $e->getMessage();
}
Expand Down
11 changes: 11 additions & 0 deletions tests/QueryLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,15 @@ public function testAddQuery()
$this->assertEquals(1, count($queries));
$this->assertTrue($queries[0]->isEqualTo($sql));
}

public function testAddQueryWithParams()
{
$sql = "SELECT foo FROM ?";
$params = ["bar"];
$queryLog = new \Pseudo\QueryLog();
$queryLog->addQuery($sql, $params);
$queries = $queryLog->getQueries();
$this->assertEquals(1, count($queries));
$this->assertTrue($queries[0]->isEqualTo($sql, $params));
}
}