Passed
Push — master ( c803c1...00f1f1 )
by Eric
13:27
created

EnvironmentTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 93
dl 0
loc 214
c 1
b 0
f 0
rs 10
wmc 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Utility - Collection of various PHP utility functions.
7
 *
8
 * @author    Eric Sizemore <[email protected]>
9
 *
10
 * @version   2.0.0
11
 *
12
 * @copyright (C) 2017 - 2024 Eric Sizemore
13
 * @license   The MIT License (MIT)
14
 *
15
 * Copyright (C) 2017 - 2024 Eric Sizemore <https://www.secondversion.com>.
16
 *
17
 * Permission is hereby granted, free of charge, to any person obtaining a copy
18
 * of this software and associated documentation files (the "Software"), to
19
 * deal in the Software without restriction, including without limitation the
20
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
21
 * sell copies of the Software, and to permit persons to whom the Software is
22
 * furnished to do so, subject to the following conditions:
23
 *
24
 * The above copyright notice and this permission notice shall be included in
25
 * all copies or substantial portions of the Software.
26
 *
27
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33
 * THE SOFTWARE.
34
 */
35
36
namespace Esi\Utility\Tests;
37
38
use ArgumentCountError;
39
use Esi\Utility\Arrays;
40
use Esi\Utility\Environment;
41
use InvalidArgumentException;
42
43
use PHPUnit\Framework\Attributes\CoversClass;
44
use PHPUnit\Framework\TestCase;
45
use RuntimeException;
46
47
use function sprintf;
48
49
/**
50
 * Environment utility tests.
51
 *
52
 * @internal
53
 */
54
#[CoversClass(Environment::class)]
55
class EnvironmentTest extends TestCase
56
{
57
    /**
58
     * Test Environment::requestMethod().
59
     */
60
    public function testRequestMethod(): void
61
    {
62
        Arrays::set($_SERVER, 'HTTP_X_HTTP_METHOD_OVERRIDE', 'GET');
63
        Arrays::set($_SERVER, 'REQUEST_METHOD', 'POST');
64
65
        self::assertSame('GET', Environment::requestMethod());
66
67
        Arrays::set($_SERVER, 'HTTP_X_HTTP_METHOD_OVERRIDE', null);
68
69
        self::assertSame('POST', Environment::requestMethod());
70
71
        Arrays::set($_SERVER, 'HTTP_X_HTTP_METHOD_OVERRIDE', 'GET');
72
        Arrays::set($_SERVER, 'REQUEST_METHOD', null);
73
74
        self::assertSame('GET', Environment::requestMethod());
75
    }
76
77
    /**
78
     * Test Environment::ipAddress().
79
     */
80
    public function testIpAddress(): void
81
    {
82
        Arrays::set($_SERVER, 'REMOTE_ADDR', '1.1.1.1');
83
        Arrays::set($_SERVER, 'HTTP_CLIENT_IP', '1.1.1.2');
84
        Arrays::set($_SERVER, 'HTTP_X_FORWARDED_FOR', '1.1.1.3');
85
        Arrays::set($_SERVER, 'HTTP_X_REAL_IP', '1.1.1.4');
86
87
        self::assertSame('1.1.1.1', Environment::ipAddress());
88
        self::assertSame('1.1.1.3', Environment::ipAddress(true));
89
90
        Arrays::set($_SERVER, 'HTTP_X_FORWARDED_FOR', null);
91
92
        self::assertSame('1.1.1.4', Environment::ipAddress(true));
93
94
        Arrays::set($_SERVER, 'HTTP_X_REAL_IP', null);
95
96
        self::assertSame('1.1.1.2', Environment::ipAddress(true));
97
98
        // What if from cloudflare?
99
        Arrays::set($_SERVER, 'HTTP_CF_CONNECTING_IP', '1.1.1.5');
100
101
        self::assertSame('1.1.1.5', Environment::ipAddress());
102
        self::assertSame('1.1.1.2', Environment::ipAddress(true));
103
104
        Arrays::set($_SERVER, 'HTTP_CF_CONNECTING_IP', null);
105
    }
106
107
    /**
108
     * Test Environment::isPrivateIp().
109
     */
110
    public function testIsPrivateIp(): void
111
    {
112
        self::assertTrue(Environment::isPrivateIp('192.168.0.0'));
113
        self::assertFalse(Environment::isPrivateIp('1.1.1.1'));
114
    }
115
116
    /**
117
     * Test Environment::isReservedIp().
118
     */
119
    public function testIsReservedIp(): void
120
    {
121
        self::assertTrue(Environment::isReservedIp('0.255.255.255'));
122
        self::assertFalse(Environment::isReservedIp('192.168.0.0'));
123
    }
124
125
    /**
126
     * Test Environment::isPublicIp().
127
     */
128
    public function testIsPublicIp(): void
129
    {
130
        self::assertTrue(Environment::isPublicIp('1.1.1.1'));
131
        self::assertFalse(Environment::isPublicIp('192.168.0.0'));
132
        self::assertFalse(Environment::isPublicIp('0.255.255.255'));
133
    }
134
135
    /**
136
     * Test Environment::host().
137
     */
138
    public function testHost(): void
139
    {
140
        $origHost    = Environment::var('HTTP_HOST');
141
        $origFwdHost = Environment::var('HTTP_X_FORWARDED_HOST');
142
        $origSrvName = Environment::var('SERVER_NAME');
143
144
        Arrays::set($_SERVER, 'HTTP_HOST', 'example.com');
145
        Arrays::set($_SERVER, 'HTTP_X_FORWARDED_HOST', 'example2.com');
146
        Arrays::set($_SERVER, 'SERVER_NAME', null);
147
148
        self::assertSame('example2.com', Environment::host(false, true));
149
        self::assertSame('example.com', Environment::host());
150
151
        Arrays::set($_SERVER, 'HTTP_HOST', 'www.example.com');
152
153
        self::assertSame('www.example.com', Environment::host());
154
        self::assertSame('example.com', Environment::host(true));
155
156
        Arrays::set($_SERVER, 'HTTP_HOST', null);
157
        Arrays::set($_SERVER, 'SERVER_NAME', null);
158
        self::assertSame('localhost', Environment::host());
159
160
        Arrays::set($_SERVER, 'HTTP_HOST', $origHost);
161
        Arrays::set($_SERVER, 'HTTP_X_FORWARDED_HOST', $origFwdHost);
162
        Arrays::set($_SERVER, 'SERVER_NAME', $origSrvName);
163
    }
164
165
    /**
166
     * Test Environment::isHttps().
167
     */
168
    public function testIsHttps(): void
169
    {
170
        Arrays::set($_SERVER, 'HTTPS', null);
171
172
        self::assertFalse(Environment::isHttps());
173
174
        Arrays::set($_SERVER, 'HTTPS', 'on');
175
176
        self::assertTrue(Environment::isHttps());
177
178
        Arrays::set($_SERVER, 'HTTPS', null);
179
180
        Arrays::set($_SERVER, 'HTTP_X_FORWARDED_PROTO', 'https');
181
        self::assertTrue(Environment::isHttps());
182
183
        Arrays::set($_SERVER, 'HTTP_X_FORWARDED_PROTO', null);
184
    }
185
186
    /**
187
     * Test Environment::url().
188
     */
189
    public function testUrl(): void
190
    {
191
        $expected      = 'http://test.dev/test.php?foo=bar';
192
        $expectedAuth  = 'http://admin:[email protected]/test.php?foo=bar';
193
        $expectedPort  = sprintf('http://test.dev:%d/test.php?foo=bar', Environment::PORT_SECURE);
194
        $expectedPort2 = sprintf('https://test.dev:%d/test.php?foo=bar', Environment::PORT_UNSECURE);
195
        $expectedSSL   = 'https://test.dev/test.php?foo=bar';
196
197
        Arrays::set($_SERVER, 'HTTP_HOST', 'test.dev');
198
        Arrays::set($_SERVER, 'REQUEST_URI', '/test.php?foo=bar');
199
        Arrays::set($_SERVER, 'QUERY_STRING', 'foo=bar');
200
        Arrays::set($_SERVER, 'PHP_SELF', '/test.php');
201
202
        // Test basic url.
203
        self::assertSame($expected, Environment::url());
204
205
        // Test server auth.
206
        Arrays::set($_SERVER, 'PHP_AUTH_USER', 'admin');
207
        Arrays::set($_SERVER, 'PHP_AUTH_PW', '123');
208
        self::assertSame($expectedAuth, Environment::url());
209
210
        Arrays::set($_SERVER, 'PHP_AUTH_USER', null);
211
        Arrays::set($_SERVER, 'PHP_AUTH_PW', null);
212
213
        // Test port.
214
        Arrays::set($_SERVER, 'SERVER_PORT', Environment::PORT_SECURE);
215
        self::assertSame($expectedPort, Environment::url());
216
217
        // Test SSL.
218
        Arrays::set($_SERVER, 'HTTPS', 'on');
219
        self::assertSame($expectedSSL, Environment::url());
220
221
        Arrays::set($_SERVER, 'SERVER_PORT', Environment::PORT_UNSECURE);
222
        self::assertSame($expectedPort2, Environment::url());
223
224
        Arrays::set($_SERVER, 'HTTPS', null);
225
226
        // Test no $_SERVER['REQUEST_URI'] (e.g., MS IIS).
227
        Arrays::set($_SERVER, 'REQUEST_URI', null);
228
        self::assertSame($expected, Environment::url());
229
230
        // Reset.
231
        Arrays::set($_SERVER, 'HTTP_HOST', null);
232
        Arrays::set($_SERVER, 'QUERY_STRING', null);
233
        Arrays::set($_SERVER, 'PHP_SELF', null);
234
    }
235
236
    /**
237
     * Test Environment::iniGet().
238
     */
239
    public function testIniGet(): void
240
    {
241
        self::assertNotEmpty(Environment::iniGet('request_order'));
242
243
        self::assertLessThanOrEqual('1', Environment::iniGet('display_errors', true));
244
245
        self::expectException(RuntimeException::class);
246
        Environment::iniGet('');
247
248
        self::expectException(RuntimeException::class);
249
        Environment::iniGet('this_should_notbe_a_valid_option');
250
    }
251
252
    /**
253
     * Test Environment::iniSet().
254
     */
255
    public function testIniSet(): void
256
    {
257
        // @var string $oldValue
258
        $oldValue = Environment::iniSet('display_errors', Environment::iniGet('display_errors'));
259
260
        self::assertSame($oldValue, Environment::iniSet('display_errors', $oldValue));
261
262
        self::expectException(ArgumentCountError::class);
263
        // @phpstan-ignore-next-line
264
        Environment::iniSet('');
265
266
        self::expectException(InvalidArgumentException::class);
267
        Environment::iniSet('', '');
268
    }
269
}
270