Repository navigation
Expand file tree
/
Copy pathUrlPatternTest.php
More file actions
236 lines (188 loc) · 7.28 KB
/
Copy pathUrlPatternTest.php
File metadata and controls
236 lines (188 loc) · 7.28 KB
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
<?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.
*/
declare(strict_types=1);
namespace League\Uri;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\UrlPattern\Result;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class UrlPatternTest extends TestCase
{
#[Test]
#[DataProvider('provideOptionalId')]
public function it_extracts_optional_parameters(string $pattern, string $input, ?string $expected): void
{
$result = UrlPattern::from($pattern, 'http://example.com')->extract($input);
self::assertInstanceOf(Result::class, $result);
self::assertSame($expected, $result->path['id']);
self::assertSame($expected, $result->path['id']);
}
public static function provideOptionalId(): iterable
{
yield 'missing' => ['/book/:id?', 'http://example.com/book', null];
yield 'present' => ['/book/:id?', 'http://example.com/book/123', '123'];
}
#[Test]
public function it_detects_a_regexp_group_for_regexp_parameter(): void
{
$pattern = UrlPatternBuilder::from('/book/:id?(\d+)')->build();
self::assertTrue($pattern->hasRegexpGroup);
}
#[Test]
public function it_does_not_detects_a_regexp_group_for_an_optional_parameter(): void
{
$pattern = UrlPatternBuilder::from('/book/:id?')->build();
self::assertFalse($pattern->hasRegexpGroup);
self::assertSame('/book/:id?', $pattern->path);
self::assertSame('*', $pattern->fragment);
}
#[Test]
public function it_does_not_detect_a_regexp_group_for_a_fixed_pattern(): void
{
$pattern = UrlPatternBuilder::from('/book/123')->build();
self::assertFalse($pattern->hasRegexpGroup);
}
#[Test]
public function it_matches_a_fixed_pattern(): void
{
$pattern = UrlPatternBuilder::from('/book/123')->build();
self::assertTrue($pattern->match('/book/123'));
self::assertFalse($pattern->match('/book/456'));
$result = $pattern->extract('/book/123');
self::assertInstanceOf(Result::class, $result);
self::assertFalse($result->hasValue());
self::assertFalse($pattern->hasVariable());
}
#[Test]
public function it_extracts_a_named_path_parameter(): void
{
$pattern = UrlPatternBuilder::from('/users/:id')->build();
$result = $pattern->extract('/users/42');
self::assertInstanceOf(Result::class, $result);
self::assertTrue($result->hasValue());
self::assertTrue($pattern->hasVariable());
self::assertSame('42', $result->path['id']);
}
#[Test]
public function it_treats_a_single_host_component_as_a_host_input(): void
{
$pattern = (new UrlPatternBuilder())->host('{:foo}.example.com')->build();
$result = $pattern->extract('http://xn--fi8h.example.com');
self::assertNotNull($result);
self::assertSame('🍅', $result->host['foo']);
}
#[Test]
public function it_parses_multiple_components_as_a_uri(): void
{
$pattern = (new UrlPatternBuilder())
->scheme('https')
->host('{:host}.example.com')
->path('/users/:id')
->build();
$result = $pattern->extract('https://foo.example.com/users/42');
self::assertNotNull($result);
self::assertSame('foo', $result->host['host']);
self::assertSame('42', $result->path['id']);
}
#[Test]
public function it_rejects_unexpected_uri_components(): void
{
$pattern = UrlPatternBuilder::from('/users/:id')->build();
self::assertTrue($pattern->match('/users/42?foo=bar'));
self::assertTrue($pattern->match('/users/42#section'));
}
#[Test]
public function it_decodes_path_captures(): void
{
$pattern = UrlPatternBuilder::from('/:foo')->build();
$result = $pattern->extract('/%F0%9F%8D%85');
self::assertInstanceOf(Result::class, $result);
self::assertSame('🍅', $result->path['foo']);
}
#[Test]
public function it_extracts_multiple_groups(): void
{
$pattern = UrlPattern::from('http://example.com/users/:user/books/:book');
$result = $pattern->extract('http://example.com/users/42/books/123');
self::assertInstanceOf(Result::class, $result);
self::assertSame('42', $result->path['user']);
self::assertSame('123', $result->path['book']);
}
#[Test]
public function it_resolves_a_relative_path_against_the_base_url(): void
{
$pattern = UrlPattern::from(
'/users/:id',
'https://example.com/api/',
);
self::assertTrue($pattern->match('https://example.com/users/42'));
}
#[Test]
#[DataProvider('provideInvalidInput')]
public function it_throws_an_exception_on_syntax_error(string $input): void
{
$this->expectException(SyntaxError::class);
UrlPattern::from($input);
}
public static function provideInvalidInput(): iterable
{
yield ['https://example.org/%('];
yield ['https://example.org/%(('];
yield ['(\\'];
yield ['()'];
}
#[Test]
public function it_can_return_implicit_components_value(): void
{
$pattern = (new UrlPatternBuilder(
path: '/hello/{:name}'
))
->host('{:subdomain.}?localhost')
->port('*')
->build();
$result = $pattern->extract('http://api.localhost:4000/hello/john?search=world');
self::assertInstanceOf(Result::class, $result);
self::assertSame(4000, $result->port->integer(0, 80));
self::assertTrue($result->query->hasValue());
self::assertSame('search=world', $result->query->input);
self::assertFalse($result->fragment->hasValue());
self::assertSame('', $result->fragment->input);
self::assertSame('/hello/john', $result->path->input);
self::assertSame('john', $result->path->string('name'));
}
#[Test]
public function it_recognizes_an_escaped_protocol_suffix(): void
{
$pattern = UrlPattern::from('data\\:foo*');
self::assertSame('data', $pattern->scheme);
self::assertSame('foo*', $pattern->path);
self::assertSame('*', $pattern->username);
self::assertSame('*', $pattern->password);
self::assertSame('', $pattern->host);
self::assertSame('', $pattern->port);
self::assertSame('*', $pattern->query);
self::assertSame('*', $pattern->fragment);
self::assertTrue($pattern->match('data:foobar'));
}
#[Test]
public function it_resolves_correctly_the_port(): void
{
$pattern = UrlPattern::from('/foo/*', 'https://example.com');
self::assertSame('https', $pattern->scheme);
self::assertSame('/foo/*', $pattern->path);
self::assertSame('*', $pattern->username);
self::assertSame('*', $pattern->password);
self::assertSame('example.com', $pattern->host);
self::assertSame('', $pattern->port);
self::assertSame('*', $pattern->query);
self::assertSame('*', $pattern->fragment);
}
}