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