1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 17:12:41 +00:00
Arsse/tests/lib/Db/BindingTests.php

254 lines
7.9 KiB
PHP
Raw Normal View History

<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
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-08-29 14:50:31 +00:00
trait BindingTests {
public function testBindNull() {
$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,
"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-08-29 14:50:31 +00:00
public function testBindTrue() {
$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-08-29 14:50:31 +00:00
public function testBindFalse() {
$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-08-29 14:50:31 +00:00
public function testBindInteger() {
$input = 2112;
$exp = [
"null" => null,
"integer" => 2112,
"float" => 2112.0,
"date" => gmdate("Y-m-d", 2112),
"time" => gmdate("H:i:s", 2112),
"datetime" => gmdate("Y-m-d H:i:s", 2112),
"binary" => "2112",
"string" => "2112",
"boolean" => 1,
];
$this->checkBinding($input, $exp);
2017-05-04 23:38:54 +00:00
$this->checkBinding($input, $exp, true);
}
2017-08-29 14:50:31 +00:00
public function testBindIntegerZero() {
$input = 0;
$exp = [
"null" => null,
"integer" => 0,
"float" => 0.0,
"date" => gmdate("Y-m-d", 0),
"time" => gmdate("H:i:s", 0),
"datetime" => gmdate("Y-m-d H:i:s", 0),
"binary" => "0",
"string" => "0",
"boolean" => 0,
];
$this->checkBinding($input, $exp);
2017-05-04 23:38:54 +00:00
$this->checkBinding($input, $exp, true);
}
2017-08-29 14:50:31 +00:00
public function testBindFloat() {
$input = 2112.0;
$exp = [
"null" => null,
"integer" => 2112,
"float" => 2112.0,
"date" => gmdate("Y-m-d", 2112),
"time" => gmdate("H:i:s", 2112),
"datetime" => gmdate("Y-m-d H:i:s", 2112),
"binary" => "2112",
"string" => "2112",
"boolean" => 1,
];
$this->checkBinding($input, $exp);
2017-05-04 23:38:54 +00:00
$this->checkBinding($input, $exp, true);
}
2017-08-29 14:50:31 +00:00
public function testBindFloatZero() {
$input = 0.0;
$exp = [
"null" => null,
"integer" => 0,
"float" => 0.0,
"date" => gmdate("Y-m-d", 0),
"time" => gmdate("H:i:s", 0),
"datetime" => gmdate("Y-m-d H:i:s", 0),
"binary" => "0",
"string" => "0",
"boolean" => 0,
];
$this->checkBinding($input, $exp);
2017-05-04 23:38:54 +00:00
$this->checkBinding($input, $exp, true);
}
2017-08-29 14:50:31 +00:00
public function testBindAsciiString() {
$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-08-29 14:50:31 +00:00
public function testBindUtf8String() {
$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-08-29 14:50:31 +00:00
public function testBindBinaryString() {
// 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-08-29 14:50:31 +00:00
public function testBindIso8601DateString() {
$input = "2017-01-09T13:11:17";
$time = strtotime($input." UTC");
$exp = [
"null" => null,
"integer" => 2017,
"float" => 2017.0,
"date" => gmdate("Y-m-d", $time),
"time" => gmdate("H:i:s", $time),
"datetime" => gmdate("Y-m-d H:i:s", $time),
"binary" => $input,
"string" => $input,
"boolean" => 1,
];
$this->checkBinding($input, $exp);
2017-05-04 23:38:54 +00:00
$this->checkBinding($input, $exp, true);
}
2017-08-29 14:50:31 +00:00
public function testBindArbitraryDateString() {
$input = "Today";
$time = date_create($input, new \DateTimezone("UTC"))->getTimestamp();
$exp = [
"null" => null,
"integer" => 0,
"float" => 0.0,
"date" => gmdate("Y-m-d", $time),
"time" => gmdate("H:i:s", $time),
"datetime" => gmdate("Y-m-d H:i:s", $time),
"binary" => $input,
"string" => $input,
"boolean" => 1,
];
$this->checkBinding($input, $exp);
2017-05-04 23:38:54 +00:00
$this->checkBinding($input, $exp, true);
}
2017-08-29 14:50:31 +00:00
public function testBindMutableDateObject($class = '\DateTime') {
$input = new $class("Noon Today");
$time = $input->getTimestamp();
$exp = [
"null" => null,
"integer" => $time,
"float" => (float) $time,
"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),
"boolean" => 1,
];
$this->checkBinding($input, $exp);
2017-05-04 23:38:54 +00:00
$this->checkBinding($input, $exp, true);
}
2017-08-29 14:50:31 +00:00
public function testBindImmutableDateObject() {
$this->testBindMutableDateObject('\DateTimeImmutable');
}
2017-08-29 14:50:31 +00:00
}