2010-10-15 03:28:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class DbDatetimeTest extends FunctionalTest {
|
2010-11-22 03:02:11 +01:00
|
|
|
|
2011-03-30 08:49:11 +02:00
|
|
|
static $fixture_file = 'DbDatetimeTest.yml';
|
2011-03-23 04:32:24 +01:00
|
|
|
|
|
|
|
protected $extraDataObjects = array('DbDatetimeTest_Team');
|
2010-11-22 03:02:11 +01:00
|
|
|
|
|
|
|
private static $offset = 0; // number of seconds of php and db time are out of sync
|
|
|
|
private static $offset_thresholds = array( // throw an error if the offset exceeds 30 minutes
|
|
|
|
E_USER_ERROR => 1800,
|
|
|
|
E_USER_NOTICE => 5,
|
|
|
|
);
|
2012-03-11 19:49:49 +01:00
|
|
|
|
|
|
|
private $adapter;
|
2010-10-15 03:28:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = '') {
|
2010-11-22 03:02:11 +01:00
|
|
|
$allowedDifference = 5 + abs(self::$offset); // seconds
|
2010-10-15 03:28:43 +02:00
|
|
|
|
|
|
|
$time1 = is_numeric($date1) ? $date1 : strtotime($date1);
|
|
|
|
$time2 = is_numeric($date2) ? $date2 : strtotime($date2);
|
|
|
|
|
2010-11-22 03:02:11 +01:00
|
|
|
$this->assertTrue(abs($time1-$time2)<$allowedDifference, $comment . " (times differ by " . abs($time1-$time2) . " seconds)");
|
2010-10-15 03:28:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getDbNow() {
|
|
|
|
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('now', '%U');
|
|
|
|
return DB::query($query)->value();
|
|
|
|
}
|
|
|
|
|
2010-11-22 03:02:11 +01:00
|
|
|
function setUpOnce() {
|
|
|
|
parent::setUpOnce();
|
|
|
|
|
|
|
|
self::$offset = time() - strtotime(DB::query('SELECT ' . DB::getConn()->now())->value());
|
|
|
|
foreach(self::$offset_thresholds as $code => $offset) {
|
|
|
|
if(abs(self::$offset) > $offset) {
|
|
|
|
if($code == E_USER_NOTICE) {
|
|
|
|
Debug::show('The time of the database is out of sync with the webserver by ' . abs(self::$offset) . ' seconds.');
|
|
|
|
} else {
|
|
|
|
trigger_error('The time of the database is out of sync with the webserver by ' . abs(self::$offset) . ' seconds.', $code);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-15 03:28:43 +02:00
|
|
|
function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
$this->adapter = DB::getConn();
|
|
|
|
$this->supportDbDatetime = method_exists($this->adapter, 'datetimeIntervalClause');
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCorrectNow() {
|
|
|
|
if($this->supportDbDatetime) {
|
2011-10-07 12:38:21 +02:00
|
|
|
$clause = $this->adapter->formattedDatetimeClause('now', '%U');
|
|
|
|
$result = DB::query('SELECT ' . $clause)->value();
|
2010-10-15 03:28:43 +02:00
|
|
|
$this->assertRegExp('/^\d*$/', (string) $result);
|
|
|
|
$this->assertTrue($result>0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDbDatetimeFormat() {
|
|
|
|
if($this->supportDbDatetime) {
|
2011-10-07 12:38:21 +02:00
|
|
|
$clause = $this->adapter->formattedDatetimeClause('1973-10-14 10:30:00', '%H:%i, %d/%m/%Y');
|
|
|
|
$result = DB::query('SELECT ' . $clause)->value();
|
2010-10-15 03:28:43 +02:00
|
|
|
$this->matchesRoughly($result, date('H:i, d/m/Y', strtotime('1973-10-14 10:30:00')), 'nice literal time');
|
|
|
|
|
2011-10-07 12:38:21 +02:00
|
|
|
$clause = $this->adapter->formattedDatetimeClause('now', '%d');
|
|
|
|
$result = DB::query('SELECT ' . $clause)->value();
|
2010-10-15 03:28:43 +02:00
|
|
|
$this->matchesRoughly($result, date('d', $this->getDbNow()), 'todays day');
|
|
|
|
|
2011-10-07 12:38:21 +02:00
|
|
|
$clause = $this->adapter->formattedDatetimeClause('"Created"', '%U') . ' AS test FROM "DbDateTimeTest_Team"';
|
|
|
|
$result = DB::query('SELECT ' . $clause)->value();
|
2011-03-23 04:32:24 +01:00
|
|
|
$this->matchesRoughly($result, strtotime(DataObject::get_one('DbDateTimeTest_Team')->Created), 'fixture ->Created as timestamp');
|
2010-10-15 03:28:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDbDatetimeInterval() {
|
|
|
|
if($this->supportDbDatetime) {
|
|
|
|
|
2011-10-07 12:38:21 +02:00
|
|
|
$clause = $this->adapter->datetimeIntervalClause('1973-10-14 10:30:00', '+18 Years');
|
|
|
|
$result = DB::query('SELECT ' . $clause)->value();
|
2010-10-15 03:28:43 +02:00
|
|
|
$this->matchesRoughly($result, '1991-10-14 10:30:00', 'add 18 years');
|
|
|
|
|
2011-10-07 12:38:21 +02:00
|
|
|
$clause = $this->adapter->datetimeIntervalClause('now', '+1 Day');
|
|
|
|
$result = DB::query('SELECT ' . $clause)->value();
|
2010-10-15 03:28:43 +02:00
|
|
|
$this->matchesRoughly($result, date('Y-m-d H:i:s', strtotime('+1 Day', $this->getDbNow())), 'tomorrow');
|
|
|
|
|
2011-05-19 01:38:43 +02:00
|
|
|
$query = new SQLQuery();
|
|
|
|
$query->select($this->adapter->datetimeIntervalClause('"Created"', '-15 Minutes') . ' AS "test"')
|
2011-05-30 22:18:50 +02:00
|
|
|
->from('"DbDateTimeTest_Team"')
|
2011-05-19 01:38:43 +02:00
|
|
|
->limit(1);
|
|
|
|
$result = $query->execute()->value();
|
2011-03-23 04:32:24 +01:00
|
|
|
$this->matchesRoughly($result, date('Y-m-d H:i:s', strtotime(Dataobject::get_one('DbDateTimeTest_Team')->Created) - 900), '15 Minutes before creating fixture');
|
2010-10-15 03:28:43 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDbDatetimeDifference() {
|
|
|
|
if($this->supportDbDatetime) {
|
|
|
|
|
2011-10-07 12:38:21 +02:00
|
|
|
$clause = $this->adapter->datetimeDifferenceClause('1974-10-14 10:30:00', '1973-10-14 10:30:00');
|
|
|
|
$result = DB::query('SELECT ' . $clause)->value();
|
2010-10-15 03:28:43 +02:00
|
|
|
$this->matchesRoughly($result/86400, 365, '1974 - 1973 = 365 * 86400 sec');
|
|
|
|
|
2011-10-07 12:38:21 +02:00
|
|
|
$clause = $this->adapter->datetimeDifferenceClause(date('Y-m-d H:i:s', strtotime('-15 seconds')), 'now');
|
|
|
|
$result = DB::query('SELECT ' . $clause)->value();
|
2010-10-15 03:28:43 +02:00
|
|
|
$this->matchesRoughly($result, -15, '15 seconds ago - now');
|
|
|
|
|
2011-10-07 12:38:21 +02:00
|
|
|
$clause = $this->adapter->datetimeDifferenceClause('now', $this->adapter->datetimeIntervalClause('now', '+45 Minutes'));
|
|
|
|
$result = DB::query('SELECT ' . $clause)->value();
|
2010-10-15 03:28:43 +02:00
|
|
|
$this->matchesRoughly($result, -45 * 60, 'now - 45 minutes ahead');
|
|
|
|
|
2011-05-19 01:38:43 +02:00
|
|
|
$query = new SQLQuery();
|
|
|
|
$query->select($this->adapter->datetimeDifferenceClause('"LastEdited"', '"Created"') . ' AS "test"')
|
2011-05-30 22:18:50 +02:00
|
|
|
->from('"DbDateTimeTest_Team"')
|
2011-05-19 01:38:43 +02:00
|
|
|
->limit(1);
|
|
|
|
|
|
|
|
$result = $query->execute()->value();
|
2011-03-23 04:32:24 +01:00
|
|
|
$lastedited = Dataobject::get_one('DbDateTimeTest_Team')->LastEdited;
|
|
|
|
$created = Dataobject::get_one('DbDateTimeTest_Team')->Created;
|
2010-10-15 03:28:43 +02:00
|
|
|
$this->matchesRoughly($result, strtotime($lastedited) - strtotime($created), 'age of HomePage record in seconds since unix epoc');
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2011-03-23 04:32:24 +01:00
|
|
|
|
|
|
|
class DbDateTimeTest_Team extends DataObject implements TestOnly {
|
|
|
|
static $db = array(
|
|
|
|
'Title' => 'Varchar'
|
|
|
|
);
|
|
|
|
}
|