File: IpAddressTest.php

package info (click to toggle)
php-league-uri-src 7.5.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,712 kB
  • sloc: php: 16,698; javascript: 127; makefile: 43; xml: 36
file content (190 lines) | stat: -rw-r--r-- 5,718 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php

/**
 * League.Uri (https://uri.thephpleague.com)
 *
 * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace League\Uri\Components;

use League\Uri\Exceptions\SyntaxError;
use PHPUnit\Framework\TestCase;
use Stringable;

#[\PHPUnit\Framework\Attributes\CoversClass(\League\Uri\Components\Host::class)]
#[\PHPUnit\Framework\Attributes\Group('host')]
final class IpAddressTest extends TestCase
{
    #[\PHPUnit\Framework\Attributes\DataProvider('validIpAddressProvider')]
    public function testValidIpAddress(
        Stringable|int|string|null $host,
        bool $isDomain,
        bool $isIp,
        bool $isIpv4,
        bool $isIpv6,
        bool $isIpFuture,
        ?string $ipVersion,
        string $uri,
        ?string $ip,
        string $iri
    ): void {
        $host = null === $host ? Host::new() : Host::new((string) $host);

        self::assertSame($isIp, $host->isIp());
        self::assertSame($isIpv4, $host->isIpv4());
        self::assertSame($isIpv6, $host->isIpv6());
        self::assertSame($isIpFuture, $host->isIpFuture());
        self::assertNotEquals($isIp, $host->isRegisteredName());
        self::assertSame($ip, $host->getIp());
        self::assertSame($ipVersion, $host->getIpVersion());
    }

    public static function validIpAddressProvider(): array
    {
        return [
            'ip host object' => [
                Host::fromIp('127.0.0.1'),
                false,
                true,
                true,
                false,
                false,
                '4',
                '127.0.0.1',
                '127.0.0.1',
                '127.0.0.1',
            ],
            'ipv4' => [
                '127.0.0.1',
                false,
                true,
                true,
                false,
                false,
                '4',
                '127.0.0.1',
                '127.0.0.1',
                '127.0.0.1',
            ],
            'ipv6' => [
                '[::1]',
                false,
                true,
                false,
                true,
                false,
                '6',
                '[::1]',
                '::1',
                '[::1]',
            ],
            'scoped ipv6' => [
                '[fe80:1234::%251]',
                false,
                true,
                false,
                true,
                false,
                '6',
                '[fe80:1234::%251]',
                'fe80:1234::%1',
                '[fe80:1234::%251]',
            ],
            'ipfuture' => [
                '[v1.ZZ.ZZ]',
                false,
                true,
                false,
                false,
                true,
                '1',
                '[v1.ZZ.ZZ]',
                'ZZ.ZZ',
                '[v1.ZZ.ZZ]',
            ],
            'registered name' => [
                'uri.thephpleague.com',
                false,
                false,
                false,
                false,
                false,
                null,
                'uri.thephpleague.com',
                null,
                'uri.thephpleague.com',
            ],
        ];
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('createFromIpValid')]
    public function testCreateFromIp(string $input, string $version, string $expected): void
    {
        self::assertSame($expected, (string) Host::fromIp($input, $version));
    }

    public static function createFromIpValid(): array
    {
        return [
            'ipv4' => ['127.0.0.1', '', '127.0.0.1'],
            'ipv4 does care about the version string' => ['127.0.0.1', 'FA', '[vFA.127.0.0.1]'],
            'ipv6' => ['::1', '', '[::1]'],
            'ipv6 does care about the version string' => ['::1', '12', '[v12.::1]'],
            'ipv6 with scope' => ['fe80:1234::%1', '', '[fe80:1234::%251]'],
            'valid IpFuture' => ['csucj.$&+;::', 'AF', '[vAF.csucj.$&+;::]'],
            'octal IP' => ['0300.0250.0000.0001', '', '192.168.0.1'],
        ];
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('createFromIpFailed')]
    public function testCreateFromIpFailed(string $input): void
    {
        $this->expectException(SyntaxError::class);
        Host::fromIp($input);
    }

    public static function createFromIpFailed(): array
    {
        return [
            'false ipv4' => ['127..1'],
            'hostname' => ['example.com'],
            'false ipfuture' => ['vAF.csucj.$&+;:/:'],
            'formatted ipv6' => ['[::1]'],
        ];
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('withoutZoneIdentifierProvider')]
    public function testWithoutZoneIdentifier(string $host, string $expected): void
    {
        self::assertSame($expected, (string) Host::new($host)->withoutZoneIdentifier());
    }

    public static function withoutZoneIdentifierProvider(): array
    {
        return [
            'ipv4 host' => ['127.0.0.1', '127.0.0.1'],
            'ipv6 host' => ['[::1]', '[::1]'],
            'ipv6 scoped (1)' => ['[fe80::%251]', '[fe80::]'],
            'ipv6 scoped (2)' => ['[fe80::%1]', '[fe80::]'],
        ];
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('hasZoneIdentifierProvider')]
    public function testHasZoneIdentifier(string $host, bool $expected): void
    {
        self::assertSame($expected, Host::new($host)->hasZoneIdentifier());
    }

    public static function hasZoneIdentifierProvider(): array
    {
        return [
            ['127.0.0.1', false],
            ['[::1]', false],
            ['[fe80::%251]', true],
        ];
    }
}