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