. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Services; use Fisharebest\Webtrees\TestCase; use Mockery; /** * Mock function. * * @param mixed ...$args * * @return mixed */ function ini_get(...$args) { return TimeoutServiceTest::$mock_functions->ini_get(...$args); } /** * Mock function. * * @param mixed ...$args * * @return mixed */ function microtime(...$args) { return TimeoutServiceTest::$mock_functions->microtime(...$args); } /** * Test harness for the class TimeoutServiceTest */ class TimeoutServiceTest extends TestCase { /** @var object */ public static $mock_functions; /** * Initialize the test script * * @return void */ protected function setUp() { parent::setUp(); self::$mock_functions = Mockery::mock(); } /** * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct() * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp() * * @return void */ public function testNoTimeOut() { $now = \microtime(true); $timeout_service = new TimeoutService($now); self::$mock_functions ->shouldReceive('ini_get') ->with('max_execution_time') ->once() ->andReturn('0'); $this->assertFalse($timeout_service->isTimeNearlyUp()); } /** * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct() * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp() * * @return void */ public function testTimeOutReached() { $now = \microtime(true); $timeout_service = new TimeoutService($now); self::$mock_functions ->shouldReceive('ini_get') ->with('max_execution_time') ->once() ->andReturn('30'); self::$mock_functions ->shouldReceive('microtime') ->with('true') ->once() ->andReturn($now + 60.0); $this->assertTrue($timeout_service->isTimeNearlyUp()); } /** * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct() * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp() * * @return void */ public function testTimeOutNotReached() { $now = \microtime(true); $timeout_service = new TimeoutService($now); self::$mock_functions ->shouldReceive('ini_get') ->with('max_execution_time') ->once() ->andReturn('30'); self::$mock_functions ->shouldReceive('microtime') ->with('true') ->once() ->andReturn($now + 10.0); $this->assertFalse($timeout_service->isTimeNearlyUp()); } /** * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct() * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeLimitUp() * * @return void */ public function testTimeLimitNotReached() { $now = \microtime(true); $timeout_service = new TimeoutService($now); self::$mock_functions ->shouldReceive('microtime') ->with('true') ->once() ->andReturn($now + 1.4); $this->assertFalse($timeout_service->isTimeLimitUp()); } /** * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct() * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeLimitUp() * * @return void */ public function testTimeLimitReached() { $now = \microtime(true); $timeout_service = new TimeoutService($now); self::$mock_functions ->shouldReceive('microtime') ->with('true') ->once() ->andReturn($now + 1.6); $this->assertTrue($timeout_service->isTimeLimitUp()); } }