Skip to content

Commit

Permalink
Fix formatting and simplify API
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Feb 3, 2021
1 parent 5d7c584 commit 3944d56
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 76 deletions.
21 changes: 21 additions & 0 deletions src/FakePdoTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,25 @@ public function rollback()
Server::restoreSnapshot('transaction');
return true;
}

/**
* @param string $statement
* @return int|false
*/
public function exec($statement)
{
$statement = trim($statement);

if (strpos($statement, 'SET ')===0) {
return false;
}

$sth = $this->prepare($statement);

if ($sth->execute()) {
return $sth->rowCount();
}

return false;
}
}
56 changes: 19 additions & 37 deletions src/Php7/FakePdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,27 @@ class FakePdo extends PDO implements FakePdoInterface
{
use FakePdoTrait;

/**
* @param string $statement
* @param array $options
* @return FakePdoStatement
*/
public function prepare($statement, array $options = [])
/**
* @param string $statement
* @param array $options
* @return FakePdoStatement
*/
public function prepare($statement, $options = [])
{
return new FakePdoStatement($this, $statement, $this->real);
}

/**
* @param string $statement
* @return int|false
*/
public function exec($statement)
{
$statement = trim($statement);
if (strpos($statement, 'SET ')===0){
return false;
}

$sth = $this->prepare($statement);
if ($sth->execute()){
return $sth->rowCount();
}
return false;
}

/**
* @param string $statement
* @param int $mode
* @param null $arg3
* @param array $ctorargs
* @return FakePdoStatement
*/
public function query($statement, $mode = PDO::ATTR_DEFAULT_FETCH_MODE, $arg3 = null, array $ctorargs = [])
{
$sth = $this->prepare($statement);
$sth->execute();
return $sth;
}
/**
* @param string $statement
* @param int $mode
* @param null $arg3
* @param array $ctorargs
* @return FakePdoStatement
*/
public function query($statement, $mode = PDO::ATTR_DEFAULT_FETCH_MODE, $arg3 = null, array $ctorargs = [])
{
$sth = $this->prepare($statement);
$sth->execute();
return $sth;
}
}
54 changes: 18 additions & 36 deletions src/Php8/FakePdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,30 @@
use Vimeo\MysqlEngine\FakePdoInterface;
use Vimeo\MysqlEngine\FakePdoTrait;

class FakePdo extends \PDO implements FakePdoInterface
class FakePdo extends PDO implements FakePdoInterface
{
use FakePdoTrait;

/**
* @param string $statement
* @param array $options
* @return FakePdoStatement
*/
/**
* @param string $statement
* @param array $options
* @return FakePdoStatement
*/
public function prepare($statement, array $options = [])
{
return new FakePdoStatement($this, $statement, $this->real);
}

/**
* @param string $statement
* @return int|false
*/
public function exec($statement)
{
$statement = trim($statement);
if (str_starts_with($statement, 'SET ')){
return false;
}

$sth = $this->prepare($statement);
if ($sth->execute()){
return $sth->rowCount();
}
return false;
}

/**
* @param string $statement
* @param int|null $mode
* @param mixed ...$fetchModeArgs
* @return FakePdoStatement
*/
public function query(string $statement, ?int $mode = PDO::ATTR_DEFAULT_FETCH_MODE, mixed ...$fetchModeArgs)
{
$sth = $this->prepare($statement);
$sth->execute();
return $sth;
}
/**
* @param string $statement
* @param int|null $mode
* @param mixed ...$fetchModeArgs
* @return FakePdoStatement
*/
public function query(string $statement, ?int $mode = PDO::ATTR_DEFAULT_FETCH_MODE, mixed ...$fetchModeArgs)
{
$sth = $this->prepare($statement);
$sth->execute();
return $sth;
}
}
15 changes: 12 additions & 3 deletions tests/SelectProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testCast()

$this->assertInstanceOf(SelectQuery::class, $select_query);

$conn = new \Vimeo\MysqlEngine\Php8\FakePdo('mysql:foo');
$conn = self::getPdo('mysql:foo');

$this->assertSame(
[['a' => 3]],
Expand All @@ -37,7 +37,7 @@ public function testSubqueryCalculation()

$this->assertInstanceOf(SelectQuery::class, $select_query);

$conn = new \Vimeo\MysqlEngine\Php8\FakePdo('mysql:foo');
$conn = self::getPdo('mysql:foo');

$this->assertSame(
[['a' => 5]],
Expand All @@ -58,7 +58,7 @@ public function testStringDecimalIntComparison()

$this->assertInstanceOf(SelectQuery::class, $select_query);

$conn = new \Vimeo\MysqlEngine\Php8\FakePdo('mysql:foo');
$conn = self::getPdo('mysql:foo');

$this->assertSame(
[['a' => 0]],
Expand All @@ -70,4 +70,13 @@ public function testStringDecimalIntComparison()
)->rows
);
}

private static function getPdo(string $connection_string) : \PDO
{
if (\PHP_MAJOR_VERSION === 8) {
return new \Vimeo\MysqlEngine\Php8\FakePdo($connection_string);
}

return new \Vimeo\MysqlEngine\Php7\FakePdo($connection_string);
}
}

0 comments on commit 3944d56

Please sign in to comment.