Skip to content

Commit

Permalink
fix: use default fetch mode for PHP8 PDO implementation (#45)
Browse files Browse the repository at this point in the history
This is a copy of the changes from PR#15 (0e864b2), making the
two versions of `FakePdo` behave the same WRT respecting
`->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, *)`.

Made sure to add an appropriate test.
  • Loading branch information
Kenneth-Sills authored Jun 27, 2024
1 parent d781588 commit 09be725
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Php8/FakePdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ class FakePdo extends PDO implements FakePdoInterface
#[\ReturnTypeWillChange]
public function prepare($statement, array $options = [])
{
return new FakePdoStatement($this, $statement, $this->real);
$statement = new FakePdoStatement($this, $statement, $this->real);

if ($this->defaultFetchMode) {
$statement->setFetchMode($this->defaultFetchMode);
}

return $statement;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ public function testInvalidQuery()
$this->assertSame([], $query->fetchAll(\PDO::FETCH_ASSOC));
}

public function testSelectWithDefaultFetchAssoc()
{
$pdo = self::getConnectionToFullDB();
$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);

$query = $pdo->prepare("SELECT id FROM `video_game_characters` WHERE `id` > :id ORDER BY `id` ASC");
$query->bindValue(':id', 14);
$query->execute();

$this->assertSame(
[
['id' => '15'],
['id' => '16']
],
$query->fetchAll()
);
}

public function testSelectFetchDefault()
{
$pdo = self::getConnectionToFullDB();
Expand Down

0 comments on commit 09be725

Please sign in to comment.