2017-03-02 03:47:51 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace JKingWeb\NewsSync;
|
2017-03-03 18:20:26 +00:00
|
|
|
use JKingWeb\NewsSync\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-03-07 23:01:13 +00:00
|
|
|
static protected $imp = Db\SQLite3\Statement::class;
|
2017-03-02 03:47:51 +00:00
|
|
|
|
|
|
|
function setUp() {
|
|
|
|
date_default_timezone_set("UTC");
|
|
|
|
$c = new \SQLite3(":memory:");
|
|
|
|
$c->enableExceptions(true);
|
|
|
|
$this->c = $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
function tearDown() {
|
|
|
|
try {$this->s->close();} catch(\Exception $e) {}
|
|
|
|
$this->c->close();
|
|
|
|
unset($this->c);
|
|
|
|
}
|
|
|
|
|
2017-03-03 18:20:26 +00:00
|
|
|
protected function checkBinding($input, array $expectations) {
|
|
|
|
$nativeStatement = $this->c->prepare("SELECT ? as value");
|
|
|
|
$s = new self::$imp($this->c, $nativeStatement);
|
|
|
|
$types = array_unique(Statement::TYPES);
|
|
|
|
foreach($types as $type) {
|
|
|
|
$s->rebindArray([$type]);
|
2017-03-09 20:01:18 +00:00
|
|
|
$val = $s->runArray([$input])->getRow()['value'];
|
2017-03-03 18:20:26 +00:00
|
|
|
$this->assertSame($expectations[$type], $val, "Type $type failed comparison.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 03:47:51 +00:00
|
|
|
function testConstructStatement() {
|
2017-03-03 18:20:26 +00:00
|
|
|
$nativeStatement = $this->c->prepare("SELECT ? as value");
|
2017-03-07 23:01:13 +00:00
|
|
|
$this->assertInstanceOf(Statement::class, new Db\SQLite3\Statement($this->c, $nativeStatement));
|
2017-03-02 03:47:51 +00:00
|
|
|
}
|
2017-03-03 18:20:26 +00:00
|
|
|
|
|
|
|
function testBindMissingValue() {
|
|
|
|
$nativeStatement = $this->c->prepare("SELECT ? as value");
|
|
|
|
$s = new self::$imp($this->c, $nativeStatement);
|
2017-03-09 20:01:18 +00:00
|
|
|
$val = $s->runArray()->getRow()['value'];
|
2017-03-03 18:20:26 +00:00
|
|
|
$this->assertSame(null, $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testBindMultipleValues() {
|
|
|
|
$exp = [
|
|
|
|
'one' => 1,
|
|
|
|
'two' => 2,
|
|
|
|
];
|
|
|
|
$nativeStatement = $this->c->prepare("SELECT ? as one, ? as two");
|
|
|
|
$s = new self::$imp($this->c, $nativeStatement, ["int", "int"]);
|
2017-03-09 20:01:18 +00:00
|
|
|
$val = $s->runArray([1,2])->getRow();
|
2017-03-03 18:20:26 +00:00
|
|
|
$this->assertSame($exp, $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testBindWithoutType() {
|
2017-03-08 18:59:20 +00:00
|
|
|
$nativeStatement = $this->c->prepare("SELECT ? as value");
|
|
|
|
$this->assertException("paramTypeMissing", "Db");
|
2017-03-03 18:20:26 +00:00
|
|
|
$s = new self::$imp($this->c, $nativeStatement, []);
|
2017-03-09 03:16:35 +00:00
|
|
|
$s->runArray([1]);
|
2017-03-03 18:20:26 +00:00
|
|
|
}
|
2017-03-08 18:59:20 +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");
|
2017-03-09 03:16:35 +00:00
|
|
|
$s->runArray([null]);
|
2017-03-08 18:59:20 +00:00
|
|
|
}
|
2017-03-09 14:44:50 +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
|
|
|
}
|