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