From fb922e65e45f03febccf9b0ef1f0f57d0bbd5a3e Mon Sep 17 00:00:00 2001 From: "J. King" Date: Thu, 4 May 2017 19:38:54 -0400 Subject: [PATCH] Tests for strict binding --- tests/Db/SQLite3/TestDbStatementSQLite3.php | 4 ++-- tests/lib/Db/BindingTests.php | 26 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/tests/Db/SQLite3/TestDbStatementSQLite3.php b/tests/Db/SQLite3/TestDbStatementSQLite3.php index 77ea9aa..f347c01 100644 --- a/tests/Db/SQLite3/TestDbStatementSQLite3.php +++ b/tests/Db/SQLite3/TestDbStatementSQLite3.php @@ -23,12 +23,12 @@ class TestDbStatementSQLite3 extends \PHPUnit\Framework\TestCase { 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"); $s = new self::$imp($this->c, $nativeStatement); $types = array_unique(Statement::TYPES); foreach($types as $type) { - $s->rebindArray([$type]); + $s->rebindArray([$strict ? "strict $type" : $type]); $val = $s->runArray([$input])->getRow()['value']; $this->assertSame($expectations[$type], $val, "Type $type failed comparison."); } diff --git a/tests/lib/Db/BindingTests.php b/tests/lib/Db/BindingTests.php index 599d314..9a2e3b2 100644 --- a/tests/lib/Db/BindingTests.php +++ b/tests/lib/Db/BindingTests.php @@ -18,6 +18,20 @@ trait BindingTests { "boolean" => null, ]; $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() { @@ -34,6 +48,7 @@ trait BindingTests { "boolean" => 1, ]; $this->checkBinding($input, $exp); + $this->checkBinding($input, $exp, true); } function testBindFalse() { @@ -50,6 +65,7 @@ trait BindingTests { "boolean" => 0, ]; $this->checkBinding($input, $exp); + $this->checkBinding($input, $exp, true); } function testBindInteger() { @@ -66,6 +82,7 @@ trait BindingTests { "boolean" => 1, ]; $this->checkBinding($input, $exp); + $this->checkBinding($input, $exp, true); } function testBindIntegerZero() { @@ -82,6 +99,7 @@ trait BindingTests { "boolean" => 0, ]; $this->checkBinding($input, $exp); + $this->checkBinding($input, $exp, true); } function testBindFloat() { @@ -98,6 +116,7 @@ trait BindingTests { "boolean" => 1, ]; $this->checkBinding($input, $exp); + $this->checkBinding($input, $exp, true); } function testBindFloatZero() { @@ -114,6 +133,7 @@ trait BindingTests { "boolean" => 0, ]; $this->checkBinding($input, $exp); + $this->checkBinding($input, $exp, true); } function testBindAsciiString() { @@ -130,6 +150,7 @@ trait BindingTests { "boolean" => 1, ]; $this->checkBinding($input, $exp); + $this->checkBinding($input, $exp, true); } function testBindUtf8String() { @@ -146,6 +167,7 @@ trait BindingTests { "boolean" => 1, ]; $this->checkBinding($input, $exp); + $this->checkBinding($input, $exp, true); } function testBindBinaryString() { @@ -163,6 +185,7 @@ trait BindingTests { "boolean" => 1, ]; $this->checkBinding($input, $exp); + $this->checkBinding($input, $exp, true); } function testBindIso8601DateString() { @@ -180,6 +203,7 @@ trait BindingTests { "boolean" => 1, ]; $this->checkBinding($input, $exp); + $this->checkBinding($input, $exp, true); } function testBindArbitraryDateString() { @@ -197,6 +221,7 @@ trait BindingTests { "boolean" => 1, ]; $this->checkBinding($input, $exp); + $this->checkBinding($input, $exp, true); } function testBindMutableDateObject($class = '\DateTime') { @@ -214,6 +239,7 @@ trait BindingTests { "boolean" => 1, ]; $this->checkBinding($input, $exp); + $this->checkBinding($input, $exp, true); } function testBindImmutableDateObject() {