diff --git a/src/Dev/SapphireTest.php b/src/Dev/SapphireTest.php index 7af35ecc5..0bde7d7c8 100644 --- a/src/Dev/SapphireTest.php +++ b/src/Dev/SapphireTest.php @@ -1359,6 +1359,22 @@ if (class_exists(IsEqualCanonicalizing::class)) { $this->getName(false) ); } + + /** + * Test safe version of sleep() + * + * @param int $seconds + * @return DBDatetime + * @throws Exception + */ + protected function mockSleep(int $seconds): DBDatetime + { + $now = DBDatetime::now(); + $now->modify(sprintf('+ %d seconds', $seconds)); + DBDatetime::set_mock_now($now); + + return $now; + } } } @@ -2655,4 +2671,20 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly } return $rules; } + + /** + * Test safe version of sleep() + * + * @param int $seconds + * @return DBDatetime + * @throws Exception + */ + protected function mockSleep(int $seconds): DBDatetime + { + $now = DBDatetime::now(); + $now->modify(sprintf('+ %d seconds', $seconds)); + DBDatetime::set_mock_now($now); + + return $now; + } } diff --git a/tests/php/ORM/DBDatetimeTest.php b/tests/php/ORM/DBDatetimeTest.php index 6abf14d4e..71b24be22 100644 --- a/tests/php/ORM/DBDatetimeTest.php +++ b/tests/php/ORM/DBDatetimeTest.php @@ -58,6 +58,25 @@ class DBDatetimeTest extends SapphireTest }); } + public function testMockSleep() + { + DBDatetime::set_mock_now('2010-01-01 10:00:00'); + + $this->mockSleep(1); + $this->assertEquals( + '2010-01-01 10:00:01', + DBDatetime::now()->Rfc2822(), + 'We expect the time to move forward by 1 second' + ); + + $this->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() { $date = DBDatetime::create_field('Datetime', '');