xref: /webtrees/tests/app/Services/TimeoutServiceTest.php (revision 73f6d2b56f07f42ca02190f0dfc40653a1e91834)
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#[CoversClass(TimeoutService::class)]
45class TimeoutServiceTest extends TestCase
46{
47    protected function setUp(): void
48    {
49        parent::setUp();
50
51        self::$mock_functions = $this->createMock(MockGlobalFunctions::class);
52    }
53
54    protected function tearDown(): void
55    {
56        parent::setUp();
57
58        self::$mock_functions = null;
59    }
60
61    public function testNoTimeOut(): void
62    {
63        $now = 1500000000.0;
64
65        $timeout_service = new TimeoutService($now);
66
67        self::$mock_functions
68            ->method('iniGet')
69            ->with('max_execution_time')
70            ->willReturn('0');
71
72        self::assertFalse($timeout_service->isTimeNearlyUp());
73    }
74
75    public function testTimeOutReached(): void
76    {
77        $now = 1500000000.0;
78
79        $timeout_service = new TimeoutService($now);
80
81        self::$mock_functions
82            ->method('iniGet')
83            ->with('max_execution_time')
84            ->willReturn('30');
85
86        $time_factory = $this->createMock(TimeFactoryInterface::class);
87        $time_factory->method('now')->willReturn($now + 60.0);
88        Registry::timeFactory($time_factory);
89
90        self::assertTrue($timeout_service->isTimeNearlyUp());
91    }
92
93    public function testTimeOutNotReached(): void
94    {
95        $now = Registry::timeFactory()->now();
96
97        $timeout_service = new TimeoutService($now);
98
99        self::$mock_functions
100            ->method('iniGet')
101            ->with('max_execution_time')
102            ->willReturn('30');
103
104        $time_factory = $this->createMock(TimeFactoryInterface::class);
105        $time_factory->method('now')->willReturn($now + 10.0);
106        Registry::timeFactory($time_factory);
107
108        self::assertFalse($timeout_service->isTimeNearlyUp());
109    }
110
111    public function testTimeLimitNotReached(): void
112    {
113        $now = Registry::timeFactory()->now();
114
115        $timeout_service = new TimeoutService($now);
116
117        $time_factory = $this->createMock(TimeFactoryInterface::class);
118        $time_factory->method('now')->willReturn($now + 1.4);
119        Registry::timeFactory($time_factory);
120
121        self::assertFalse($timeout_service->isTimeLimitUp());
122    }
123
124    public function testTimeLimitReached(): void
125    {
126        $now = Registry::timeFactory()->now();
127
128        $timeout_service = new TimeoutService($now);
129
130        $time_factory = $this->createMock(TimeFactoryInterface::class);
131        $time_factory->method('now')->willReturn($now + 1.6);
132        Registry::timeFactory($time_factory);
133
134        self::assertTrue($timeout_service->isTimeLimitUp());
135    }
136}
137