Passed
Push — master ( e6d102...7dc300 )
by Eric
12:39
created

EnvironmentTest::testUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

239
        self::/** @scrutinizer ignore-call */ 
240
              expectException(RuntimeException::class);
Loading history...
240
        Environment::iniGet('');
241
242
        self::expectException(RuntimeException::class);
243
        Environment::iniGet('this_should_notbe_a_valid_option');
244
    }
245
246
    /**
247
     * Test Environment::iniSet().
248
     */
249
    public function testIniSet(): void
250
    {
251
        // @var string $oldValue
1 ignored issue
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
252
        $oldValue = Environment::iniSet('display_errors', Environment::iniGet('display_errors'));
253
254
        self::assertSame($oldValue, Environment::iniSet('display_errors', $oldValue));
255
256
        self::expectException(ArgumentCountError::class);
1 ignored issue
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

256
        self::/** @scrutinizer ignore-call */ 
257
              expectException(ArgumentCountError::class);
Loading history...
257
        // @phpstan-ignore-next-line
258
        Environment::iniSet('');
1 ignored issue
show
Bug introduced by
The call to Esi\Utility\Environment::iniSet() has too few arguments starting with value. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

258
        Environment::/** @scrutinizer ignore-call */ 
259
                     iniSet('');

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
259
260
        self::expectException(InvalidArgumentException::class);
261
        Environment::iniSet('', '');
262
    }
263
}
264