xref: /webtrees/tests/app/Services/TimeoutServiceTest.php (revision 8b7961e5bb7d784beed8a0f5c7c3ca1f5928a679)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Services;
21
22use Fisharebest\Webtrees\Contracts\TimeFactoryInterface;
23use Fisharebest\Webtrees\MockGlobalFunctions;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\TestCase;
26
27/**
28 * Mock function.
29 *
30 * @param mixed ...$args
31 *
32 * @return mixed
33 */
34function ini_get(...$args)
35{
36    if (TestCase::$mock_functions === null) {
37        return \ini_get(...$args);
38    }
39
40    return TestCase::$mock_functions->iniGet(...$args);
41}
42
43/**
44 * Test harness for the class TimeoutService
45 */
46class TimeoutServiceTest extends TestCase
47{
48    protected function setUp(): void
49    {
50        parent::setUp();
51
52        self::$mock_functions = $this->getMockForAbstractClass(MockGlobalFunctions::class);
53    }
54
55    protected function tearDown(): void
56    {
57        parent::setUp();
58
59        self::$mock_functions = null;
60    }
61
62    /**
63     * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct
64     * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp
65     */
66    public function testNoTimeOut(): void
67    {
68        $now = 1500000000.0;
69
70        $timeout_service = new TimeoutService($now);
71
72        self::$mock_functions
73            ->method('iniGet')
74            ->with('max_execution_time')
75            ->willReturn('0');
76
77        self::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(): void
85    {
86        $now = 1500000000.0;
87
88        $timeout_service = new TimeoutService($now);
89
90        self::$mock_functions
91            ->method('iniGet')
92            ->with('max_execution_time')
93            ->willReturn('30');
94
95        $time_factory = $this->createMock(TimeFactoryInterface::class);
96        $time_factory->method('now')->willReturn($now + 60.0);
97        Registry::timeFactory($time_factory);
98
99        self::assertTrue($timeout_service->isTimeNearlyUp());
100    }
101
102    /**
103     * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct
104     * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeNearlyUp
105     */
106    public function testTimeOutNotReached(): void
107    {
108        $now = Registry::timeFactory()->now();
109
110        $timeout_service = new TimeoutService($now);
111
112        self::$mock_functions
113            ->method('iniGet')
114            ->with('max_execution_time')
115            ->willReturn('30');
116
117        $time_factory = $this->createMock(TimeFactoryInterface::class);
118        $time_factory->method('now')->willReturn($now + 10.0);
119        Registry::timeFactory($time_factory);
120
121        self::assertFalse($timeout_service->isTimeNearlyUp());
122    }
123
124    /**
125     * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct
126     * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeLimitUp
127     */
128    public function testTimeLimitNotReached(): void
129    {
130        $now = Registry::timeFactory()->now();
131
132        $timeout_service = new TimeoutService($now);
133
134        $time_factory = $this->createMock(TimeFactoryInterface::class);
135        $time_factory->method('now')->willReturn($now + 1.4);
136        Registry::timeFactory($time_factory);
137
138        self::assertFalse($timeout_service->isTimeLimitUp());
139    }
140
141    /**
142     * @covers \Fisharebest\Webtrees\Services\TimeoutService::__construct
143     * @covers \Fisharebest\Webtrees\Services\TimeoutService::isTimeLimitUp
144     */
145    public function testTimeLimitReached(): void
146    {
147        $now = Registry::timeFactory()->now();
148
149        $timeout_service = new TimeoutService($now);
150
151        $time_factory = $this->createMock(TimeFactoryInterface::class);
152        $time_factory->method('now')->willReturn($now + 1.6);
153        Registry::timeFactory($time_factory);
154
155        self::assertTrue($timeout_service->isTimeLimitUp());
156    }
157}
158