Skip to content

Commit

Permalink
fix: correctly handle the default ::fetch style for statements
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kenneth-Sills committed Jun 27, 2024
1 parent c438184 commit c346b8f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/FakePdoStatementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public function fetch(
$cursor_orientation = \PDO::FETCH_ORI_NEXT,
$cursor_offset = 0
) {
if ($fetch_style === -123) {
if ($fetch_style === -123 || $fetch_style === \PDO::FETCH_DEFAULT) {
$fetch_style = $this->fetchMode;
}

Expand Down Expand Up @@ -415,7 +415,7 @@ public function fetchColumn($column = 0)
*/
public function universalFetchAll(int $fetch_style = -123, ...$args) : array
{
if ($fetch_style === -123) {
if ($fetch_style === -123 || $fetch_style === \PDO::FETCH_DEFAULT) {
$fetch_style = $this->fetchMode;
$fetch_argument = $this->fetchArgument;
$ctor_args = $this->fetchConstructorArgs;
Expand Down
22 changes: 22 additions & 0 deletions tests/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ public function testInvalidQuery()
$this->assertSame([], $query->fetchAll(\PDO::FETCH_ASSOC));
}

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

$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', 0 => '15'],
$query->fetch(\PDO::FETCH_DEFAULT)
);

$this->assertSame(
[
['id' => '15', 0 => '15'],
['id' => '16', 0 => '16']
],
$query->fetchAll(\PDO::FETCH_DEFAULT)
);
}

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

0 comments on commit c346b8f

Please sign in to comment.