. */ 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; /** * Mock function. * * @param mixed ...$args * * @return mixed */ function ini_get(...$args) { if (TestCase::$mock_functions === null) { return \ini_get(...$args); } return TestCase::$mock_functions->iniGet(...$args); } /** * Test harness for the class TimeoutService */ class TimeoutServiceTest extends TestCase { /** * @return void */ protected function setUp(): void { parent::setUp(); self::$mock_functions = $this->getMockForAbstractClass(MockGlobalFunctions::class); } /** * @return void */ protected function tearDown(): void { parent::setUp(); self::$mock_functions = null; } /** * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp * * @return void */ public function testNoTimeOut(): void { $now = 1500000000.0; $timeout_service = new TimeoutService($now); self::$mock_functions ->method('iniGet') ->with('max_execution_time') ->willReturn('0'); self::assertFalse($timeout_service->isTimeNearlyUp()); } /** * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp * * @return void */ public function testTimeOutReached(): void { $now = 1500000000.0; $timeout_service = new TimeoutService($now); self::$mock_functions ->method('iniGet') ->with('max_execution_time') ->willReturn('30'); $time_factory = $this->createMock(TimeFactoryInterface::class); $time_factory->method('now')->willReturn($now + 60.0); Registry::timeFactory($time_factory); self::assertTrue($timeout_service->isTimeNearlyUp()); } /** * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp * * @return void */ public function testTimeOutNotReached(): void { $now = Registry::timeFactory()->now(); $timeout_service = new TimeoutService($now); self::$mock_functions ->method('iniGet') ->with('max_execution_time') ->willReturn('30'); $time_factory = $this->createMock(TimeFactoryInterface::class); $time_factory->method('now')->willReturn($now + 10.0); Registry::timeFactory($time_factory); self::assertFalse($timeout_service->isTimeNearlyUp()); } /** * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeLimitUp * * @return void */ public function testTimeLimitNotReached(): void { $now = Registry::timeFactory()->now(); $timeout_service = new TimeoutService($now); $time_factory = $this->createMock(TimeFactoryInterface::class); $time_factory->method('now')->willReturn($now + 1.4); Registry::timeFactory($time_factory); self::assertFalse($timeout_service->isTimeLimitUp()); } /** * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeLimitUp * * @return void */ public function testTimeLimitReached(): void { $now = Registry::timeFactory()->now(); $timeout_service = new TimeoutService($now); $time_factory = $this->createMock(TimeFactoryInterface::class); $time_factory->method('now')->willReturn($now + 1.6); Registry::timeFactory($time_factory); self::assertTrue($timeout_service->isTimeLimitUp()); } }