mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
NEW: Mock sleep unit test utility.
This commit is contained in:
parent
995e5dc71d
commit
78d20f6fe5
@ -247,6 +247,22 @@ class DBDatetime extends DBDate implements TemplateGlobalProvider
|
|||||||
self::$mock_now = null;
|
self::$mock_now = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test safe version of sleep()
|
||||||
|
*
|
||||||
|
* @param int $seconds
|
||||||
|
* @return DBDatetime
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static function mockSleep(int $seconds): DBDatetime
|
||||||
|
{
|
||||||
|
$now = DBDatetime::now();
|
||||||
|
$now->modify(sprintf('+ %d seconds', $seconds));
|
||||||
|
DBDatetime::set_mock_now($now);
|
||||||
|
|
||||||
|
return $now;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run a callback with specific time, original mock value is retained after callback
|
* Run a callback with specific time, original mock value is retained after callback
|
||||||
*
|
*
|
||||||
|
@ -58,6 +58,25 @@ class DBDatetimeTest extends SapphireTest
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testMockSleep()
|
||||||
|
{
|
||||||
|
DBDatetime::set_mock_now('2010-01-01 10:00:00');
|
||||||
|
|
||||||
|
DBDatetime::mockSleep(1);
|
||||||
|
$this->assertEquals(
|
||||||
|
'2010-01-01 10:00:01',
|
||||||
|
DBDatetime::now()->Rfc2822(),
|
||||||
|
'We expect the time to move forward by 1 second'
|
||||||
|
);
|
||||||
|
|
||||||
|
DBDatetime::mockSleep(10);
|
||||||
|
$this->assertEquals(
|
||||||
|
'2010-01-01 10:00:11',
|
||||||
|
DBDatetime::now()->Rfc2822(),
|
||||||
|
'We expect the time to move forward by 10 seconds'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testSetNullAndZeroValues()
|
public function testSetNullAndZeroValues()
|
||||||
{
|
{
|
||||||
$date = DBDatetime::create_field('Datetime', '');
|
$date = DBDatetime::create_field('Datetime', '');
|
||||||
|
Loading…
Reference in New Issue
Block a user