mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 21:22:40 +00:00
Tests for Db\ResultSQLite3
This commit is contained in:
parent
34c69fbf92
commit
176247894d
3 changed files with 105 additions and 19 deletions
|
@ -9,7 +9,29 @@ class ResultSQLite3 implements Result {
|
||||||
protected $cur = null;
|
protected $cur = null;
|
||||||
protected $rows = 0;
|
protected $rows = 0;
|
||||||
|
|
||||||
public function __construct($result, $changes, $statement = null) {
|
// actual public methods
|
||||||
|
|
||||||
|
public function getSingle() {
|
||||||
|
$this->next();
|
||||||
|
if($this->valid()) {
|
||||||
|
$keys = array_keys($this->cur);
|
||||||
|
return $this->cur[array_shift($keys)];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get() {
|
||||||
|
$this->next();
|
||||||
|
return ($this->valid() ? $this->cur : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function changes() {
|
||||||
|
return $this->rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
// constructor/destructor
|
||||||
|
|
||||||
|
public function __construct($result, $changes = 0, $statement = null) {
|
||||||
$this->st = $statement; //keeps the statement from being destroyed, invalidating the result set
|
$this->st = $statement; //keeps the statement from being destroyed, invalidating the result set
|
||||||
$this->set = $result;
|
$this->set = $result;
|
||||||
$this->rows = $changes;
|
$this->rows = $changes;
|
||||||
|
@ -20,6 +42,8 @@ class ResultSQLite3 implements Result {
|
||||||
unset($this->set);
|
unset($this->set);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PHP iterator methods
|
||||||
|
|
||||||
public function valid() {
|
public function valid() {
|
||||||
$this->cur = $this->set->fetchArray(\SQLITE3_ASSOC);
|
$this->cur = $this->set->fetchArray(\SQLITE3_ASSOC);
|
||||||
return ($this->cur !== false);
|
return ($this->cur !== false);
|
||||||
|
@ -43,22 +67,4 @@ class ResultSQLite3 implements Result {
|
||||||
$this->cur = null;
|
$this->cur = null;
|
||||||
$this->set->reset();
|
$this->set->reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSingle() {
|
|
||||||
$this->next();
|
|
||||||
if($this->valid()) {
|
|
||||||
$keys = array_keys($this->cur);
|
|
||||||
return $this->cur[array_shift($keys)];
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get() {
|
|
||||||
$this->next();
|
|
||||||
return ($this->valid() ? $this->cur : null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function changes() {
|
|
||||||
return $this->rows;
|
|
||||||
}
|
|
||||||
}
|
}
|
76
tests/Db/SQLite3/TestDbResultSQLite3.php
Normal file
76
tests/Db/SQLite3/TestDbResultSQLite3.php
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace JKingWeb\NewsSync;
|
||||||
|
|
||||||
|
|
||||||
|
class TestDbResultSQLite3 extends \PHPUnit\Framework\TestCase {
|
||||||
|
use Test\Tools;
|
||||||
|
|
||||||
|
protected $c;
|
||||||
|
|
||||||
|
function setUp() {
|
||||||
|
$c = new \SQLite3(":memory:");
|
||||||
|
$c->enableExceptions(true);
|
||||||
|
$this->c = $c;
|
||||||
|
}
|
||||||
|
|
||||||
|
function tearDown() {
|
||||||
|
$this->c->close();
|
||||||
|
unset($this->c);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testConstructResult() {
|
||||||
|
$set = $this->c->query("SELECT 1");
|
||||||
|
$this->assertInstanceOf(Db\ResultSQLite3::class, new Db\ResultSQLite3($set));
|
||||||
|
}
|
||||||
|
|
||||||
|
function testGetChangeCount() {
|
||||||
|
$this->c->query("CREATE TABLE test(col)");
|
||||||
|
$set = $this->c->query("INSERT INTO test(col) values(1)");
|
||||||
|
$rows = $this->c->changes();
|
||||||
|
$this->assertEquals($rows, (new Db\ResultSQLite3($set,$rows))->changes());
|
||||||
|
}
|
||||||
|
|
||||||
|
function testIterateOverResults() {
|
||||||
|
$set = $this->c->query("SELECT 1 as col union select 2 as col union select 3 as col");
|
||||||
|
$rows = [];
|
||||||
|
foreach(new Db\ResultSQLite3($set) as $row) {
|
||||||
|
$rows[] = $row['col'];
|
||||||
|
}
|
||||||
|
$this->assertEquals([1,2,3], $rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testIterateOverResultsTwice() {
|
||||||
|
$set = $this->c->query("SELECT 1 as col union select 2 as col union select 3 as col");
|
||||||
|
$rows = [];
|
||||||
|
$test = new Db\ResultSQLite3($set);
|
||||||
|
foreach($test as $row) {
|
||||||
|
$rows[] = $row['col'];
|
||||||
|
}
|
||||||
|
foreach($test as $row) {
|
||||||
|
$rows[] = $row['col'];
|
||||||
|
}
|
||||||
|
$this->assertEquals([1,2,3,1,2,3], $rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testGetSingleValues() {
|
||||||
|
$set = $this->c->query("SELECT 1867 as year union select 1970 as year union select 2112 as year");
|
||||||
|
$test = new Db\ResultSQLite3($set);
|
||||||
|
$this->assertEquals(1867, $test->getSingle());
|
||||||
|
$this->assertEquals(1970, $test->getSingle());
|
||||||
|
$this->assertEquals(2112, $test->getSingle());
|
||||||
|
$this->assertSame(null, $test->getSingle());
|
||||||
|
}
|
||||||
|
|
||||||
|
function testGetRows() {
|
||||||
|
$set = $this->c->query("SELECT '2112' as album, '2112' as track union select 'Clockwork Angels' as album, 'The Wreckers' as track");
|
||||||
|
$rows = [
|
||||||
|
['album' => '2112', 'track' => '2112'],
|
||||||
|
['album' => 'Clockwork Angels', 'track' => 'The Wreckers'],
|
||||||
|
];
|
||||||
|
$test = new Db\ResultSQLite3($set);
|
||||||
|
$this->assertEquals($rows[0], $test->get());
|
||||||
|
$this->assertEquals($rows[1], $test->get());
|
||||||
|
$this->assertSame(null, $test->get());
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,4 +27,8 @@
|
||||||
<file>User/TestAuthorization.php</file>
|
<file>User/TestAuthorization.php</file>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
|
|
||||||
|
<testsuite name="SQLite3 database driver">
|
||||||
|
<file>Db/SQLite3/TestDbResultSQLite3.php</file>
|
||||||
|
</testsuite>
|
||||||
|
|
||||||
</phpunit>
|
</phpunit>
|
Loading…
Reference in a new issue