From 09be72574815913e4c3021e536fdd8285ff43deb Mon Sep 17 00:00:00 2001 From: Kenneth Sills <132029135+Kenneth-Sills@users.noreply.github.com> Date: Thu, 27 Jun 2024 14:15:41 -0400 Subject: [PATCH] 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. --- src/Php8/FakePdo.php | 8 +++++++- tests/EndToEndTest.php | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Php8/FakePdo.php b/src/Php8/FakePdo.php index 5d6c5a77..c56aba93 100644 --- a/src/Php8/FakePdo.php +++ b/src/Php8/FakePdo.php @@ -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; } /** diff --git a/tests/EndToEndTest.php b/tests/EndToEndTest.php index ab64ef53..e2790b56 100644 --- a/tests/EndToEndTest.php +++ b/tests/EndToEndTest.php @@ -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();