xref: /webtrees/tests/app/Services/TimeoutServiceTest.php (revision 1d20bd321d0d495f91b510f26ceb6ff79dd6a36e)
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    public function setUp()
57    {
58        self::$mock_functions = Mockery::mock();
59    }
60
61    /**
62     * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp()
63     */
64    public function testNoTimeOut()
65    {
66        $now = \microtime(true);
67
68        $timeout_service = new TimeoutService($now);
69
70        self::$mock_functions
71            ->shouldReceive('ini_get')
72            ->with('max_execution_time')
73            ->once()
74            ->andReturn('0');
75
76        $this->assertFalse($timeout_service->isTimeNearlyUp());
77    }
78
79    /**
80     * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp()
81     */
82    public function testTimeOutReached()
83    {
84        $now = \microtime(true);
85
86        $timeout_service = new TimeoutService($now);
87
88        self::$mock_functions
89            ->shouldReceive('ini_get')
90            ->with('max_execution_time')
91            ->once()
92            ->andReturn('30');
93
94        self::$mock_functions
95            ->shouldReceive('microtime')
96            ->with('true')
97            ->once()
98            ->andReturn($now + 60.0);
99
100        $this->assertTrue($timeout_service->isTimeNearlyUp());
101    }
102
103    /**
104     * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp()
105     */
106    public function testTimeOutNotReached()
107    {
108        $now = \microtime(true);
109
110        $timeout_service = new TimeoutService($now);
111
112        self::$mock_functions
113            ->shouldReceive('ini_get')
114            ->with('max_execution_time')
115            ->once()
116            ->andReturn('30');
117
118        self::$mock_functions
119            ->shouldReceive('microtime')
120            ->with('true')
121            ->once()
122            ->andReturn($now + 10.0);
123
124        $this->assertFalse($timeout_service->isTimeNearlyUp());
125    }
126
127    /**
128     * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeLimitUp()
129     */
130    public function testTimeLimitNotReached()
131    {
132        $now = \microtime(true);
133
134        $timeout_service = new TimeoutService($now);
135
136        self::$mock_functions
137            ->shouldReceive('microtime')
138            ->with('true')
139            ->once()
140            ->andReturn($now + 1.4);
141
142        $this->assertFalse($timeout_service->isTimeLimitUp());
143    }
144
145    /**
146     * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeLimitUp()
147     */
148    public function testTimeLimitReached()
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.6);
159
160        $this->assertTrue($timeout_service->isTimeLimitUp());
161    }
162}
163