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