Browse Source

Tests for strict binding

microsub
J. King 7 years ago
parent
commit
fb922e65e4
  1. 4
      tests/Db/SQLite3/TestDbStatementSQLite3.php
  2. 26
      tests/lib/Db/BindingTests.php

4
tests/Db/SQLite3/TestDbStatementSQLite3.php

@ -23,12 +23,12 @@ class TestDbStatementSQLite3 extends \PHPUnit\Framework\TestCase {
unset($this->c); unset($this->c);
} }
protected function checkBinding($input, array $expectations) { protected function checkBinding($input, array $expectations, bool $strict = false) {
$nativeStatement = $this->c->prepare("SELECT ? as value"); $nativeStatement = $this->c->prepare("SELECT ? as value");
$s = new self::$imp($this->c, $nativeStatement); $s = new self::$imp($this->c, $nativeStatement);
$types = array_unique(Statement::TYPES); $types = array_unique(Statement::TYPES);
foreach($types as $type) { foreach($types as $type) {
$s->rebindArray([$type]); $s->rebindArray([$strict ? "strict $type" : $type]);
$val = $s->runArray([$input])->getRow()['value']; $val = $s->runArray([$input])->getRow()['value'];
$this->assertSame($expectations[$type], $val, "Type $type failed comparison."); $this->assertSame($expectations[$type], $val, "Type $type failed comparison.");
} }

26
tests/lib/Db/BindingTests.php

@ -18,6 +18,20 @@ trait BindingTests {
"boolean" => null, "boolean" => null,
]; ];
$this->checkBinding($input, $exp); $this->checkBinding($input, $exp);
// types may also be strict (e.g. "strict integer") and never pass null to the database; this is useful for NOT NULL columns
// only null input should yield different results, so only this test has different expectations
$exp = [
"null" => null,
"integer" => 0,
"float" => 0.0,
"date" => date(self::$imp::dateFormat(Statement::TS_DATE), 0),
"time" => date(self::$imp::dateFormat(Statement::TS_TIME), 0),
"datetime" => date(self::$imp::dateFormat(Statement::TS_BOTH), 0),
"binary" => "",
"string" => "",
"boolean" => 0,
];
$this->checkBinding($input, $exp, true);
} }
function testBindTrue() { function testBindTrue() {
@ -34,6 +48,7 @@ trait BindingTests {
"boolean" => 1, "boolean" => 1,
]; ];
$this->checkBinding($input, $exp); $this->checkBinding($input, $exp);
$this->checkBinding($input, $exp, true);
} }
function testBindFalse() { function testBindFalse() {
@ -50,6 +65,7 @@ trait BindingTests {
"boolean" => 0, "boolean" => 0,
]; ];
$this->checkBinding($input, $exp); $this->checkBinding($input, $exp);
$this->checkBinding($input, $exp, true);
} }
function testBindInteger() { function testBindInteger() {
@ -66,6 +82,7 @@ trait BindingTests {
"boolean" => 1, "boolean" => 1,
]; ];
$this->checkBinding($input, $exp); $this->checkBinding($input, $exp);
$this->checkBinding($input, $exp, true);
} }
function testBindIntegerZero() { function testBindIntegerZero() {
@ -82,6 +99,7 @@ trait BindingTests {
"boolean" => 0, "boolean" => 0,
]; ];
$this->checkBinding($input, $exp); $this->checkBinding($input, $exp);
$this->checkBinding($input, $exp, true);
} }
function testBindFloat() { function testBindFloat() {
@ -98,6 +116,7 @@ trait BindingTests {
"boolean" => 1, "boolean" => 1,
]; ];
$this->checkBinding($input, $exp); $this->checkBinding($input, $exp);
$this->checkBinding($input, $exp, true);
} }
function testBindFloatZero() { function testBindFloatZero() {
@ -114,6 +133,7 @@ trait BindingTests {
"boolean" => 0, "boolean" => 0,
]; ];
$this->checkBinding($input, $exp); $this->checkBinding($input, $exp);
$this->checkBinding($input, $exp, true);
} }
function testBindAsciiString() { function testBindAsciiString() {
@ -130,6 +150,7 @@ trait BindingTests {
"boolean" => 1, "boolean" => 1,
]; ];
$this->checkBinding($input, $exp); $this->checkBinding($input, $exp);
$this->checkBinding($input, $exp, true);
} }
function testBindUtf8String() { function testBindUtf8String() {
@ -146,6 +167,7 @@ trait BindingTests {
"boolean" => 1, "boolean" => 1,
]; ];
$this->checkBinding($input, $exp); $this->checkBinding($input, $exp);
$this->checkBinding($input, $exp, true);
} }
function testBindBinaryString() { function testBindBinaryString() {
@ -163,6 +185,7 @@ trait BindingTests {
"boolean" => 1, "boolean" => 1,
]; ];
$this->checkBinding($input, $exp); $this->checkBinding($input, $exp);
$this->checkBinding($input, $exp, true);
} }
function testBindIso8601DateString() { function testBindIso8601DateString() {
@ -180,6 +203,7 @@ trait BindingTests {
"boolean" => 1, "boolean" => 1,
]; ];
$this->checkBinding($input, $exp); $this->checkBinding($input, $exp);
$this->checkBinding($input, $exp, true);
} }
function testBindArbitraryDateString() { function testBindArbitraryDateString() {
@ -197,6 +221,7 @@ trait BindingTests {
"boolean" => 1, "boolean" => 1,
]; ];
$this->checkBinding($input, $exp); $this->checkBinding($input, $exp);
$this->checkBinding($input, $exp, true);
} }
function testBindMutableDateObject($class = '\DateTime') { function testBindMutableDateObject($class = '\DateTime') {
@ -214,6 +239,7 @@ trait BindingTests {
"boolean" => 1, "boolean" => 1,
]; ];
$this->checkBinding($input, $exp); $this->checkBinding($input, $exp);
$this->checkBinding($input, $exp, true);
} }
function testBindImmutableDateObject() { function testBindImmutableDateObject() {

Loading…
Cancel
Save