PR fixes.

This commit is contained in:
Mojmir Fendek 2022-05-19 14:09:04 +12:00
parent 78d20f6fe5
commit a9a6b0f114
3 changed files with 34 additions and 18 deletions

View File

@ -1359,6 +1359,22 @@ if (class_exists(IsEqualCanonicalizing::class)) {
$this->getName(false) $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; 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;
}
} }

View File

@ -247,22 +247,6 @@ 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
* *

View File

@ -62,14 +62,14 @@ class DBDatetimeTest extends SapphireTest
{ {
DBDatetime::set_mock_now('2010-01-01 10:00:00'); DBDatetime::set_mock_now('2010-01-01 10:00:00');
DBDatetime::mockSleep(1); $this->mockSleep(1);
$this->assertEquals( $this->assertEquals(
'2010-01-01 10:00:01', '2010-01-01 10:00:01',
DBDatetime::now()->Rfc2822(), DBDatetime::now()->Rfc2822(),
'We expect the time to move forward by 1 second' 'We expect the time to move forward by 1 second'
); );
DBDatetime::mockSleep(10); $this->mockSleep(10);
$this->assertEquals( $this->assertEquals(
'2010-01-01 10:00:11', '2010-01-01 10:00:11',
DBDatetime::now()->Rfc2822(), DBDatetime::now()->Rfc2822(),