Skip to content

Commit 09be725

Browse files
fix: use default fetch mode for PHP8 PDO implementation (#45)
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.
1 parent d781588 commit 09be725

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Php8/FakePdo.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ class FakePdo extends PDO implements FakePdoInterface
1717
#[\ReturnTypeWillChange]
1818
public function prepare($statement, array $options = [])
1919
{
20-
return new FakePdoStatement($this, $statement, $this->real);
20+
$statement = new FakePdoStatement($this, $statement, $this->real);
21+
22+
if ($this->defaultFetchMode) {
23+
$statement->setFetchMode($this->defaultFetchMode);
24+
}
25+
26+
return $statement;
2127
}
2228

2329
/**

tests/EndToEndTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ public function testInvalidQuery()
3434
$this->assertSame([], $query->fetchAll(\PDO::FETCH_ASSOC));
3535
}
3636

37+
public function testSelectWithDefaultFetchAssoc()
38+
{
39+
$pdo = self::getConnectionToFullDB();
40+
$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
41+
42+
$query = $pdo->prepare("SELECT id FROM `video_game_characters` WHERE `id` > :id ORDER BY `id` ASC");
43+
$query->bindValue(':id', 14);
44+
$query->execute();
45+
46+
$this->assertSame(
47+
[
48+
['id' => '15'],
49+
['id' => '16']
50+
],
51+
$query->fetchAll()
52+
);
53+
}
54+
3755
public function testSelectFetchDefault()
3856
{
3957
$pdo = self::getConnectionToFullDB();

0 commit comments

Comments
 (0)