1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00

Add ResultEmpty class

This allows for the creation of synthetic empty result sets
This commit is contained in:
J. King 2017-11-19 15:49:41 -05:00
parent 2037efce61
commit 5d4ea6edc0
4 changed files with 72 additions and 5 deletions

View file

@ -17,15 +17,19 @@ abstract class AbstractResult implements Result {
$out = array_shift($this->cur); $out = array_shift($this->cur);
$this->next(); $this->next();
return $out; return $out;
} else {
return null;
} }
$this->next();
return null;
} }
public function getRow() { public function getRow() {
$out = ($this->valid() ? $this->cur : null); if ($this->valid()) {
$this->next(); $out = $this->cur;
return $out; $this->next();
return $out;
} else {
return null;
}
} }
public function getAll(): array { public function getAll(): array {

25
lib/Db/ResultEmpty.php Normal file
View file

@ -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;
}
}

View file

@ -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());
}
}

View file

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