File: AuthorityTest.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 (274 lines) | stat: -rw-r--r-- 9,322 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?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\Contracts\UriInterface;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\Http;
use League\Uri\Uri;
use League\Uri\UriString;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UriInterface as Psr7UriInterface;

use function parse_url;

#[CoversClass(Authority::class)]
#[Group('userinfo')]
final class AuthorityTest extends TestCase
{
    #[DataProvider('validAuthorityDataProvider')]
    public function testConstructor(
        ?string $authority,
        ?string $host,
        ?int $port,
        ?string $userInfo,
        ?string $component
    ): void {
        if (null === $authority) {
            $instance = Authority::new();
        } else {
            $instance = Authority::new($authority);
        }

        self::assertSame($host, $instance->getHost());
        self::assertSame($port, $instance->getPort());
        self::assertSame($userInfo, $instance->getUserInfo());
        self::assertSame($component, $instance->value());
    }

    public static function validAuthorityDataProvider(): array
    {
        return [
            'null values' => [
                'authority' => null,
                'host' => null,
                'port' => null,
                'userInfo' => null,
                'component' => null,
            ],
            'empty string' => [
                'authority' => '',
                'host' => '',
                'port' => null,
                'userInfo' => null,
                'component' => '',
            ],
            'auth with port' => [
                'authority' => 'example.com:443',
                'host' => 'example.com',
                'port' => 443,
                'userInfo' => null,
                'component' => 'example.com:443',
            ],
            'auth with user info' => [
                'authority' => 'foo:bar@example.com',
                'host' => 'example.com',
                'port' => null,
                'userInfo' => 'foo:bar',
                'component' => 'foo:bar@example.com',
            ],
            'auth with user info AND port' => [
                'authority' => 'foo:bar@example.com:443',
                'host' => 'example.com',
                'port' => 443,
                'userInfo' => 'foo:bar',
                'component' => 'foo:bar@example.com:443',
            ],
        ];
    }

    #[DataProvider('invalidAuthorityDataProvider')]
    public function testConstructorFails(string $authority): void
    {
        $this->expectException(SyntaxError::class);

        Authority::new($authority);
    }

    public static function invalidAuthorityDataProvider(): array
    {
        return [
            'invalid port' => ['foo:bar@example.com:foo'],
            'invalid user info' => ["\0foo:bar@example.com:443"],
            'invalid host' => ['foo:bar@[:1]:80'],
        ];
    }

    public function testWithHost(): void
    {
        $authority = Authority::new('foo:bar@example.com:443');
        self::assertSame($authority, $authority->withHost('eXAmPle.CoM'));
        self::assertNotEquals($authority, $authority->withHost('[::1]'));
    }

    #[DataProvider('invalidHostDataProvider')]
    public function testWithHostFails(?string $host): void
    {
        $this->expectException(SyntaxError::class);

        Authority::new('foo:bar@example.com:443')->withHost($host);
    }

    public static function invalidHostDataProvider(): array
    {
        return [
            'invalid host' => ["foo\0"],
            'null host' => [null],
        ];
    }

    public function testWithPort(): void
    {
        $authority = Authority::new('foo:bar@example.com:443');

        self::assertSame($authority, $authority->withPort(443));
        self::assertNotEquals($authority, $authority->withPort(80));
    }

    public function testWithPortFails(): void
    {
        $this->expectException(SyntaxError::class);

        Authority::new('foo:bar@example.com:443')->withPort(-1);
    }

    public function testWithUserInfo(): void
    {
        $authority = Authority::new('foo:bar@example.com:443');

        self::assertSame($authority, $authority->withUserInfo('foo', 'bar'));
        self::assertNotEquals($authority, $authority->withUserInfo('foo'));
    }

    public function testWithUserInfoFails(): void
    {
        $this->expectException(SyntaxError::class);

        Authority::new('foo:bar@example.com:443')->withUserInfo("\0foo", 'bar');
    }

    #[DataProvider('stringRepresentationDataProvider')]
    public function testAuthorityStringRepresentation(
        ?string $authority,
        string $string,
        ?string $json,
        ?string $content,
        string $uriComponent
    ): void {
        if (null === $authority) {
            $instance = Authority::new();
        } else {
            $instance = Authority::new($authority);
        }

        self::assertSame($string, (string) $instance);
        self::assertSame($json, json_encode($instance));
        self::assertSame($content, $instance->value());
        self::assertSame($uriComponent, $instance->getUriComponent());
    }

    public static function stringRepresentationDataProvider(): array
    {
        return [
            'null' => [
                'authority' => null,
                'string' => '',
                'json' => 'null',
                'content' => null,
                'uriComponent' => '',
            ],
            'empty string' => [
                'authority' => '',
                'string' => '',
                'json' => '""',
                'content' => '',
                'uriComponent' => '//',
            ],
            'full authority' => [
                'authority' => 'foo:bar@eXAmPle.cOm:443',
                'string' => 'foo:bar@example.com:443',
                'json' => '"foo:bar@example.com:443"',
                'content' => 'foo:bar@example.com:443',
                'uriComponent' => '//foo:bar@example.com:443',
            ],
            'unicode host' => [
                'authority' => 'foo:bar@مثال.إختبار:443',
                'string' => 'foo:bar@xn--mgbh0fb.xn--kgbechtv:443',
                'json' => '"foo:bar@xn--mgbh0fb.xn--kgbechtv:443"',
                'content' => 'foo:bar@xn--mgbh0fb.xn--kgbechtv:443',
                'uriComponent' => '//foo:bar@xn--mgbh0fb.xn--kgbechtv:443',
            ],
        ];
    }

    #[DataProvider('getURIProvider')]
    public function testCreateFromUri(UriInterface|Psr7UriInterface $uri, ?string $expected, array $components): void
    {
        $authority = Authority::fromUri($uri);

        self::assertSame($expected, $authority->value());
        self::assertSame($components, $authority->components());
    }

    public static function getURIProvider(): iterable
    {
        return [
            'PSR-7 URI object' => [
                'uri' => Http::new('http://foo:bar@example.com?foo=bar'),
                'expected' => 'foo:bar@example.com',
                'components' => ['user' => 'foo', 'pass' => 'bar', 'host' => 'example.com', 'port' => null],
            ],
            'PSR-7 URI object with no authority' => [
                'uri' => Http::new('path/to/the/sky?foo'),
                'expected' => null,
                'components' => ['user' => null, 'pass' => null, 'host' => null, 'port' => null],
            ],
            'PSR-7 URI object with empty string authority' => [
                'uri' => Http::new('file:///path/to/the/sky'),
                'expected' => '',
                'components' => ['user' => null, 'pass' => null, 'host' => '', 'port' => null],
            ],
            'League URI object' => [
                'uri' => Uri::new('http://foo:bar@example.com:83?foo=bar'),
                'expected' => 'foo:bar@example.com:83',
                'components' => ['user' => 'foo', 'pass' => 'bar', 'host' => 'example.com', 'port' => 83],
            ],
            'League URI object with no authority' => [
                'uri' => Uri::new('path/to/the/sky?foo'),
                'expected' => null,
                'components' => ['user' => null, 'pass' => null, 'host' => null, 'port' => null],
            ],
            'League URI object with empty string authority' => [
                'uri' => Uri::new('file:///path/to/the/sky'),
                'expected' => '',
                'components' => ['user' => null, 'pass' => null, 'host' => '', 'port' => null],
            ],
        ];
    }

    public function testCreateFromParseUrl(): void
    {
        $instance = Authority::fromComponents(parse_url('http://user:pass@ExaMplE.CoM:42#foobar'));

        self::assertSame('user:pass@example.com:42', $instance->toString());
    }

    public function testCreateFromParseUrlWithoutAuthority(): void
    {
        $instance = Authority::fromComponents(UriString::parse('/example.com:42#foobar'));

        self::assertNull($instance->value());
    }
}