Skip to content

Commit c82d364

Browse files
author
Jordan Hall
committed
Ensure PDO source can gracefully handle reading from a table with no data
1 parent 3015bb0 commit c82d364

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/Objects/Sources/PDOSource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function getTableFields()
3838

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

41-
$tableFields = array_keys($row);
41+
$tableFields = is_array($row) ? array_keys($row) : [];
4242

4343
return $tableFields;
4444
}

tests/Unit/Data/source.sqlite

2 KB
Binary file not shown.

tests/Unit/PDOSourceTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,25 @@ public function testCountPages()
165165

166166
$this->assertEquals(1, $source->countPages());
167167
}
168+
169+
private function createEmptyTableSource()
170+
{
171+
return new PDOSource(new PDO('sqlite:'.__DIR__.'/Data/source.sqlite'), 'empty_table');
172+
}
173+
174+
public function testGetFieldsOnEmptyTable()
175+
{
176+
$source = $this->createEmptyTableSource();
177+
178+
$this->assertEquals([], $source->getFields());
179+
}
180+
181+
public function testGetDataRowsOnEmptyTable()
182+
{
183+
$source = $this->createEmptyTableSource();
184+
185+
$dataRows = $source->getDataRows(1, ['thing1', 'thing2']);
186+
187+
$this->assertCount(0, $dataRows);
188+
}
168189
}

0 commit comments

Comments
 (0)