summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGreg Roach <greg@subaqua.co.uk>2022-06-02 14:51:06 +0100
committerGreg Roach <greg@subaqua.co.uk>2022-06-02 14:51:06 +0100
commiteeec557a973754cac923d5bfe748a55077cdabba (patch)
tree2367ac1f8de7c895c138065a9141b24dc9000d64 /tests
parent2e464181f49de4b07e9c43f180e054c9d1ee7e4a (diff)
downloadwebtrees-eeec557a973754cac923d5bfe748a55077cdabba.tar.gz
webtrees-eeec557a973754cac923d5bfe748a55077cdabba.tar.bz2
webtrees-eeec557a973754cac923d5bfe748a55077cdabba.zip
Use a factory to make the current time
Diffstat (limited to 'tests')
-rw-r--r--tests/MockGlobalFunctions.php9
-rw-r--r--tests/app/Services/TimeoutServiceTest.php52
2 files changed, 17 insertions, 44 deletions
diff --git a/tests/MockGlobalFunctions.php b/tests/MockGlobalFunctions.php
index 4036daa4c7..fd4471990b 100644
--- a/tests/MockGlobalFunctions.php
+++ b/tests/MockGlobalFunctions.php
@@ -25,15 +25,6 @@ namespace Fisharebest\Webtrees;
abstract class MockGlobalFunctions
{
/**
- * Mock version of microtime()
- *
- * @param bool $get_as_float
- *
- * @return float|array<int>
- */
- abstract public function microtime(bool $get_as_float);
-
- /**
* Mock version of ini_get()
*
* @param string $varname
diff --git a/tests/app/Services/TimeoutServiceTest.php b/tests/app/Services/TimeoutServiceTest.php
index 8bb733b215..226923dfe8 100644
--- a/tests/app/Services/TimeoutServiceTest.php
+++ b/tests/app/Services/TimeoutServiceTest.php
@@ -19,7 +19,9 @@ declare(strict_types=1);
namespace Fisharebest\Webtrees\Services;
+use Fisharebest\Webtrees\Contracts\TimeFactoryInterface;
use Fisharebest\Webtrees\MockGlobalFunctions;
+use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\TestCase;
/**
@@ -39,22 +41,6 @@ function ini_get(...$args)
}
/**
- * Mock function.
- *
- * @param mixed ...$args
- *
- * @return mixed
- */
-function microtime(...$args)
-{
- if (TestCase::$mock_functions === null) {
- return \microtime(...$args);
- }
-
- return TestCase::$mock_functions->microtime(...$args);
-}
-
-/**
* Test harness for the class TimeoutService
*/
class TimeoutServiceTest extends TestCase
@@ -116,10 +102,9 @@ class TimeoutServiceTest extends TestCase
->with('max_execution_time')
->willReturn('30');
- self::$mock_functions
- ->method('microtime')
- ->with(true)
- ->willReturn($now + 60.0);
+ $time_factory = $this->createMock(TimeFactoryInterface::class);
+ $time_factory->method('now')->willReturn($now + 60.0);
+ Registry::timeFactory($time_factory);
self::assertTrue($timeout_service->isTimeNearlyUp());
}
@@ -132,7 +117,7 @@ class TimeoutServiceTest extends TestCase
*/
public function testTimeOutNotReached(): void
{
- $now = \microtime(true);
+ $now = Registry::timeFactory()->now();
$timeout_service = new TimeoutService($now);
@@ -141,10 +126,9 @@ class TimeoutServiceTest extends TestCase
->with('max_execution_time')
->willReturn('30');
- self::$mock_functions
- ->method('microtime')
- ->with(true)
- ->willReturn($now + 10.0);
+ $time_factory = $this->createMock(TimeFactoryInterface::class);
+ $time_factory->method('now')->willReturn($now + 10.0);
+ Registry::timeFactory($time_factory);
self::assertFalse($timeout_service->isTimeNearlyUp());
}
@@ -157,14 +141,13 @@ class TimeoutServiceTest extends TestCase
*/
public function testTimeLimitNotReached(): void
{
- $now = \microtime(true);
+ $now = Registry::timeFactory()->now();
$timeout_service = new TimeoutService($now);
- self::$mock_functions
- ->method('microtime')
- ->with(true)
- ->willReturn($now + 1.4);
+ $time_factory = $this->createMock(TimeFactoryInterface::class);
+ $time_factory->method('now')->willReturn($now + 1.4);
+ Registry::timeFactory($time_factory);
self::assertFalse($timeout_service->isTimeLimitUp());
}
@@ -177,14 +160,13 @@ class TimeoutServiceTest extends TestCase
*/
public function testTimeLimitReached(): void
{
- $now = \microtime(true);
+ $now = Registry::timeFactory()->now();
$timeout_service = new TimeoutService($now);
- self::$mock_functions
- ->method('microtime')
- ->with(true)
- ->willReturn($now + 1.6);
+ $time_factory = $this->createMock(TimeFactoryInterface::class);
+ $time_factory->method('now')->willReturn($now + 1.6);
+ Registry::timeFactory($time_factory);
self::assertTrue($timeout_service->isTimeLimitUp());
}