Browse Source

Add ResultEmpty class

This allows for the creation of synthetic empty result sets
microsub
J. King 7 years ago
parent
commit
5d4ea6edc0
  1. 14
      lib/Db/AbstractResult.php
  2. 25
      lib/Db/ResultEmpty.php
  3. 37
      tests/Db/TestResultEmpty.php
  4. 1
      tests/phpunit.xml

14
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 {

25
lib/Db/ResultEmpty.php

@ -0,0 +1,25 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\Db;
use JKingWeb\Arsse\Db\Exception;
class ResultEmpty extends AbstractResult {
public function changes() {
return 0;
}
public function lastId() {
return 0;
}
// PHP iterator methods
public function valid() {
return false;
}
}

37
tests/Db/TestResultEmpty.php

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace JKingWeb\Arsse;
/** @covers \JKingWeb\Arsse\Db\ResultEmpty<extended> */
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());
}
}

1
tests/phpunit.xml

@ -56,6 +56,7 @@
<testsuite name="Database drivers">
<file>Db/TestTransaction.php</file>
<file>Db/TestResultAggregate.php</file>
<file>Db/TestResultEmpty.php</file>
<file>Db/SQLite3/TestDbResultSQLite3.php</file>
<file>Db/SQLite3/TestDbStatementSQLite3.php</file>
<file>Db/SQLite3/TestDbDriverCreationSQLite3.php</file>

Loading…
Cancel
Save