2017-03-02 03:47:51 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse;
|
|
|
|
use JKingWeb\Arsse\Db\Statement;
|
2017-03-02 03:47:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestDbStatementSQLite3 extends \PHPUnit\Framework\TestCase {
|
2017-03-02 23:42:19 +00:00
|
|
|
use Test\Tools, Test\Db\BindingTests;
|
2017-03-02 03:47:51 +00:00
|
|
|
|
|
|
|
protected $c;
|
2017-04-07 01:41:21 +00:00
|
|
|
static protected $imp = Db\SQLite3\Statement::class;
|
2017-03-02 03:47:51 +00:00
|
|
|
|
|
|
|
function setUp() {
|
2017-05-22 14:02:36 +00:00
|
|
|
if(!extension_loaded("sqlite3")) {
|
|
|
|
$this->markTestSkipped("SQLite extension not loaded");
|
|
|
|
}
|
2017-03-02 03:47:51 +00:00
|
|
|
$c = new \SQLite3(":memory:");
|
|
|
|
$c->enableExceptions(true);
|
|
|
|
$this->c = $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
function tearDown() {
|
2017-04-07 01:41:21 +00:00
|
|
|
try {$this->s->close();} catch(\Exception $e) {}
|
2017-03-02 03:47:51 +00:00
|
|
|
$this->c->close();
|
|
|
|
unset($this->c);
|
|
|
|
}
|
|
|
|
|
2017-05-04 23:38:54 +00:00
|
|
|
protected function checkBinding($input, array $expectations, bool $strict = false) {
|
2017-04-07 01:41:21 +00:00
|
|
|
$nativeStatement = $this->c->prepare("SELECT ? as value");
|
|
|
|
$s = new self::$imp($this->c, $nativeStatement);
|
|
|
|
$types = array_unique(Statement::TYPES);
|
|
|
|
foreach($types as $type) {
|
2017-05-04 23:38:54 +00:00
|
|
|
$s->rebindArray([$strict ? "strict $type" : $type]);
|
2017-04-07 01:41:21 +00:00
|
|
|
$val = $s->runArray([$input])->getRow()['value'];
|
|
|
|
$this->assertSame($expectations[$type], $val, "Type $type failed comparison.");
|
|
|
|
}
|
|
|
|
}
|
2017-03-03 18:20:26 +00:00
|
|
|
|
2017-04-07 01:41:21 +00:00
|
|
|
function testConstructStatement() {
|
2017-03-03 18:20:26 +00:00
|
|
|
$nativeStatement = $this->c->prepare("SELECT ? as value");
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertInstanceOf(Statement::class, new Db\SQLite3\Statement($this->c, $nativeStatement));
|
|
|
|
}
|
|
|
|
|
|
|
|
function testBindMissingValue() {
|
|
|
|
$nativeStatement = $this->c->prepare("SELECT ? as value");
|
|
|
|
$s = new self::$imp($this->c, $nativeStatement);
|
|
|
|
$val = $s->runArray()->getRow()['value'];
|
|
|
|
$this->assertSame(null, $val);
|
|
|
|
}
|
2017-03-03 18:20:26 +00:00
|
|
|
|
|
|
|
function testBindMultipleValues() {
|
|
|
|
$exp = [
|
|
|
|
'one' => 1,
|
|
|
|
'two' => 2,
|
|
|
|
];
|
2017-04-07 01:41:21 +00:00
|
|
|
$nativeStatement = $this->c->prepare("SELECT ? as one, ? as two");
|
|
|
|
$s = new self::$imp($this->c, $nativeStatement, ["int", "int"]);
|
|
|
|
$val = $s->runArray([1,2])->getRow();
|
|
|
|
$this->assertSame($exp, $val);
|
2017-03-03 18:20:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 01:41:21 +00:00
|
|
|
function testBindRecursively() {
|
2017-04-07 01:24:26 +00:00
|
|
|
$exp = [
|
|
|
|
'one' => 1,
|
|
|
|
'two' => 2,
|
|
|
|
'three' => 3,
|
2017-04-07 01:41:21 +00:00
|
|
|
'four' => 4,
|
2017-04-07 01:24:26 +00:00
|
|
|
];
|
2017-04-07 01:41:21 +00:00
|
|
|
$nativeStatement = $this->c->prepare("SELECT ? as one, ? as two, ? as three, ? as four");
|
|
|
|
$s = new self::$imp($this->c, $nativeStatement, ["int", ["int", "int"], "int"]);
|
|
|
|
$val = $s->runArray([1, [2, 3], 4])->getRow();
|
|
|
|
$this->assertSame($exp, $val);
|
|
|
|
}
|
2017-04-07 01:24:26 +00:00
|
|
|
|
2017-03-03 18:20:26 +00:00
|
|
|
function testBindWithoutType() {
|
2017-03-08 18:59:20 +00:00
|
|
|
$nativeStatement = $this->c->prepare("SELECT ? as value");
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertException("paramTypeMissing", "Db");
|
|
|
|
$s = new self::$imp($this->c, $nativeStatement, []);
|
|
|
|
$s->runArray([1]);
|
2017-03-03 18:20:26 +00:00
|
|
|
}
|
2017-03-08 18:59:20 +00:00
|
|
|
|
2017-04-07 01:41:21 +00:00
|
|
|
function testViolateConstraint() {
|
|
|
|
$this->c->exec("CREATE TABLE test(id integer not null)");
|
|
|
|
$nativeStatement = $this->c->prepare("INSERT INTO test(id) values(?)");
|
|
|
|
$s = new self::$imp($this->c, $nativeStatement, ["int"]);
|
|
|
|
$this->assertException("constraintViolation", "Db", "ExceptionInput");
|
|
|
|
$s->runArray([null]);
|
|
|
|
}
|
2017-03-09 14:44:50 +00:00
|
|
|
|
2017-04-07 01:41:21 +00:00
|
|
|
function testMismatchTypes() {
|
|
|
|
$this->c->exec("CREATE TABLE test(id integer primary key)");
|
|
|
|
$nativeStatement = $this->c->prepare("INSERT INTO test(id) values(?)");
|
|
|
|
$s = new self::$imp($this->c, $nativeStatement, ["str"]);
|
|
|
|
$this->assertException("typeViolation", "Db", "ExceptionInput");
|
|
|
|
$s->runArray(['ook']);
|
|
|
|
}
|
2017-03-02 03:47:51 +00:00
|
|
|
}
|