Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ActuallyConnor committed Sep 26, 2024
1 parent c558b2e commit bdfdc7c
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 48 deletions.
14 changes: 9 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
{
"name": "actuallyconnor/pseudo",
"description": "PDO/MySQL Connection Mocking",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Connor Smyth",
"email": "[email protected]"
"email": "[email protected]",
"homepage": "https://connorsmyth.com",
"role": "Developer"
},
{
"name": "Josh Butts",
"email": "[email protected]"
"email": "[email protected]",
"role": "Original Developer"
}
],
"require": {
"php": ">=8.0",
"ext-pdo": "*",
"ext-ctype": "*"
"php": ">=8.0"
},
"require-dev": {
"ext-pdo": "*",
"ext-ctype": "*",
"phpunit/phpunit": "^11.3"
},
"autoload": {
Expand Down
3 changes: 0 additions & 3 deletions src/Pseudo/ParsedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

class ParsedQuery
{
private mixed $parsedQuery;
private string $rawQuery;
private string $hash;

Expand All @@ -15,10 +14,8 @@ class ParsedQuery
public function __construct(string $query)
{
$parser = new PHPSQLParser();
$this->parsedQuery = $parser->parse($query);
$this->rawQuery = $query;
$this->hash = sha1($query);
$test = 0;
}

public function isEqualTo($query)
Expand Down
54 changes: 26 additions & 28 deletions src/Pseudo/Pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public function __construct(
$this->queryLog = new QueryLog();
}

/**
* @throws Exception
*/
public function prepare($query, $options = null): PdoStatement
{
$result = $this->mockedQueries->getResult($query);
Expand All @@ -33,7 +36,6 @@ public function beginTransaction(): bool
return true;
}
return false;
// not yet implemented
}

public function commit(): bool
Expand All @@ -43,7 +45,6 @@ public function commit(): bool
return true;
}
return false;
// not yet implemented
}

public function rollBack(): bool
Expand All @@ -53,8 +54,8 @@ public function rollBack(): bool

return true;
}
// not yet implemented
return true;

return false;
}

public function inTransaction(): bool
Expand All @@ -64,40 +65,42 @@ public function inTransaction(): bool

public function setAttribute($attribute, $value): bool
{
// not yet implemented
throw new \RuntimeException('Not yet implemented');
}

public function exec($statement): false|int
{
$result = $this->query($statement);
if ($result) {
return $result->rowCount();
}
return 0;

return $result->rowCount();
}

/**
* @param string $query
* @param int|null $fetchMode
* @param mixed ...$fetchModeArgs
* @return PdoStatement
* @throws Exception
*/
public function query(string $query, ?int $fetchMode = null, mixed ...$fetchModeArgs): PdoStatement
{
if ($this->mockedQueries->exists($query)) {
$result = $this->mockedQueries->getResult($query);
if ($result) {
$this->queryLog->addQuery($query);
$statement = new PdoStatement();
$statement->setResult($result);
return $statement;
}

$this->queryLog->addQuery($query);
$statement = new PdoStatement();
$statement->setResult($result);

return $statement;
}

throw new Exception('Unable to convert query to PdoStatement');
}

/**
* @param null $name
* @return false|string
* @throws Exception
*/
public function lastInsertId($name = null): false|string
{
Expand All @@ -122,35 +125,33 @@ private function getLastResult(): Result|false
return false;
}

$result = $this->mockedQueries->getResult($lastQuery);

return $result;
return $this->mockedQueries->getResult($lastQuery);
}

public function errorCode(): ?string
{
// not yet implemented
throw new \RuntimeException('Not yet implemented');
}

public function errorInfo(): array
{
// not yet implemented
throw new \RuntimeException('Not yet implemented');
}

public function getAttribute($attribute)
{
// not yet implemented
throw new \RuntimeException('Not yet implemented');
}

public function quote($string, $type = PDO::PARAM_STR): false|string
{
// not yet implemented
throw new \RuntimeException('Not yet implemented');
}

/**
* @param string $filePath
*/
public function save($filePath): void
public function save(string $filePath): void
{
file_put_contents($filePath, serialize($this->mockedQueries));
}
Expand All @@ -164,16 +165,13 @@ public function load($filePath): void
}

/**
* @param $sql
* @param string $sql
* @param null $expectedResults
* @param null $params
*/
public function mock($sql, $expectedResults = null, $params = null): void
public function mock(string $sql, $expectedResults = null, $params = null): void
{
$test = $this->mockedQueries->count();
$this->mockedQueries->addQuery($sql, $params, $expectedResults);
$test = $this->mockedQueries->count();
$hell0 = 1;
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Pseudo/PdoStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function setFetchMode($mode, $className = null, ...$params): bool|int
$constants = $r->getConstants();
$constantNames = array_keys($constants);
$allowedConstantNames = array_filter($constantNames, function ($val) {
return strpos($val, 'FETCH_') === 0;
return str_starts_with($val, 'FETCH_');
});
$allowedConstantVals = [];
foreach ($allowedConstantNames as $name) {
Expand All @@ -239,35 +239,35 @@ public function setFetchMode($mode, $className = null, ...$params): bool|int

public function nextRowset(): bool
{
// not implemented
throw new \RuntimeException('Not yet implemented');
}

public function closeCursor(): bool
{
// not implemented
throw new \RuntimeException('Not yet implemented');
}

public function debugDumpParams(): ?bool
{
// not implemented
throw new \RuntimeException('Not yet implemented');
}


// some functions make no sense when not actually talking to a database, so they are not implemented

public function setAttribute($attribute, $value): bool
{
// not implemented
throw new \RuntimeException('Not yet implemented');
}

public function getAttribute($name): mixed
{
// not implemented
throw new \RuntimeException('Not yet implemented');
}

public function getColumnMeta($column): false|array
{
// not implemented
throw new \RuntimeException('Not yet implemented');
}

public function getBoundParams(): array
Expand Down
11 changes: 8 additions & 3 deletions src/Pseudo/QueryLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

namespace Pseudo;

use ArrayAccess;
use ArrayIterator;
use Countable;
use InvalidArgumentException;
use IteratorAggregate;
use Traversable;

class QueryLog implements \IteratorAggregate, \ArrayAccess, \Countable
class QueryLog implements IteratorAggregate, ArrayAccess, Countable
{
private $queries = [];

Expand All @@ -13,9 +18,9 @@ public function count(): int
return count($this->queries);
}

public function getIterator(): \Traversable
public function getIterator(): Traversable
{
return new \ArrayIterator($this->queries);
return new ArrayIterator($this->queries);
}

public function offsetExists($offset): bool
Expand Down
3 changes: 3 additions & 0 deletions src/Pseudo/ResultCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public function exists($sql): bool
return isset($this->queries[$query->getHash()]);
}

/**
* @throws Exception
*/
public function getResult(string|ParsedQuery $query): Result
{
if (!($query instanceof ParsedQuery)) {
Expand Down
1 change: 1 addition & 0 deletions src/php-sql-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2681,3 +2681,4 @@ public function getArgument()

define('HAVE_PHP_SQL_PARSER', 1);
}

4 changes: 2 additions & 2 deletions tests/Unit/PdoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
namespace Pseudo\UnitTest;

use PHPUnit\Framework\TestCase;
use Pseudo\Exception;
use Pseudo\Pdo;
use Pseudo\Exception;
use Pseudo\Result;
use Pseudo\ResultCollection;

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

public function testDebuggingRawQueries()
{
$message = null;
Expand Down

0 comments on commit bdfdc7c

Please sign in to comment.