1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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; 26use PHPUnit\Framework\Attributes\CoversClass; 27 28/** 29 * Mock function. 30 * 31 * @param mixed ...$args 32 * 33 * @return mixed 34 */ 35function ini_get(...$args) 36{ 37 if (TestCase::$mock_functions === null) { 38 return \ini_get(...$args); 39 } 40 41 return TestCase::$mock_functions->iniGet(...$args); 42} 43 44 45#[CoversClass(TimeoutService::class)] 46class TimeoutServiceTest extends TestCase 47{ 48 protected function setUp(): void 49 { 50 parent::setUp(); 51 52 self::$mock_functions = $this->createMock(MockGlobalFunctions::class); 53 } 54 55 protected function tearDown(): void 56 { 57 parent::setUp(); 58 59 self::$mock_functions = null; 60 } 61 62 public function testNoTimeOut(): void 63 { 64 $now = 1500000000.0; 65 66 $timeout_service = new TimeoutService($now); 67 68 self::$mock_functions 69 ->method('iniGet') 70 ->with('max_execution_time') 71 ->willReturn('0'); 72 73 self::assertFalse($timeout_service->isTimeNearlyUp()); 74 } 75 76 public function testTimeOutReached(): void 77 { 78 $now = 1500000000.0; 79 80 $timeout_service = new TimeoutService($now); 81 82 self::$mock_functions 83 ->method('iniGet') 84 ->with('max_execution_time') 85 ->willReturn('30'); 86 87 $time_factory = $this->createMock(TimeFactoryInterface::class); 88 $time_factory->method('now')->willReturn($now + 60.0); 89 Registry::timeFactory($time_factory); 90 91 self::assertTrue($timeout_service->isTimeNearlyUp()); 92 } 93 94 public function testTimeOutNotReached(): void 95 { 96 $now = Registry::timeFactory()->now(); 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 + 10.0); 107 Registry::timeFactory($time_factory); 108 109 self::assertFalse($timeout_service->isTimeNearlyUp()); 110 } 111 112 public function testTimeLimitNotReached(): void 113 { 114 $now = Registry::timeFactory()->now(); 115 116 $timeout_service = new TimeoutService($now); 117 118 $time_factory = $this->createMock(TimeFactoryInterface::class); 119 $time_factory->method('now')->willReturn($now + 1.4); 120 Registry::timeFactory($time_factory); 121 122 self::assertFalse($timeout_service->isTimeLimitUp()); 123 } 124 125 public function testTimeLimitReached(): void 126 { 127 $now = Registry::timeFactory()->now(); 128 129 $timeout_service = new TimeoutService($now); 130 131 $time_factory = $this->createMock(TimeFactoryInterface::class); 132 $time_factory->method('now')->willReturn($now + 1.6); 133 Registry::timeFactory($time_factory); 134 135 self::assertTrue($timeout_service->isTimeLimitUp()); 136 } 137} 138