File: DomainTest.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 (465 lines) | stat: -rw-r--r-- 15,855 bytes parent folder | download
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?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 ArrayIterator;
use League\Uri\Contracts\UriException;
use League\Uri\Contracts\UriInterface;
use League\Uri\Exceptions\OffsetOutOfBounds;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\Http;
use League\Uri\Uri;
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;

#[CoversClass(Domain::class)]
#[Group('host')]
final class DomainTest extends TestCase
{
    public function testItCanBeInstantiatedWithAHostInterfaceImplementingObject(): void
    {
        $host = Host::new('uri.thephpleague.com');
        $domain = Domain::new($host);

        self::assertSame('uri.thephpleague.com', $domain->value());
    }

    public function testItFailsIfTheHostInterfaceImplementingObjectIsNotADomain(): void
    {
        $this->expectException(UriException::class);

        Domain::new(Host::fromIp('127.0.0.1'));
    }

    public function testItFailsIfTheHostIsNotADomain(): void
    {
        $this->expectException(UriException::class);
        Domain::new('127.0.0.1');
    }

    public function testIterator(): void
    {
        $host = Domain::new('uri.thephpleague.com');

        self::assertEquals(['com', 'thephpleague', 'uri'], iterator_to_array($host));
        self::assertFalse($host->isIp());
        self::assertTrue($host->isDomain());
        self::assertTrue($host->isRegisteredName());
    }

    /**
     * Test valid Domain.
     */
    #[DataProvider('validDomainProvider')]
    public function testValidDomain(string $host, string $uri, string $iri): void
    {
        $host = Domain::new($host);

        self::assertSame($uri, $host->value());
        self::assertSame($iri, $host->toUnicode());
    }

    public static function validDomainProvider(): array
    {
        return [
            'normalized' => [
                'Master.EXAMPLE.cOm',
                'master.example.com',
                'master.example.com',
            ],
            'dot ending' => [
                'example.com.',
                'example.com.',
                'example.com.',
            ],
            'partial numeric' => [
                '23.42c.two',
                '23.42c.two',
                '23.42c.two',
            ],
            'all numeric' => [
                '98.3.2',
                '98.3.2',
                '98.3.2',
            ],
            'mix IP format with host label' => [
                'toto.127.0.0.1',
                'toto.127.0.0.1',
                'toto.127.0.0.1',
            ],
            'idn support' => [
                'مثال.إختبار',
                'xn--mgbh0fb.xn--kgbechtv',
                'مثال.إختبار',
            ],
            'IRI support' => [
                'xn--mgbh0fb.xn--kgbechtv',
                'xn--mgbh0fb.xn--kgbechtv',
                'مثال.إختبار',
            ],
        ];
    }

    #[DataProvider('invalidDomainProvider')]
    public function testInvalidDomain(?string $invalid): void
    {
        $this->expectException(SyntaxError::class);

        Domain::new($invalid);
    }

    public static function invalidDomainProvider(): array
    {
        return [
            'empty string' => [''],
            'ipv4' => ['127.0.0.1'],
            'ipv6' => ['::1'],
            'empty label' => ['tot.    .coucou.com'],
            'space in the label' => ['re view'],
            'Invalid IPv4 format' => ['[127.0.0.1]'],
            'Invalid IPv6 format' => ['[[::1]]'],
            'Invalid IPv6 format 2' => ['[::1'],
            'naked ipv6' => ['::1'],
            'scoped naked ipv6' => ['fe80:1234::%251'],
            'invalid character in scope ipv6' => ['[fe80:1234::%25%23]'],
            'space character in starting label' => ['example. com'],
            'invalid character in host label' => ["examp\0le.com"],
            'invalid IP with scope' => ['[127.2.0.1%253]'],
            'invalid scope IPv6' => ['[ab23::1234%251]'],
            'invalid scope ID' => ['[fe80::1234%25?@]'],
            'invalid scope ID with utf8 character' => ['[fe80::1234%25€]'],
            'invalid IPFuture' => ['[v4.1.2.3]'],
            'invalid host with mix content' => ['_b%C3%A9bé.be-'],
            'invalid IDN domain' => ['a⒈com'],
            'invalid Host with fullwith (1)' =>  ['%00.com'],
            'invalid host with fullwidth escaped' =>   ['%ef%bc%85%ef%bc%94%ef%bc%91.com'],
            'registered name not domain name' => ['master..plan.be'],
        ];
    }

    #[DataProvider('isAbsoluteProvider')]
    public function testIsAbsolute(string $raw, bool $expected): void
    {
        self::assertSame($expected, Domain::new($raw)->isAbsolute());
    }

    public static function isAbsoluteProvider(): array
    {
        return [
            ['example.com.', true],
            ['example.com', false],
        ];
    }

    public function testIpProperty(): void
    {
        $host = Domain::new('example.com');

        self::assertNull($host->getIpVersion());
        self::assertNull($host->getIp());
    }

    #[DataProvider('hostnamesProvider')]
    public function testValidUnicodeDomain(string $unicode, string $ascii): void
    {
        $host = Domain::new($unicode);

        self::assertSame($ascii, $host->toAscii());
        self::assertSame($unicode, $host->toUnicode());
    }

    public static function hostnamesProvider(): array
    {
        // http://en.wikipedia.org/wiki/.test_(international_domain_name)#Test_TLDs
        return [
            ['مثال.إختبار', 'xn--mgbh0fb.xn--kgbechtv'],
            ['مثال.آزمایشی', 'xn--mgbh0fb.xn--hgbk6aj7f53bba'],
            ['例子.测试', 'xn--fsqu00a.xn--0zwm56d'],
            ['例子.測試', 'xn--fsqu00a.xn--g6w251d'],
            ['пример.испытание', 'xn--e1afmkfd.xn--80akhbyknj4f'],
            ['उदाहरण.परीक्षा', 'xn--p1b6ci4b4b3a.xn--11b5bs3a9aj6g'],
            ['παράδειγμα.δοκιμή', 'xn--hxajbheg2az3al.xn--jxalpdlp'],
            ['실례.테스트', 'xn--9n2bp8q.xn--9t4b11yi5a'],
            ['בײַשפּיל.טעסט', 'xn--fdbk5d8ap9b8a8d.xn--deba0ad'],
            ['例え.テスト', 'xn--r8jz45g.xn--zckzah'],
            ['உதாரணம்.பரிட்சை', 'xn--zkc6cc5bi7f6e.xn--hlcj6aya9esc7a'],
            ['derhausüberwacher.de', 'xn--derhausberwacher-pzb.de'],
            ['renangonçalves.com', 'xn--renangonalves-pgb.com'],
            ['рф.ru', 'xn--p1ai.ru'],
            ['δοκιμή.gr', 'xn--jxalpdlp.gr'],
            ['ফাহাদ্১৯.বাংলা', 'xn--65bj6btb5gwimc.xn--54b7fta0cc'],
            ['𐌀𐌖𐌋𐌄𐌑𐌉·𐌌𐌄𐌕𐌄𐌋𐌉𐌑.gr', 'xn--uba5533kmaba1adkfh6ch2cg.gr'],
            ['guangdong.广东', 'guangdong.xn--xhq521b'],
            ['gwóźdź.pl', 'xn--gwd-hna98db.pl'],
        ];
    }

    #[DataProvider('countableProvider')]
    public function testCountable(string $host, int $nblabels, array $more): void
    {
        self::assertCount($nblabels, Domain::new($host));
    }

    public static function countableProvider(): array
    {
        return [
            'string' => ['secure.example.com', 3, ['com', 'example', 'secure']],
            'numeric' => ['92.56.8', 3, ['8', '56', '92']],
        ];
    }

    #[DataProvider('createFromLabelsValid')]
    public function testCreateFromLabels(iterable $input, string $expected): void
    {
        self::assertSame($expected, (string) Domain::fromLabels(...$input));
    }

    public static function createFromLabelsValid(): array
    {
        return [
            'array' => [['com', 'example', 'www'], 'www.example.com'],
            'iterator' => [new ArrayIterator(['com', 'example', 'www']), 'www.example.com'],
            'FQDN' => [['', 'com', 'example', 'www'], 'www.example.com.'],
            'another host object' => [Domain::new('example.com.'), 'example.com.'],
        ];
    }

    public function testCreateFromLabelsFailedWithEmptyStringLabel(): void
    {
        $this->expectException(SyntaxError::class);
        Domain::fromLabels('');
    }

    public function testCreateFromLabelsSucceedsWithEmptyLabel(): void
    {
        self::assertNull(Domain::fromLabels()->value());
    }

    public function testGet(): void
    {
        $host = Domain::new('master.example.com');
        self::assertSame('com', $host->get(0));
        self::assertSame('example', $host->get(1));
        self::assertSame('master', $host->get(-1));
        self::assertNull($host->get(23));
    }

    public function testOffsets(): void
    {
        $host = Domain::new('master.example.com');
        self::assertSame([2], $host->keys('master'));
        self::assertSame([0, 1, 2], $host->keys());
    }

    public function testLabels(): void
    {
        $host = Domain::new('master.example.com');
        self::assertSame(['com', 'example', 'master'], [...$host]);
        self::assertSame(['', 'localhost'], [...Domain::new('localhost.')]);
    }

    #[DataProvider('withoutProvider')]
    public function testWithout(string $host, int $without, string $res): void
    {
        self::assertSame($res, (string) Domain::new($host)->withoutLabel($without));
    }

    public static function withoutProvider(): array
    {
        return [
            'remove one string label' => ['secure.example.com', 0, 'secure.example'],
            'remove one string label negative offset' => ['secure.example.com', -1, 'example.com'],
        ];
    }

    public function testWithoutLabelVariadicArgument(): void
    {
        $host = Domain::new('www.example.com');

        self::assertSame($host, $host->withoutLabel());
    }

    public function testWithoutTriggersException(): void
    {
        $this->expectException(OffsetOutOfBounds::class);

        Domain::new('bébé.be')->withoutLabel(-23);
    }

    #[DataProvider('validPrepend')]
    public function testPrepend(string $raw, string $prepend, string $expected): void
    {
        self::assertSame($expected, (string) Domain::new($raw)->prepend($prepend));
    }

    public static function validPrepend(): array
    {
        return [
            ['secure.example.com', 'master', 'master.secure.example.com'],
            ['secure.example.com.', 'master', 'master.secure.example.com.'],
            ['secure.example.com', '127.0.0.1', '127.0.0.1.secure.example.com'],
            ['secure.example.com', '127.', '127.secure.example.com'],
            ['secure.example.com.', '127.', '127.secure.example.com.'],
            ['secure.example.com', '127', '127.secure.example.com'],
        ];
    }

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

        Domain::new('secure.example.com')->prepend('master..');
    }

    public function testPrependNull(): void
    {
        $domain = Domain::new('secure.example.com');

        self::assertSame($domain->prepend(null), $domain);
    }

    #[DataProvider('validAppend')]
    public function testAppend(string $raw, string $append, string $expected): void
    {
        self::assertSame($expected, (string) Domain::new($raw)->append($append));
    }

    public static function validAppend(): array
    {
        return [
            ['secure.example.com', 'master', 'secure.example.com.master'],
            ['secure.example.com', 'master.', 'secure.example.com.master.'],
            ['toto', '127.0.0.1', 'toto.127.0.0.1'],
            ['example.com', '', 'example.com.'],
            ['secure.example.com.', 'master', 'secure.example.com.master.'],
            ['secure.example.com.', 'master.', 'secure.example.com.master.'],
            ['secure.example.com', 'master.', 'secure.example.com.master.'],
        ];
    }

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

        Domain::new('secure.example.com')->append('master..');
    }

    public function testAppendNull(): void
    {
        $domain = Domain::new('secure.example.com');

        self::assertSame($domain->append(null), $domain);
    }

    #[DataProvider('replaceValid')]
    public function testReplace(string $raw, string $input, int $offset, string $expected): void
    {
        self::assertSame($expected, (string) Domain::new($raw)->withLabel($offset, $input));
    }

    public static function replaceValid(): array
    {
        return [
            ['master.example.com', 'shop', 3, 'master.example.com.shop'],
            ['master.example.com', 'shop', -4, 'shop.master.example.com'],
            ['master.example.com', 'shop', 2, 'shop.example.com'],
            ['master.example.com', 'master', 2, 'master.example.com'],
            ['secure.example.com', '127.0.0.1', 0, 'secure.example.127.0.0.1'],
            ['master.example.com.', 'shop', -2, 'master.shop.com.'],
            ['master.example.com', 'shop', -1, 'shop.example.com'],
            ['foo', 'bar', -1, 'bar'],
        ];
    }

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

        Domain::new('secure.example.com')->withLabel(2, '[::1]');
    }

    public function testReplaceMustFailed(): void
    {
        $this->expectException(OffsetOutOfBounds::class);

        Domain::new('secure.example.com')->withLabel(23, 'foo');
    }

    #[DataProvider('rootProvider')]
    public function testWithRoot(string $host, string $expected_with_root, string $expected_without_root): void
    {
        $host = Domain::new($host);

        self::assertSame($expected_with_root, (string) $host->withRootLabel());
        self::assertSame($expected_without_root, (string) $host->withoutRootLabel());
    }

    public static function rootProvider(): array
    {
        return [
            ['example.com', 'example.com.', 'example.com'],
            ['example.com.', 'example.com.', 'example.com'],
        ];
    }

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

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

    public static function getURIProvider(): iterable
    {
        return [
            'PSR-7 URI object' => [
                'uri' => Http::new('http://example.com?foo=bar'),
                'expected' => 'example.com',
            ],
            'League URI object' => [
                'uri' => Uri::new('http://example.com?foo=bar'),
                'expected' => 'example.com',
            ],
        ];
    }

    public function testCreateFromAuthority(): void
    {
        $uri = Uri::new('http://example.com:443');
        $auth = Authority::fromUri($uri);

        self::assertEquals(Domain::fromUri($uri), Domain::fromAuthority($auth));
    }

    public function testSlice(): void
    {
        $domain = Domain::new('ulb.ac.be');

        self::assertSame($domain->value(), $domain->slice(-3)->value());
        self::assertSame($domain->value(), $domain->slice(0)->value());

        self::assertSame('ulb.ac', $domain->slice(1)->value());
        self::assertSame('ulb', $domain->slice(-1)->value());
        self::assertSame('be', $domain->slice(-3, 1)->value());
    }

    public function testSliceThrowsOnOverFlow(): void
    {
        $this->expectException(OffsetOutOfBounds::class);

        Domain::new('ulb.ac.be')->slice(5);
    }
}