From 2822864a85bb7a9190742ffa45b70c197c2958fe Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sat, 4 Jun 2022 20:16:22 -0400 Subject: [PATCH] Fix most test failures MySQL is still being stubborn. It may be a type-conversion issue. --- lib/Db/MySQL/Driver.php | 2 +- tests/lib/AbstractTest.php | 25 ++++++++++++++++++++++++- tests/lib/DatabaseDrivers/MySQL.php | 4 ++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/Db/MySQL/Driver.php b/lib/Db/MySQL/Driver.php index de3bc3d6..532d05d3 100644 --- a/lib/Db/MySQL/Driver.php +++ b/lib/Db/MySQL/Driver.php @@ -165,7 +165,7 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver { $drv->report_mode = \MYSQLI_REPORT_OFF; $this->db = mysqli_init(); $this->db->options(\MYSQLI_SET_CHARSET_NAME, "utf8mb4"); - $this->db->options(\MYSQLI_OPT_INT_AND_FLOAT_NATIVE, false); + $this->db->options(\MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true); $this->db->options(\MYSQLI_OPT_CONNECT_TIMEOUT, ceil(Arsse::$conf->dbTimeoutConnect)); @$this->db->real_connect($host, $user, $password, $db, $port, $socket); if ($this->db->connect_errno) { diff --git a/tests/lib/AbstractTest.php b/tests/lib/AbstractTest.php index f874a107..d352bd6d 100644 --- a/tests/lib/AbstractTest.php +++ b/tests/lib/AbstractTest.php @@ -415,9 +415,11 @@ abstract class AbstractTest extends \PHPUnit\Framework\TestCase { foreach ($info['rows'] as $r) { $row = []; foreach ($r as $c => $v) { + // store any date values for later comparison if ($types[$c] === "datetime") { $dates[] = $v; } + // serialize to CSV, null being represented by no value if ($v === null) { $row[] = ""; } elseif (static::$stringOutput || is_string($v)) { @@ -435,9 +437,11 @@ abstract class AbstractTest extends \PHPUnit\Framework\TestCase { $data = $drv->prepare("SELECT $cols from $table")->run()->getAll(); $types = $info['columns']; $act = []; + $extra = []; foreach ($data as $r) { $row = []; foreach ($r as $c => $v) { + // account for dates which might be off by one second if ($types[$c] === "datetime") { if (array_search($v, $dates, true) === false) { $v = Date::transform(Date::sub("PT1S", $v), "sql"); @@ -457,8 +461,27 @@ abstract class AbstractTest extends \PHPUnit\Framework\TestCase { $row[] = (string) $v; } } - $act[] = implode(",", $row); + $row = implode(",", $row); + // now search for the actual output row in the expected output + $found = array_keys($exp, $row, true); + foreach ($found as $k) { + if(!isset($act[$k])) { + $act[$k] = $row; + // skip to the next row + continue 2; + } + } + // if the row was not found, add it to a buffer which will be added to the actual output once all found rows are processed + $extra[] = $row; } + // add any unfound rows to the end of the actual array + $base = sizeof($exp) + 1; + foreach ($extra as $k => $v) { + $act[$base + $k] = $v; + } + // sort the actual output by keys + ksort($act); + // finally perform the comparison to be shown to the tester $this->assertSame($exp, $act, "Actual table $table does not match expectations"); } } diff --git a/tests/lib/DatabaseDrivers/MySQL.php b/tests/lib/DatabaseDrivers/MySQL.php index 2b6c0164..ab772be0 100644 --- a/tests/lib/DatabaseDrivers/MySQL.php +++ b/tests/lib/DatabaseDrivers/MySQL.php @@ -16,7 +16,7 @@ trait MySQL { protected static $dbResultClass = \JKingWeb\Arsse\Db\MySQL\Result::class; protected static $dbStatementClass = \JKingWeb\Arsse\Db\MySQL\Statement::class; protected static $dbDriverClass = \JKingWeb\Arsse\Db\MySQL\Driver::class; - protected static $stringOutput = true; + protected static $stringOutput = false; public static function dbInterface() { if (!class_exists("mysqli")) { @@ -25,7 +25,7 @@ trait MySQL { $drv = new \mysqli_driver; $drv->report_mode = \MYSQLI_REPORT_OFF; $d = mysqli_init(); - $d->options(\MYSQLI_OPT_INT_AND_FLOAT_NATIVE, false); + $d->options(\MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true); $d->options(\MYSQLI_SET_CHARSET_NAME, "utf8mb4"); @$d->real_connect(Arsse::$conf->dbMySQLHost, Arsse::$conf->dbMySQLUser, Arsse::$conf->dbMySQLPass, Arsse::$conf->dbMySQLDb, Arsse::$conf->dbMySQLPort); if ($d->connect_errno) {