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