1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>. 16 */ 17declare(strict_types=1); 18 19namespace Fisharebest\Webtrees\Services; 20 21use Fisharebest\Webtrees\MockGlobalFunctions; 22use Fisharebest\Webtrees\TestCase; 23 24/** 25 * Mock function. 26 * 27 * @param mixed ...$args 28 * 29 * @return mixed 30 */ 31function ini_get(...$args) 32{ 33 if (TestCase::$mock_functions === null) { 34 return \ini_get(...$args); 35 } 36 37 return TestCase::$mock_functions->iniGet(...$args); 38} 39 40/** 41 * Mock function. 42 * 43 * @param mixed ...$args 44 * 45 * @return mixed 46 */ 47function microtime(...$args) 48{ 49 if (TestCase::$mock_functions === null) { 50 return \microtime(...$args); 51 } 52 53 return TestCase::$mock_functions->microtime(...$args); 54} 55 56/** 57 * Test harness for the class TimeoutService 58 */ 59class TimeoutServiceTest extends TestCase 60{ 61 /** 62 * @return void 63 */ 64 protected function setUp(): void 65 { 66 parent::setUp(); 67 68 self::$mock_functions = $this->getMockForAbstractClass(MockGlobalFunctions::class); 69 } 70 71 /** 72 * @return void 73 */ 74 protected function tearDown(): void 75 { 76 parent::setUp(); 77 78 self::$mock_functions = null; 79 } 80 81 /** 82 * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct 83 * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp 84 * 85 * @return void 86 */ 87 public function testNoTimeOut(): void 88 { 89 $now = 1500000000.0; 90 91 $timeout_service = new TimeoutService($now); 92 93 self::$mock_functions 94 ->method('iniGet') 95 ->with('max_execution_time') 96 ->willReturn('0'); 97 98 $this->assertFalse($timeout_service->isTimeNearlyUp()); 99 } 100 101 /** 102 * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct 103 * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp 104 * 105 * @return void 106 */ 107 public function testTimeOutReached(): void 108 { 109 $now = 1500000000.0; 110 111 $timeout_service = new TimeoutService($now); 112 113 self::$mock_functions 114 ->method('iniGet') 115 ->with('max_execution_time') 116 ->willReturn('30'); 117 118 self::$mock_functions 119 ->method('microtime') 120 ->with(true) 121 ->willReturn($now + 60.0); 122 123 $this->assertTrue($timeout_service->isTimeNearlyUp()); 124 } 125 126 /** 127 * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct 128 * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp 129 * 130 * @return void 131 */ 132 public function testTimeOutNotReached(): void 133 { 134 $now = \microtime(true); 135 136 $timeout_service = new TimeoutService($now); 137 138 self::$mock_functions 139 ->method('iniGet') 140 ->with('max_execution_time') 141 ->willReturn('30'); 142 143 self::$mock_functions 144 ->method('microtime') 145 ->with(true) 146 ->willReturn($now + 10.0); 147 148 $this->assertFalse($timeout_service->isTimeNearlyUp()); 149 } 150 151 /** 152 * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct 153 * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeLimitUp 154 * 155 * @return void 156 */ 157 public function testTimeLimitNotReached(): void 158 { 159 $now = \microtime(true); 160 161 $timeout_service = new TimeoutService($now); 162 163 self::$mock_functions 164 ->method('microtime') 165 ->with(true) 166 ->willReturn($now + 1.4); 167 168 $this->assertFalse($timeout_service->isTimeLimitUp()); 169 } 170 171 /** 172 * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct 173 * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeLimitUp 174 * 175 * @return void 176 */ 177 public function testTimeLimitReached(): void 178 { 179 $now = \microtime(true); 180 181 $timeout_service = new TimeoutService($now); 182 183 self::$mock_functions 184 ->method('microtime') 185 ->with(true) 186 ->willReturn($now + 1.6); 187 188 $this->assertTrue($timeout_service->isTimeLimitUp()); 189 } 190} 191