From c82d36440567ebb6c6777a1708b459ca6b613ede Mon Sep 17 00:00:00 2001 From: Jordan Hall Date: Mon, 24 Feb 2020 10:14:09 +0000 Subject: [PATCH] Ensure PDO source can gracefully handle reading from a table with no data --- src/Objects/Sources/PDOSource.php | 2 +- tests/Unit/Data/source.sqlite | Bin 3072 -> 5120 bytes tests/Unit/PDOSourceTest.php | 21 +++++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Objects/Sources/PDOSource.php b/src/Objects/Sources/PDOSource.php index 36e38d0..f99cc17 100644 --- a/src/Objects/Sources/PDOSource.php +++ b/src/Objects/Sources/PDOSource.php @@ -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; } diff --git a/tests/Unit/Data/source.sqlite b/tests/Unit/Data/source.sqlite index 11c35f4b04df5994a7317be95daf3e1c70cbf54a..a48580a8bf42b8899a584dea86bb02a20691ec7d 100644 GIT binary patch delta 292 zcmZpWXwaA-Ey%*az`zQ`Fu*iX$5@nwK~E!-mzkS^g~^qHIh9$M$(8BK#=?t?^^H8t z?BdGGjE(UniAg!BsksFul^9GG=O92O4fjN~~c(WkOVy4Z)%x^gW&h-ea diff --git a/tests/Unit/PDOSourceTest.php b/tests/Unit/PDOSourceTest.php index c30f906..c448a78 100644 --- a/tests/Unit/PDOSourceTest.php +++ b/tests/Unit/PDOSourceTest.php @@ -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); + } }