mirror of
https://github.com/silverstripe/silverstripe-sqlite3
synced 2024-10-22 17:05:37 +02:00
MINOR: fixed the test to be independant of php time()
This commit is contained in:
parent
065106bb80
commit
31d6e2fad8
@ -1,6 +1,25 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class DbDatetimeTest extends FunctionalTest {
|
class DbDatetimeTest extends FunctionalTest {
|
||||||
|
static $fixture_file = 'sqlite3/tests/DbDatetimeTest.yml';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if dates match more or less. This takes into the account the db query
|
||||||
|
* can overflow to the next second giving offset readings.
|
||||||
|
*/
|
||||||
|
private function matchesRoughly($date1, $date2, $comment = '') {
|
||||||
|
$allowedDifference = 5; // seconds
|
||||||
|
|
||||||
|
$time1 = is_numeric($date1) ? $date1 : strtotime($date1);
|
||||||
|
$time2 = is_numeric($date2) ? $date2 : strtotime($date2);
|
||||||
|
|
||||||
|
$this->assertTrue(abs($time1-$time2)<$allowedDifference, $comment . " ($date1, $date2)");
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getDbNow() {
|
||||||
|
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('now', '%U');
|
||||||
|
return DB::query($query)->value();
|
||||||
|
}
|
||||||
|
|
||||||
function setUp() {
|
function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
@ -8,37 +27,28 @@ class DbDatetimeTest extends FunctionalTest {
|
|||||||
$this->supportDbDatetime = method_exists($this->adapter, 'datetimeIntervalClause');
|
$this->supportDbDatetime = method_exists($this->adapter, 'datetimeIntervalClause');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testIfWebserverInSyncWithDbServer() {
|
|
||||||
if($this->supportDbDatetime) {
|
|
||||||
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('now', '%Y-%m-%d %H:%i:%s');
|
|
||||||
$result = DB::query($query)->value();
|
|
||||||
$this->assertEquals($result, date('Y-m-d H:i:s'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function testCorrectNow() {
|
function testCorrectNow() {
|
||||||
if($this->supportDbDatetime) {
|
if($this->supportDbDatetime) {
|
||||||
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('now', '%U');
|
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('now', '%U');
|
||||||
$result = DB::query($query)->value();
|
$result = DB::query($query)->value();
|
||||||
$this->assertEquals($result, time());
|
$this->assertRegExp('/^\d*$/', $result);
|
||||||
|
$this->assertTrue($result>0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function testDbDatetimeFormat() {
|
function testDbDatetimeFormat() {
|
||||||
if($this->supportDbDatetime) {
|
if($this->supportDbDatetime) {
|
||||||
|
|
||||||
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('1973-10-14 10:30:00', '%H:%i, %d/%m/%Y');
|
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('1973-10-14 10:30:00', '%H:%i, %d/%m/%Y');
|
||||||
$result = DB::query($query)->value();
|
$result = DB::query($query)->value();
|
||||||
$this->assertEquals($result, date('H:i, d/m/Y', strtotime('1973-10-14 10:30:00')), 'nice literal time');
|
$this->matchesRoughly($result, date('H:i, d/m/Y', strtotime('1973-10-14 10:30:00')), 'nice literal time');
|
||||||
|
|
||||||
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('now', '%d');
|
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('now', '%d');
|
||||||
$result = DB::query($query)->value();
|
$result = DB::query($query)->value();
|
||||||
$this->assertEquals($result, date('d'), 'todays day');
|
$this->matchesRoughly($result, date('d', $this->getDbNow()), 'todays day');
|
||||||
|
|
||||||
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('"Created"', '%U') . ' AS test FROM "SiteTree" WHERE "URLSegment" = \'home\'';
|
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('"Created"', '%U') . ' AS test FROM "SiteTree" WHERE "URLSegment" = \'home\'';
|
||||||
$result = DB::query($query)->value();
|
$result = DB::query($query)->value();
|
||||||
$this->assertEquals($result, date('U', strtotime(Dataobject::get_one('SiteTree',"\"URLSegment\" = 'home'")->Created)), 'SiteTree[home]->Created as timestamp');
|
$this->matchesRoughly($result, strtotime(Dataobject::get_one('SiteTree',"\"URLSegment\" = 'home'")->Created), 'SiteTree[home]->Created as timestamp');
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,15 +57,15 @@ class DbDatetimeTest extends FunctionalTest {
|
|||||||
|
|
||||||
$query = 'SELECT ' . $this->adapter->datetimeIntervalClause('1973-10-14 10:30:00', '+18 Years');
|
$query = 'SELECT ' . $this->adapter->datetimeIntervalClause('1973-10-14 10:30:00', '+18 Years');
|
||||||
$result = DB::query($query)->value();
|
$result = DB::query($query)->value();
|
||||||
$this->assertEquals($result, '1991-10-14 10:30:00', 'add 18 years');
|
$this->matchesRoughly($result, '1991-10-14 10:30:00', 'add 18 years');
|
||||||
|
|
||||||
$query = 'SELECT ' . $this->adapter->datetimeIntervalClause('now', '+1 Day');
|
$query = 'SELECT ' . $this->adapter->datetimeIntervalClause('now', '+1 Day');
|
||||||
$result = DB::query($query)->value();
|
$result = DB::query($query)->value();
|
||||||
$this->assertEquals($result, date('Y-m-d H:i:s', strtotime('+1 Day')), 'tomorrow');
|
$this->matchesRoughly($result, date('Y-m-d H:i:s', strtotime('+1 Day', $this->getDbNow())), 'tomorrow');
|
||||||
|
|
||||||
$query = 'SELECT ' . $this->adapter->datetimeIntervalClause('"Created"', '-15 Minutes') . ' AS "test" FROM "SiteTree" WHERE "URLSegment" = \'home\'';
|
$query = 'SELECT ' . $this->adapter->datetimeIntervalClause('"Created"', '-15 Minutes') . ' AS "test" FROM "SiteTree" WHERE "URLSegment" = \'home\'';
|
||||||
$result = DB::query($query)->value();
|
$result = DB::query($query)->value();
|
||||||
$this->assertEquals($result, date('Y-m-d H:i:s', strtotime(Dataobject::get_one('SiteTree',"\"URLSegment\" = 'home'")->Created) - 900), '15 Minutes before creating SiteTree[home]');
|
$this->matchesRoughly($result, date('Y-m-d H:i:s', strtotime(Dataobject::get_one('SiteTree',"\"URLSegment\" = 'home'")->Created) - 900), '15 Minutes before creating SiteTree[home]');
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,21 +75,21 @@ class DbDatetimeTest extends FunctionalTest {
|
|||||||
|
|
||||||
$query = 'SELECT ' . $this->adapter->datetimeDifferenceClause('1974-10-14 10:30:00', '1973-10-14 10:30:00');
|
$query = 'SELECT ' . $this->adapter->datetimeDifferenceClause('1974-10-14 10:30:00', '1973-10-14 10:30:00');
|
||||||
$result = DB::query($query)->value();
|
$result = DB::query($query)->value();
|
||||||
$this->assertEquals($result/86400, 365, '1974 - 1973 = 365 * 86400 sec');
|
$this->matchesRoughly($result/86400, 365, '1974 - 1973 = 365 * 86400 sec');
|
||||||
|
|
||||||
$query = 'SELECT ' . $this->adapter->datetimeDifferenceClause(date('Y-m-d H:i:s', strtotime('-15 seconds')), 'now');
|
$query = 'SELECT ' . $this->adapter->datetimeDifferenceClause(date('Y-m-d H:i:s', strtotime('-15 seconds')), 'now');
|
||||||
$result = DB::query($query)->value();
|
$result = DB::query($query)->value();
|
||||||
$this->assertEquals($result, -15, '15 seconds ago - now');
|
$this->matchesRoughly($result, -15, '15 seconds ago - now');
|
||||||
|
|
||||||
$query = 'SELECT ' . $this->adapter->datetimeDifferenceClause('now', $this->adapter->datetimeIntervalClause('now', '+45 Minutes'));
|
$query = 'SELECT ' . $this->adapter->datetimeDifferenceClause('now', $this->adapter->datetimeIntervalClause('now', '+45 Minutes'));
|
||||||
$result = DB::query($query)->value();
|
$result = DB::query($query)->value();
|
||||||
$this->assertEquals($result, -45 * 60, 'now - 45 minutes ahead');
|
$this->matchesRoughly($result, -45 * 60, 'now - 45 minutes ahead');
|
||||||
|
|
||||||
$query = 'SELECT ' . $this->adapter->datetimeDifferenceClause('"LastEdited"', '"Created"') . ' AS "test" FROM "SiteTree" WHERE "URLSegment" = \'home\'';
|
$query = 'SELECT ' . $this->adapter->datetimeDifferenceClause('"LastEdited"', '"Created"') . ' AS "test" FROM "SiteTree" WHERE "URLSegment" = \'home\'';
|
||||||
$result = DB::query($query)->value();
|
$result = DB::query($query)->value();
|
||||||
$lastedited = Dataobject::get_one('SiteTree',"\"URLSegment\" = 'home'")->LastEdited;
|
$lastedited = Dataobject::get_one('SiteTree',"\"URLSegment\" = 'home'")->LastEdited;
|
||||||
$created = Dataobject::get_one('SiteTree',"\"URLSegment\" = 'home'")->Created;
|
$created = Dataobject::get_one('SiteTree',"\"URLSegment\" = 'home'")->Created;
|
||||||
$this->assertEquals($result, strtotime($lastedited) - strtotime($created), 'age of HomePage record in seconds since unix epoc');
|
$this->matchesRoughly($result, strtotime($lastedited) - strtotime($created), 'age of HomePage record in seconds since unix epoc');
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
4
tests/DbDatetimeTest.yml
Normal file
4
tests/DbDatetimeTest.yml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
Page:
|
||||||
|
first:
|
||||||
|
Title: Home page
|
||||||
|
URLSegment: home
|
Loading…
Reference in New Issue
Block a user