From 5d4ea6edc0c6ec60063347772bf01b7461e44cf6 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sun, 19 Nov 2017 15:49:41 -0500 Subject: [PATCH] Add ResultEmpty class This allows for the creation of synthetic empty result sets --- lib/Db/AbstractResult.php | 14 +++++++++----- lib/Db/ResultEmpty.php | 25 ++++++++++++++++++++++++ tests/Db/TestResultEmpty.php | 37 ++++++++++++++++++++++++++++++++++++ tests/phpunit.xml | 1 + 4 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 lib/Db/ResultEmpty.php create mode 100644 tests/Db/TestResultEmpty.php diff --git a/lib/Db/AbstractResult.php b/lib/Db/AbstractResult.php index c1a60c65..fe4df26d 100644 --- a/lib/Db/AbstractResult.php +++ b/lib/Db/AbstractResult.php @@ -17,15 +17,19 @@ abstract class AbstractResult implements Result { $out = array_shift($this->cur); $this->next(); return $out; + } else { + return null; } - $this->next(); - return null; } public function getRow() { - $out = ($this->valid() ? $this->cur : null); - $this->next(); - return $out; + if ($this->valid()) { + $out = $this->cur; + $this->next(); + return $out; + } else { + return null; + } } public function getAll(): array { diff --git a/lib/Db/ResultEmpty.php b/lib/Db/ResultEmpty.php new file mode 100644 index 00000000..f11d75ea --- /dev/null +++ b/lib/Db/ResultEmpty.php @@ -0,0 +1,25 @@ + */ +class TestResultEmpty extends Test\AbstractTest { + + public function testGetChangeCountAndLastInsertId() { + $r = new Db\ResultEmpty; + $this->assertEquals(0, $r->changes()); + $this->assertEquals(0, $r->lastId()); + } + + public function testIterateOverResults() { + $rows = []; + foreach (new Db\ResultEmpty as $index => $row) { + $rows[$index] = $row['col']; + } + $this->assertEquals([], $rows); + } + + public function testGetSingleValues() { + $test = new Db\ResultEmpty; + $this->assertSame(null, $test->getValue()); + } + + public function testGetRows() { + $test = new Db\ResultEmpty; + $this->assertSame(null, $test->getRow()); + } + + public function testGetAllRows() { + $test = new Db\ResultEmpty; + $rows = []; + $this->assertEquals($rows, $test->getAll()); + } +} \ No newline at end of file diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 9d65e7b2..3feab1bc 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -56,6 +56,7 @@ Db/TestTransaction.php Db/TestResultAggregate.php + Db/TestResultEmpty.php Db/SQLite3/TestDbResultSQLite3.php Db/SQLite3/TestDbStatementSQLite3.php Db/SQLite3/TestDbDriverCreationSQLite3.php