1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Services; 21 22use Fisharebest\Webtrees\Contracts\TimeFactoryInterface; 23use Fisharebest\Webtrees\MockGlobalFunctions; 24use Fisharebest\Webtrees\Registry; 25use Fisharebest\Webtrees\TestCase; 26 27/** 28 * Mock function. 29 * 30 * @param mixed ...$args 31 * 32 * @return mixed 33 */ 34function ini_get(...$args) 35{ 36 if (TestCase::$mock_functions === null) { 37 return \ini_get(...$args); 38 } 39 40 return TestCase::$mock_functions->iniGet(...$args); 41} 42 43/** 44 * Test harness for the class TimeoutService 45 */ 46class TimeoutServiceTest extends TestCase 47{ 48 /** 49 * @return void 50 */ 51 protected function setUp(): void 52 { 53 parent::setUp(); 54 55 self::$mock_functions = $this->getMockForAbstractClass(MockGlobalFunctions::class); 56 } 57 58 /** 59 * @return void 60 */ 61 protected function tearDown(): void 62 { 63 parent::setUp(); 64 65 self::$mock_functions = null; 66 } 67 68 /** 69 * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct 70 * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp 71 * 72 * @return void 73 */ 74 public function testNoTimeOut(): void 75 { 76 $now = 1500000000.0; 77 78 $timeout_service = new TimeoutService($now); 79 80 self::$mock_functions 81 ->method('iniGet') 82 ->with('max_execution_time') 83 ->willReturn('0'); 84 85 self::assertFalse($timeout_service->isTimeNearlyUp()); 86 } 87 88 /** 89 * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct 90 * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp 91 * 92 * @return void 93 */ 94 public function testTimeOutReached(): void 95 { 96 $now = 1500000000.0; 97 98 $timeout_service = new TimeoutService($now); 99 100 self::$mock_functions 101 ->method('iniGet') 102 ->with('max_execution_time') 103 ->willReturn('30'); 104 105 $time_factory = $this->createMock(TimeFactoryInterface::class); 106 $time_factory->method('now')->willReturn($now + 60.0); 107 Registry::timeFactory($time_factory); 108 109 self::assertTrue($timeout_service->isTimeNearlyUp()); 110 } 111 112 /** 113 * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct 114 * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp 115 * 116 * @return void 117 */ 118 public function testTimeOutNotReached(): void 119 { 120 $now = Registry::timeFactory()->now(); 121 122 $timeout_service = new TimeoutService($now); 123 124 self::$mock_functions 125 ->method('iniGet') 126 ->with('max_execution_time') 127 ->willReturn('30'); 128 129 $time_factory = $this->createMock(TimeFactoryInterface::class); 130 $time_factory->method('now')->willReturn($now + 10.0); 131 Registry::timeFactory($time_factory); 132 133 self::assertFalse($timeout_service->isTimeNearlyUp()); 134 } 135 136 /** 137 * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct 138 * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeLimitUp 139 * 140 * @return void 141 */ 142 public function testTimeLimitNotReached(): void 143 { 144 $now = Registry::timeFactory()->now(); 145 146 $timeout_service = new TimeoutService($now); 147 148 $time_factory = $this->createMock(TimeFactoryInterface::class); 149 $time_factory->method('now')->willReturn($now + 1.4); 150 Registry::timeFactory($time_factory); 151 152 self::assertFalse($timeout_service->isTimeLimitUp()); 153 } 154 155 /** 156 * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct 157 * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeLimitUp 158 * 159 * @return void 160 */ 161 public function testTimeLimitReached(): void 162 { 163 $now = Registry::timeFactory()->now(); 164 165 $timeout_service = new TimeoutService($now); 166 167 $time_factory = $this->createMock(TimeFactoryInterface::class); 168 $time_factory->method('now')->willReturn($now + 1.6); 169 Registry::timeFactory($time_factory); 170 171 self::assertTrue($timeout_service->isTimeLimitUp()); 172 } 173} 174