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