From 3e42fbdddf5595bbbccd3627c90cbaee0e4de383 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Fri, 8 Dec 2017 16:00:23 -0500 Subject: [PATCH] Munge off-by-one dates in tests; fixes #112 --- tests/lib/AbstractTest.php | 15 +++++++++++++++ tests/lib/Database/Setup.php | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/tests/lib/AbstractTest.php b/tests/lib/AbstractTest.php index 0937f81..44d10da 100644 --- a/tests/lib/AbstractTest.php +++ b/tests/lib/AbstractTest.php @@ -29,7 +29,22 @@ abstract class AbstractTest extends \PHPUnit\Framework\TestCase { } } + public function approximateTime($exp, $act) { + if (is_null($act)) { + return null; + } + $target = Date::normalize($exp)->getTimeStamp(); + $value = Date::normalize($act)->getTimeStamp(); + if ($value >= ($target - 1) && $value <= ($target + 1)) { + // if the actual time is off by no more than one second, it's acceptable + return $exp; + } else { + return $act; + } + } + public function assertTime($exp, $test, string $msg = null) { + $test = $this->approximateTime($exp, $test); $exp = Date::transform($exp, "iso8601"); $test = Date::transform($test, "iso8601"); $this->assertSame($exp, $test, $msg); diff --git a/tests/lib/Database/Setup.php b/tests/lib/Database/Setup.php index 3a93f4c..0adb9d3 100644 --- a/tests/lib/Database/Setup.php +++ b/tests/lib/Database/Setup.php @@ -82,11 +82,23 @@ trait Setup { public function compareExpectations(array $expected): bool { foreach ($expected as $table => $info) { $cols = implode(",", array_keys($info['columns'])); + $types = $info['columns']; $data = $this->drv->prepare("SELECT $cols from $table")->run()->getAll(); $cols = array_keys($info['columns']); foreach ($info['rows'] as $index => $row) { $this->assertCount(sizeof($cols), $row, "The number of values for array index $index does not match the number of fields"); $row = array_combine($cols, $row); + foreach($data as $index => $test) { + foreach ($test as $col => $value) { + if ($types[$col]=="datetime") { + $test[$col] = $this->approximateTime($row[$col], $value); + } + } + if($row===$test) { + $data[$index] = $test; + break; + } + } $this->assertContains($row, $data, "Table $table does not contain record at array index $index."); $found = array_search($row, $data, true); unset($data[$found]);