xref: /webtrees/tests/app/Services/TimeoutServiceTest.php (revision 71239cb694d278d044f33328daaa60c8ed7431e9)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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    public function setUp()
61    {
62        self::$mock_functions = Mockery::mock();
63    }
64
65    /**
66     * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct()
67     * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp()
68     *
69     * @return void
70     */
71    public function testNoTimeOut()
72    {
73        $now = \microtime(true);
74
75        $timeout_service = new TimeoutService($now);
76
77        self::$mock_functions
78            ->shouldReceive('ini_get')
79            ->with('max_execution_time')
80            ->once()
81            ->andReturn('0');
82
83        $this->assertFalse($timeout_service->isTimeNearlyUp());
84    }
85
86    /**
87     * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct()
88     * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp()
89     *
90     * @return void
91     */
92    public function testTimeOutReached()
93    {
94        $now = \microtime(true);
95
96        $timeout_service = new TimeoutService($now);
97
98        self::$mock_functions
99            ->shouldReceive('ini_get')
100            ->with('max_execution_time')
101            ->once()
102            ->andReturn('30');
103
104        self::$mock_functions
105            ->shouldReceive('microtime')
106            ->with('true')
107            ->once()
108            ->andReturn($now + 60.0);
109
110        $this->assertTrue($timeout_service->isTimeNearlyUp());
111    }
112
113    /**
114     * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct()
115     * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp()
116     *
117     * @return void
118     */
119    public function testTimeOutNotReached()
120    {
121        $now = \microtime(true);
122
123        $timeout_service = new TimeoutService($now);
124
125        self::$mock_functions
126            ->shouldReceive('ini_get')
127            ->with('max_execution_time')
128            ->once()
129            ->andReturn('30');
130
131        self::$mock_functions
132            ->shouldReceive('microtime')
133            ->with('true')
134            ->once()
135            ->andReturn($now + 10.0);
136
137        $this->assertFalse($timeout_service->isTimeNearlyUp());
138    }
139
140    /**
141     * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct()
142     * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeLimitUp()
143     *
144     * @return void
145     */
146    public function testTimeLimitNotReached()
147    {
148        $now = \microtime(true);
149
150        $timeout_service = new TimeoutService($now);
151
152        self::$mock_functions
153            ->shouldReceive('microtime')
154            ->with('true')
155            ->once()
156            ->andReturn($now + 1.4);
157
158        $this->assertFalse($timeout_service->isTimeLimitUp());
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 testTimeLimitReached()
168    {
169        $now = \microtime(true);
170
171        $timeout_service = new TimeoutService($now);
172
173        self::$mock_functions
174            ->shouldReceive('microtime')
175            ->with('true')
176            ->once()
177            ->andReturn($now + 1.6);
178
179        $this->assertTrue($timeout_service->isTimeLimitUp());
180    }
181}
182