From 78d20f6fe563aaa419f418490bcef88b3f262634 Mon Sep 17 00:00:00 2001 From: Mojmir Fendek Date: Fri, 13 May 2022 13:15:24 +1200 Subject: [PATCH 1/2] NEW: Mock sleep unit test utility. --- src/ORM/FieldType/DBDatetime.php | 16 ++++++++++++++++ tests/php/ORM/DBDatetimeTest.php | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/ORM/FieldType/DBDatetime.php b/src/ORM/FieldType/DBDatetime.php index 8c120f246..17235d49e 100644 --- a/src/ORM/FieldType/DBDatetime.php +++ b/src/ORM/FieldType/DBDatetime.php @@ -247,6 +247,22 @@ class DBDatetime extends DBDate implements TemplateGlobalProvider 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 * diff --git a/tests/php/ORM/DBDatetimeTest.php b/tests/php/ORM/DBDatetimeTest.php index 6abf14d4e..b122e48cf 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'); + + 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() { $date = DBDatetime::create_field('Datetime', ''); From a9a6b0f114b73b407ed16d191c70e22d359c73f8 Mon Sep 17 00:00:00 2001 From: Mojmir Fendek Date: Thu, 19 May 2022 14:09:04 +1200 Subject: [PATCH 2/2] PR fixes. --- src/Dev/SapphireTest.php | 32 ++++++++++++++++++++++++++++++++ src/ORM/FieldType/DBDatetime.php | 16 ---------------- tests/php/ORM/DBDatetimeTest.php | 4 ++-- 3 files changed, 34 insertions(+), 18 deletions(-) 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/src/ORM/FieldType/DBDatetime.php b/src/ORM/FieldType/DBDatetime.php index 17235d49e..8c120f246 100644 --- a/src/ORM/FieldType/DBDatetime.php +++ b/src/ORM/FieldType/DBDatetime.php @@ -247,22 +247,6 @@ class DBDatetime extends DBDate implements TemplateGlobalProvider 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 * diff --git a/tests/php/ORM/DBDatetimeTest.php b/tests/php/ORM/DBDatetimeTest.php index b122e48cf..71b24be22 100644 --- a/tests/php/ORM/DBDatetimeTest.php +++ b/tests/php/ORM/DBDatetimeTest.php @@ -62,14 +62,14 @@ class DBDatetimeTest extends SapphireTest { DBDatetime::set_mock_now('2010-01-01 10:00:00'); - DBDatetime::mockSleep(1); + $this->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->mockSleep(10); $this->assertEquals( '2010-01-01 10:00:11', DBDatetime::now()->Rfc2822(),