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