Browse Source

Munge off-by-one dates in tests; fixes #112

microsub
J. King 6 years ago
parent
commit
3e42fbdddf
  1. 15
      tests/lib/AbstractTest.php
  2. 12
      tests/lib/Database/Setup.php

15
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);

12
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]);

Loading…
Cancel
Save