2017-12-19 22:15:05 +00:00
|
|
|
<?php
|
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2017-12-22 16:45:24 +00:00
|
|
|
namespace JKingWeb\Arsse\TestCase\Db\SQLite3PDO;
|
|
|
|
|
|
|
|
use JKingWeb\Arsse\Db\Result;
|
|
|
|
use JKingWeb\Arsse\Db\PDOResult;
|
|
|
|
use JKingWeb\Arsse\Db\SQLite3\PDODriver;
|
2017-12-19 22:15:05 +00:00
|
|
|
|
|
|
|
/** @covers \JKingWeb\Arsse\Db\PDOResult<extended> */
|
2017-12-22 16:45:24 +00:00
|
|
|
class TestResult extends \JKingWeb\Arsse\Test\AbstractTest {
|
2017-12-19 22:15:05 +00:00
|
|
|
protected $c;
|
|
|
|
|
|
|
|
public function setUp() {
|
2017-12-22 16:45:24 +00:00
|
|
|
if (!PDODriver::requirementsMet()) {
|
2017-12-19 22:15:05 +00:00
|
|
|
$this->markTestSkipped("PDO-SQLite extension not loaded");
|
|
|
|
}
|
2017-12-22 16:45:24 +00:00
|
|
|
$this->clearData();
|
2017-12-19 22:15:05 +00:00
|
|
|
$c = new \PDO("sqlite::memory:", "", "", [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]);
|
|
|
|
$this->c = $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
unset($this->c);
|
|
|
|
$this->clearData();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructResult() {
|
|
|
|
$set = $this->c->query("SELECT 1");
|
2017-12-22 16:45:24 +00:00
|
|
|
$this->assertInstanceOf(Result::class, new PDOResult($set));
|
2017-12-19 22:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetChangeCountAndLastInsertId() {
|
|
|
|
$this->c->query("CREATE TABLE test(col)");
|
|
|
|
$set = $this->c->query("INSERT INTO test(col) values(1)");
|
|
|
|
$rows = $set->rowCount();
|
|
|
|
$id = $this->c->lastInsertID();
|
2017-12-22 16:45:24 +00:00
|
|
|
$r = new PDOResult($set, [$rows,$id]);
|
2017-12-19 22:15:05 +00:00
|
|
|
$this->assertSame((int) $rows, $r->changes());
|
|
|
|
$this->assertSame((int) $id, $r->lastId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIterateOverResults() {
|
|
|
|
$set = $this->c->query("SELECT 1 as col union select 2 as col union select 3 as col");
|
|
|
|
$rows = [];
|
2017-12-22 16:45:24 +00:00
|
|
|
foreach (new PDOResult($set) as $index => $row) {
|
2017-12-19 22:15:05 +00:00
|
|
|
$rows[$index] = $row['col'];
|
|
|
|
}
|
|
|
|
$this->assertSame([0 => "1", 1 => "2", 2 => "3"], $rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIterateOverResultsTwice() {
|
|
|
|
$set = $this->c->query("SELECT 1 as col union select 2 as col union select 3 as col");
|
|
|
|
$rows = [];
|
2017-12-22 16:45:24 +00:00
|
|
|
$test = new PDOResult($set);
|
2017-12-19 22:15:05 +00:00
|
|
|
foreach ($test as $row) {
|
|
|
|
$rows[] = $row['col'];
|
|
|
|
}
|
|
|
|
$this->assertSame(["1","2","3"], $rows);
|
|
|
|
$this->assertException("resultReused", "Db");
|
|
|
|
foreach ($test as $row) {
|
|
|
|
$rows[] = $row['col'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSingleValues() {
|
|
|
|
$set = $this->c->query("SELECT 1867 as year union select 1970 as year union select 2112 as year");
|
2017-12-22 16:45:24 +00:00
|
|
|
$test = new PDOResult($set);
|
2017-12-19 22:15:05 +00:00
|
|
|
$this->assertEquals(1867, $test->getValue());
|
|
|
|
$this->assertEquals(1970, $test->getValue());
|
|
|
|
$this->assertEquals(2112, $test->getValue());
|
|
|
|
$this->assertSame(null, $test->getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetFirstValuesOnly() {
|
|
|
|
$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-12-22 16:45:24 +00:00
|
|
|
$test = new PDOResult($set);
|
2017-12-19 22:15:05 +00:00
|
|
|
$this->assertEquals(1867, $test->getValue());
|
|
|
|
$this->assertEquals(1970, $test->getValue());
|
|
|
|
$this->assertEquals(2112, $test->getValue());
|
|
|
|
$this->assertSame(null, $test->getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
public 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'],
|
|
|
|
];
|
2017-12-22 16:45:24 +00:00
|
|
|
$test = new PDOResult($set);
|
2017-12-19 22:15:05 +00:00
|
|
|
$this->assertEquals($rows[0], $test->getRow());
|
|
|
|
$this->assertEquals($rows[1], $test->getRow());
|
|
|
|
$this->assertSame(null, $test->getRow());
|
|
|
|
}
|
|
|
|
|
|
|
|
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'],
|
|
|
|
];
|
2017-12-22 16:45:24 +00:00
|
|
|
$test = new PDOResult($set);
|
2017-12-19 22:15:05 +00:00
|
|
|
$this->assertEquals($rows, $test->getAll());
|
|
|
|
}
|
|
|
|
}
|