Skip to content

Commit d781588

Browse files
fix: correctly handle the default ::fetch style for statements (#46)
We've overridden the default value to our magic -123, which normally works, but if someone passes in `PDO::FETCH_DEFAULT` manually the method can't recognize it and throw an unimplemented error. This just makes sure the default is interpretted the same as -123, better matching the standard. Added an appropriate test as well.
1 parent c438184 commit d781588

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/FakePdoStatementTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public function fetch(
339339
$cursor_orientation = \PDO::FETCH_ORI_NEXT,
340340
$cursor_offset = 0
341341
) {
342-
if ($fetch_style === -123) {
342+
if ($fetch_style === -123 || $fetch_style === \PDO::FETCH_DEFAULT) {
343343
$fetch_style = $this->fetchMode;
344344
}
345345

@@ -415,7 +415,7 @@ public function fetchColumn($column = 0)
415415
*/
416416
public function universalFetchAll(int $fetch_style = -123, ...$args) : array
417417
{
418-
if ($fetch_style === -123) {
418+
if ($fetch_style === -123 || $fetch_style === \PDO::FETCH_DEFAULT) {
419419
$fetch_style = $this->fetchMode;
420420
$fetch_argument = $this->fetchArgument;
421421
$ctor_args = $this->fetchConstructorArgs;

tests/EndToEndTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ public function testInvalidQuery()
3434
$this->assertSame([], $query->fetchAll(\PDO::FETCH_ASSOC));
3535
}
3636

37+
public function testSelectFetchDefault()
38+
{
39+
$pdo = self::getConnectionToFullDB();
40+
41+
$query = $pdo->prepare("SELECT id FROM `video_game_characters` WHERE `id` > :id ORDER BY `id` ASC");
42+
$query->bindValue(':id', 14);
43+
$query->execute();
44+
45+
$this->assertSame(
46+
['id' => '15', 0 => '15'],
47+
$query->fetch(\PDO::FETCH_DEFAULT)
48+
);
49+
50+
$this->assertSame(
51+
[
52+
['id' => '15', 0 => '15'],
53+
['id' => '16', 0 => '16']
54+
],
55+
$query->fetchAll(\PDO::FETCH_DEFAULT)
56+
);
57+
}
58+
3759
public function testSelectFetchAssoc()
3860
{
3961
$pdo = self::getConnectionToFullDB();

0 commit comments

Comments
 (0)