2017-03-02 23:42:19 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-03-02 23:42:19 +00:00
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse\Test\Db;
|
2017-08-29 14:50:31 +00:00
|
|
|
|
2017-03-28 04:12:12 +00:00
|
|
|
use JKingWeb\Arsse\Db\Statement;
|
2017-03-02 23:42:19 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
trait BindingTests {
|
|
|
|
public function testBindNull() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$input = null;
|
|
|
|
$exp = [
|
|
|
|
"null" => null,
|
|
|
|
"integer" => null,
|
|
|
|
"float" => null,
|
|
|
|
"date" => null,
|
|
|
|
"time" => null,
|
|
|
|
"datetime" => null,
|
|
|
|
"binary" => null,
|
|
|
|
"string" => null,
|
|
|
|
"boolean" => null,
|
|
|
|
];
|
|
|
|
$this->checkBinding($input, $exp);
|
2017-05-04 23:38:54 +00:00
|
|
|
// 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,
|
2017-07-07 19:25:47 +00:00
|
|
|
"date" => gmdate("Y-m-d", 0),
|
|
|
|
"time" => gmdate("H:i:s", 0),
|
|
|
|
"datetime" => gmdate("Y-m-d H:i:s", 0),
|
2017-05-04 23:38:54 +00:00
|
|
|
"binary" => "",
|
|
|
|
"string" => "",
|
|
|
|
"boolean" => 0,
|
|
|
|
];
|
|
|
|
$this->checkBinding($input, $exp, true);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-02 23:42:19 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testBindTrue() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$input = true;
|
|
|
|
$exp = [
|
|
|
|
"null" => null,
|
|
|
|
"integer" => 1,
|
|
|
|
"float" => 1.0,
|
|
|
|
"date" => null,
|
|
|
|
"time" => null,
|
|
|
|
"datetime" => null,
|
|
|
|
"binary" => "1",
|
|
|
|
"string" => "1",
|
|
|
|
"boolean" => 1,
|
|
|
|
];
|
|
|
|
$this->checkBinding($input, $exp);
|
2017-05-04 23:38:54 +00:00
|
|
|
$this->checkBinding($input, $exp, true);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-02 23:42:19 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testBindFalse() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$input = false;
|
|
|
|
$exp = [
|
|
|
|
"null" => null,
|
|
|
|
"integer" => 0,
|
|
|
|
"float" => 0.0,
|
|
|
|
"date" => null,
|
|
|
|
"time" => null,
|
|
|
|
"datetime" => null,
|
|
|
|
"binary" => "",
|
|
|
|
"string" => "",
|
|
|
|
"boolean" => 0,
|
|
|
|
];
|
|
|
|
$this->checkBinding($input, $exp);
|
2017-05-04 23:38:54 +00:00
|
|
|
$this->checkBinding($input, $exp, true);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-02 23:42:19 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testBindInteger() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$input = 2112;
|
|
|
|
$exp = [
|
|
|
|
"null" => null,
|
|
|
|
"integer" => 2112,
|
|
|
|
"float" => 2112.0,
|
2017-07-07 19:25:47 +00:00
|
|
|
"date" => gmdate("Y-m-d", 2112),
|
|
|
|
"time" => gmdate("H:i:s", 2112),
|
|
|
|
"datetime" => gmdate("Y-m-d H:i:s", 2112),
|
2017-04-07 01:41:21 +00:00
|
|
|
"binary" => "2112",
|
|
|
|
"string" => "2112",
|
|
|
|
"boolean" => 1,
|
|
|
|
];
|
|
|
|
$this->checkBinding($input, $exp);
|
2017-05-04 23:38:54 +00:00
|
|
|
$this->checkBinding($input, $exp, true);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-02 23:42:19 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testBindIntegerZero() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$input = 0;
|
|
|
|
$exp = [
|
|
|
|
"null" => null,
|
|
|
|
"integer" => 0,
|
|
|
|
"float" => 0.0,
|
2017-07-07 19:25:47 +00:00
|
|
|
"date" => gmdate("Y-m-d", 0),
|
|
|
|
"time" => gmdate("H:i:s", 0),
|
|
|
|
"datetime" => gmdate("Y-m-d H:i:s", 0),
|
2017-04-07 01:41:21 +00:00
|
|
|
"binary" => "0",
|
|
|
|
"string" => "0",
|
|
|
|
"boolean" => 0,
|
|
|
|
];
|
|
|
|
$this->checkBinding($input, $exp);
|
2017-05-04 23:38:54 +00:00
|
|
|
$this->checkBinding($input, $exp, true);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-02 23:42:19 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testBindFloat() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$input = 2112.0;
|
|
|
|
$exp = [
|
|
|
|
"null" => null,
|
|
|
|
"integer" => 2112,
|
|
|
|
"float" => 2112.0,
|
2017-07-07 19:25:47 +00:00
|
|
|
"date" => gmdate("Y-m-d", 2112),
|
|
|
|
"time" => gmdate("H:i:s", 2112),
|
|
|
|
"datetime" => gmdate("Y-m-d H:i:s", 2112),
|
2017-04-07 01:41:21 +00:00
|
|
|
"binary" => "2112",
|
|
|
|
"string" => "2112",
|
|
|
|
"boolean" => 1,
|
|
|
|
];
|
|
|
|
$this->checkBinding($input, $exp);
|
2017-05-04 23:38:54 +00:00
|
|
|
$this->checkBinding($input, $exp, true);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-02 23:42:19 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testBindFloatZero() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$input = 0.0;
|
|
|
|
$exp = [
|
|
|
|
"null" => null,
|
|
|
|
"integer" => 0,
|
|
|
|
"float" => 0.0,
|
2017-07-07 19:25:47 +00:00
|
|
|
"date" => gmdate("Y-m-d", 0),
|
|
|
|
"time" => gmdate("H:i:s", 0),
|
|
|
|
"datetime" => gmdate("Y-m-d H:i:s", 0),
|
2017-04-07 01:41:21 +00:00
|
|
|
"binary" => "0",
|
|
|
|
"string" => "0",
|
|
|
|
"boolean" => 0,
|
|
|
|
];
|
|
|
|
$this->checkBinding($input, $exp);
|
2017-05-04 23:38:54 +00:00
|
|
|
$this->checkBinding($input, $exp, true);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-02 23:42:19 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testBindAsciiString() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$input = "Random string";
|
|
|
|
$exp = [
|
|
|
|
"null" => null,
|
|
|
|
"integer" => 0,
|
|
|
|
"float" => 0.0,
|
|
|
|
"date" => null,
|
|
|
|
"time" => null,
|
|
|
|
"datetime" => null,
|
|
|
|
"binary" => $input,
|
|
|
|
"string" => $input,
|
|
|
|
"boolean" => 1,
|
|
|
|
];
|
|
|
|
$this->checkBinding($input, $exp);
|
2017-05-04 23:38:54 +00:00
|
|
|
$this->checkBinding($input, $exp, true);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-02 23:42:19 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testBindUtf8String() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$input = "é";
|
|
|
|
$exp = [
|
|
|
|
"null" => null,
|
|
|
|
"integer" => 0,
|
|
|
|
"float" => 0.0,
|
|
|
|
"date" => null,
|
|
|
|
"time" => null,
|
|
|
|
"datetime" => null,
|
|
|
|
"binary" => $input,
|
|
|
|
"string" => $input,
|
|
|
|
"boolean" => 1,
|
|
|
|
];
|
|
|
|
$this->checkBinding($input, $exp);
|
2017-05-04 23:38:54 +00:00
|
|
|
$this->checkBinding($input, $exp, true);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-02 23:42:19 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testBindBinaryString() {
|
2017-04-07 01:41:21 +00:00
|
|
|
// FIXME: This test may be unreliable; SQLite happily stores invalid UTF-8 text as bytes untouched, but other engines probably don't do this
|
|
|
|
$input = chr(233);
|
|
|
|
$exp = [
|
|
|
|
"null" => null,
|
|
|
|
"integer" => 0,
|
|
|
|
"float" => 0.0,
|
|
|
|
"date" => null,
|
|
|
|
"time" => null,
|
|
|
|
"datetime" => null,
|
|
|
|
"binary" => $input,
|
|
|
|
"string" => $input,
|
|
|
|
"boolean" => 1,
|
|
|
|
];
|
|
|
|
$this->checkBinding($input, $exp);
|
2017-05-04 23:38:54 +00:00
|
|
|
$this->checkBinding($input, $exp, true);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-02 23:42:19 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testBindIso8601DateString() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$input = "2017-01-09T13:11:17";
|
|
|
|
$time = strtotime($input);
|
|
|
|
$exp = [
|
|
|
|
"null" => null,
|
|
|
|
"integer" => 2017,
|
|
|
|
"float" => 2017.0,
|
2017-07-07 19:25:47 +00:00
|
|
|
"date" => gmdate("Y-m-d", $time),
|
|
|
|
"time" => gmdate("H:i:s", $time),
|
|
|
|
"datetime" => gmdate("Y-m-d H:i:s", $time),
|
2017-04-07 01:41:21 +00:00
|
|
|
"binary" => $input,
|
|
|
|
"string" => $input,
|
|
|
|
"boolean" => 1,
|
|
|
|
];
|
|
|
|
$this->checkBinding($input, $exp);
|
2017-05-04 23:38:54 +00:00
|
|
|
$this->checkBinding($input, $exp, true);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-02 23:42:19 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testBindArbitraryDateString() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$input = "Today";
|
|
|
|
$time = strtotime($input);
|
|
|
|
$exp = [
|
|
|
|
"null" => null,
|
|
|
|
"integer" => 0,
|
|
|
|
"float" => 0.0,
|
2017-07-07 19:25:47 +00:00
|
|
|
"date" => gmdate("Y-m-d", $time),
|
|
|
|
"time" => gmdate("H:i:s", $time),
|
|
|
|
"datetime" => gmdate("Y-m-d H:i:s", $time),
|
2017-04-07 01:41:21 +00:00
|
|
|
"binary" => $input,
|
|
|
|
"string" => $input,
|
|
|
|
"boolean" => 1,
|
|
|
|
];
|
|
|
|
$this->checkBinding($input, $exp);
|
2017-05-04 23:38:54 +00:00
|
|
|
$this->checkBinding($input, $exp, true);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-02 23:42:19 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testBindMutableDateObject($class = '\DateTime') {
|
2017-04-07 01:41:21 +00:00
|
|
|
$input = new $class("Noon Today");
|
|
|
|
$time = $input->getTimestamp();
|
|
|
|
$exp = [
|
|
|
|
"null" => null,
|
|
|
|
"integer" => $time,
|
|
|
|
"float" => (float) $time,
|
2017-07-07 19:25:47 +00:00
|
|
|
"date" => gmdate("Y-m-d", $time),
|
|
|
|
"time" => gmdate("H:i:s", $time),
|
|
|
|
"datetime" => gmdate("Y-m-d H:i:s", $time),
|
|
|
|
"binary" => gmdate("Y-m-d H:i:s", $time),
|
|
|
|
"string" => gmdate("Y-m-d H:i:s", $time),
|
2017-04-07 01:41:21 +00:00
|
|
|
"boolean" => 1,
|
|
|
|
];
|
|
|
|
$this->checkBinding($input, $exp);
|
2017-05-04 23:38:54 +00:00
|
|
|
$this->checkBinding($input, $exp, true);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-02 23:42:19 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testBindImmutableDateObject() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->testBindMutableDateObject('\DateTimeImmutable');
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|