2017-03-01 19:00:14 -05:00
|
|
|
<?php
|
2017-11-16 20:23:18 -05:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-03-01 19:00:14 -05:00
|
|
|
declare(strict_types=1);
|
2017-03-27 23:12:12 -05:00
|
|
|
namespace JKingWeb\Arsse;
|
2017-03-01 19:00:14 -05:00
|
|
|
|
2017-07-20 18:36:03 -04:00
|
|
|
/** @covers \JKingWeb\Arsse\Db\SQLite3\Result<extended> */
|
2017-07-07 21:06:38 -04:00
|
|
|
class TestDbResultSQLite3 extends Test\AbstractTest {
|
2017-03-01 19:00:14 -05:00
|
|
|
protected $c;
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function setUp() {
|
|
|
|
if (!extension_loaded("sqlite3")) {
|
2017-05-22 10:02:36 -04:00
|
|
|
$this->markTestSkipped("SQLite extension not loaded");
|
|
|
|
}
|
2017-03-01 19:00:14 -05:00
|
|
|
$c = new \SQLite3(":memory:");
|
|
|
|
$c->enableExceptions(true);
|
|
|
|
$this->c = $c;
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function tearDown() {
|
2017-03-01 19:00:14 -05:00
|
|
|
$this->c->close();
|
|
|
|
unset($this->c);
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function testConstructResult() {
|
2017-03-01 19:00:14 -05:00
|
|
|
$set = $this->c->query("SELECT 1");
|
2017-03-07 18:01:13 -05:00
|
|
|
$this->assertInstanceOf(Db\Result::class, new Db\SQLite3\Result($set));
|
2017-03-01 19:00:14 -05:00
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function testGetChangeCountAndLastInsertId() {
|
2017-03-01 19:00:14 -05:00
|
|
|
$this->c->query("CREATE TABLE test(col)");
|
|
|
|
$set = $this->c->query("INSERT INTO test(col) values(1)");
|
|
|
|
$rows = $this->c->changes();
|
2017-03-09 14:48:42 -05:00
|
|
|
$id = $this->c->lastInsertRowID();
|
2017-08-29 10:50:31 -04:00
|
|
|
$r = new Db\SQLite3\Result($set, [$rows,$id]);
|
2017-03-09 14:48:42 -05:00
|
|
|
$this->assertEquals($rows, $r->changes());
|
|
|
|
$this->assertEquals($id, $r->lastId());
|
2017-03-01 19:00:14 -05:00
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function testIterateOverResults() {
|
2017-03-01 19:00:14 -05:00
|
|
|
$set = $this->c->query("SELECT 1 as col union select 2 as col union select 3 as col");
|
|
|
|
$rows = [];
|
2017-08-29 10:50:31 -04:00
|
|
|
foreach (new Db\SQLite3\Result($set) as $index => $row) {
|
2017-07-21 11:13:04 -04:00
|
|
|
$rows[$index] = $row['col'];
|
2017-03-01 19:00:14 -05:00
|
|
|
}
|
2017-07-21 11:13:04 -04:00
|
|
|
$this->assertEquals([0 => 1, 1 => 2, 2 => 3], $rows);
|
2017-03-01 19:00:14 -05:00
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function testIterateOverResultsTwice() {
|
2017-03-01 19:00:14 -05:00
|
|
|
$set = $this->c->query("SELECT 1 as col union select 2 as col union select 3 as col");
|
|
|
|
$rows = [];
|
2017-03-07 18:01:13 -05:00
|
|
|
$test = new Db\SQLite3\Result($set);
|
2017-08-29 10:50:31 -04:00
|
|
|
foreach ($test as $row) {
|
2017-03-01 19:00:14 -05:00
|
|
|
$rows[] = $row['col'];
|
|
|
|
}
|
2017-11-05 22:13:44 -05:00
|
|
|
$this->assertEquals([1,2,3], $rows);
|
|
|
|
$this->assertException("resultReused", "Db");
|
2017-08-29 10:50:31 -04:00
|
|
|
foreach ($test as $row) {
|
2017-03-01 19:00:14 -05:00
|
|
|
$rows[] = $row['col'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function testGetSingleValues() {
|
2017-03-01 19:00:14 -05:00
|
|
|
$set = $this->c->query("SELECT 1867 as year union select 1970 as year union select 2112 as year");
|
2017-03-07 18:01:13 -05:00
|
|
|
$test = new Db\SQLite3\Result($set);
|
2017-03-06 16:34:38 -05:00
|
|
|
$this->assertEquals(1867, $test->getValue());
|
|
|
|
$this->assertEquals(1970, $test->getValue());
|
|
|
|
$this->assertEquals(2112, $test->getValue());
|
|
|
|
$this->assertSame(null, $test->getValue());
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function testGetFirstValuesOnly() {
|
2017-03-06 16:34:38 -05:00
|
|
|
$set = $this->c->query("SELECT 1867 as year, 19 as century union select 1970 as year, 20 as century union select 2112 as year, 22 as century");
|
2017-03-07 18:01:13 -05:00
|
|
|
$test = new Db\SQLite3\Result($set);
|
2017-03-06 16:34:38 -05:00
|
|
|
$this->assertEquals(1867, $test->getValue());
|
|
|
|
$this->assertEquals(1970, $test->getValue());
|
|
|
|
$this->assertEquals(2112, $test->getValue());
|
|
|
|
$this->assertSame(null, $test->getValue());
|
2017-03-01 19:00:14 -05:00
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function testGetRows() {
|
2017-03-01 19:00:14 -05:00
|
|
|
$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'],
|
|
|
|
];
|
2017-03-07 18:01:13 -05:00
|
|
|
$test = new Db\SQLite3\Result($set);
|
2017-03-09 15:01:18 -05:00
|
|
|
$this->assertEquals($rows[0], $test->getRow());
|
|
|
|
$this->assertEquals($rows[1], $test->getRow());
|
|
|
|
$this->assertSame(null, $test->getRow());
|
2017-11-05 22:13:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAllRows() {
|
|
|
|
$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\SQLite3\Result($set);
|
2017-03-06 16:34:38 -05:00
|
|
|
$this->assertEquals($rows, $test->getAll());
|
2017-03-01 19:00:14 -05:00
|
|
|
}
|
2017-08-29 10:50:31 -04:00
|
|
|
}
|