Skip to content

Commit

Permalink
Ensure PDO source can gracefully handle reading from a table with no …
Browse files Browse the repository at this point in the history
…data
  • Loading branch information
Jordan Hall committed Feb 24, 2020
1 parent 3015bb0 commit c82d364
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Objects/Sources/PDOSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function getTableFields()

$row = $stmt->fetch(PDO::FETCH_ASSOC);

$tableFields = array_keys($row);
$tableFields = is_array($row) ? array_keys($row) : [];

return $tableFields;
}
Expand Down
Binary file modified tests/Unit/Data/source.sqlite
Binary file not shown.
21 changes: 21 additions & 0 deletions tests/Unit/PDOSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,25 @@ public function testCountPages()

$this->assertEquals(1, $source->countPages());
}

private function createEmptyTableSource()
{
return new PDOSource(new PDO('sqlite:'.__DIR__.'/Data/source.sqlite'), 'empty_table');
}

public function testGetFieldsOnEmptyTable()
{
$source = $this->createEmptyTableSource();

$this->assertEquals([], $source->getFields());
}

public function testGetDataRowsOnEmptyTable()
{
$source = $this->createEmptyTableSource();

$dataRows = $source->getDataRows(1, ['thing1', 'thing2']);

$this->assertCount(0, $dataRows);
}
}

0 comments on commit c82d364

Please sign in to comment.