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