Permalink
Browse files

MINOR Merged r113933,113936-113937 from branches/2.4

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@114001 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information...
halkyon committed Nov 22, 2010
1 parent 73c6f1f commit 9ac991636e1eeace39f46a01ba34e8ddf2adadd9
Showing with 25 additions and 2 deletions.
  1. +25 −2 tests/model/DbDatetimeTest.php
@@ -1,26 +1,49 @@
<?php
class DbDatetimeTest extends FunctionalTest {
+
static $fixture_file = 'sapphire/tests/model/DbDatetimeTest.yml';
+
+ 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,
+ );
/**
* 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 = 15; // seconds
+ $allowedDifference = 5 + abs(self::$offset); // seconds
$time1 = is_numeric($date1) ? $date1 : strtotime($date1);
$time2 = is_numeric($date2) ? $date2 : strtotime($date2);
- $this->assertTrue(abs($time1-$time2)<$allowedDifference, $comment . " ($date1, $date2)");
+ $this->assertTrue(abs($time1-$time2)<$allowedDifference, $comment . " (times differ by " . abs($time1-$time2) . " seconds)");
}
private function getDbNow() {
$query = 'SELECT ' . $this->adapter->formattedDatetimeClause('now', '%U');
return DB::query($query)->value();
}
+ 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;
+ }
+ }
+ }
+
function setUp() {
parent::setUp();
$this->adapter = DB::getConn();

0 comments on commit 9ac9916

Please sign in to comment.