forked from thephpleague/uri
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilderTest.php
More file actions
234 lines (192 loc) · 7.34 KB
/
BuilderTest.php
File metadata and controls
234 lines (192 loc) · 7.34 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
<?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;
use League\Uri\Components\FragmentDirectives\TextDirective;
use League\Uri\Exceptions\SyntaxError;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(Builder::class)]
final class BuilderTest extends TestCase
{
public function test_it_can_build_a_new_uri_instance(): void
{
$builder = new Builder();
$changedBuilder = $builder
->scheme('https')
->userInfo('user', 'pass')
->host('wiki.php.net')
->port(8080)
->path('rf:c/uri_followup')
->query('a=1&b=2')
->fragment('uri_building');
self::assertSame($changedBuilder, $builder);
self::assertSame('https://user:pass@wiki.php.net:8080/rf:c/uri_followup?a=1&b=2#uri_building', $builder->build()->toAsciiString());
}
public function test_it_can_build_a_new_uri_instance_using_the_constructor(): void
{
$builder = new Builder(
scheme: 'HtTpS',
username: 'user',
password: 'pass',
host: 'WiKi.PhP.NeT',
path: 'rf:c/uri_followup',
query: 'a=1&b=2',
fragment: 'uri_building'
);
self::assertSame('https://user:pass@wiki.php.net/rf:c/uri_followup?a=1&b=2#uri_building', $builder->build()->toAsciiString());
}
public function test_it_fails_to_build_a_new_uri_if_the_user_info_is_present_and_the_host_is_missing(): void
{
$this->expectException(SyntaxError::class);
(new Builder())
->scheme('https')
->userInfo('user', 'pass')
->path('rf:c/uri_followup')
->query('a=1&b=2')
->fragment('uri_building')
->build();
}
public function test_it_fails_to_build_a_new_uri_if_the_port_is_present_and_the_host_is_missing(): void
{
$this->expectException(SyntaxError::class);
(new Builder())
->scheme('https')
->port(8080)
->path('rf:c/uri_followup')
->query('a=1&b=2')
->fragment('uri_building')
->build();
}
public function test_it_fails_to_build_a_new_uri_if_the_scheme_is_missing_and_the_path_contains_colone_before_a_slash(): void
{
$this->expectException(SyntaxError::class);
(new Builder(
path: 'rf:c',
query: 'a=1&b=2',
fragment: 'uri_building'
))->build();
}
public function test_it_fails_if_the_scheme_contains_invalid_characters(): void
{
$this->expectException(SyntaxError::class);
(new Builder())->scheme('htt*s')->build();
}
public function test_it_encodes_path_contains_invalid_characters(): void
{
self::assertSame(
'rfc/uri_f%C3%B2llowup',
(new Builder(path: 'rfc/uri_fòllowup'))->build()->getPath()
);
}
public function test_building_without_calling_any_setter(): void
{
self::assertSame('', (new Builder())->build()->toString());
}
public function test_building_with_a_base_uri(): void
{
self::assertSame('https://example.com', (new Builder())->build('https://example.com')->toString());
}
public function test_it_can_resolve_the_uri_against_a_base_uri(): void
{
$builder = (new Builder())
->userInfo('user', 'pass')
->host('host')
->path('./.././toto')
->scheme('https');
self::assertSame('https://user:pass@host/./.././toto', $builder->build()->toAsciiString());
self::assertSame('https://user:pass@host/toto', $builder->build('https://host/toto')->toAsciiString());
}
public function test_it_can_be_reset(): void
{
$builder = (new Builder())
->userInfo('user', 'pass')
->host('host')
->path('./.././toto')
->scheme('https');
self::assertSame('https://user:pass@host/./.././toto', $builder->build()->toAsciiString());
self::assertSame('', $builder->reset()->build()->toString());
}
public function test_tap_calls_callback_with_self_and_returns_same_instance(): void
{
$builder = new Builder();
$called = false;
$callback = function (Builder $b) use (&$called, $builder): Builder {
$called = true;
self::assertSame($builder, $b);
return $b->authority('example.com');
};
$result = $builder->transform($callback);
self::assertTrue($called);
self::assertSame($builder, $result);
self::assertSame('example.com', $builder->build()->getHost());
self::assertNull($builder->build()->getScheme());
}
public function test_authority_with_host_only(): void
{
$uri = (new Builder())->authority('example.com')->build();
self::assertSame('example.com', $uri->getHost());
self::assertNull($uri->getPort());
}
public function test_authority_with_host_and_port(): void
{
$uri = (new Builder())->authority('example.com:8080')->build();
self::assertSame('example.com:8080', $uri->getAuthority());
self::assertSame('example.com', $uri->getHost());
self::assertSame(8080, $uri->getPort());
}
public function test_authority_with_host_port_and_user_info(): void
{
$uri = (new Builder())->authority('john:secret@example.com:8080')->build();
self::assertSame('john:secret@example.com:8080', $uri->getAuthority());
self::assertSame('john', $uri->getUsername());
self::assertSame('secret', $uri->getPassword());
self::assertSame('example.com', $uri->getHost());
self::assertSame(8080, $uri->getPort());
}
public function test_unsetting_authority_clear_host_and_port(): void
{
$uri = (new Builder())
->host('example.com')
->port(8080)
->authority(null)
->build();
self::assertNotSame('example.com', $uri->getHost());
self::assertNotSame(8080, $uri->getPort());
}
public function test_authority_can_be_overridden(): void
{
$uri = (new Builder())
->authority('example.com:8080')
->authority('api.example.com:443')
->build();
self::assertSame('api.example.com:443', $uri->getAuthority());
self::assertNotSame('example.com:8080', (string)$uri);
}
public function test_guard_with_valid_builder_returns_same_instance(): void
{
$builder = (new Builder())
->scheme('https')
->host('example.com');
self::assertSame($builder, $builder->guard());
}
public function test_guard_with_invalid_builder_throws_exception(): void
{
$this->expectException(SyntaxError::class);
$this->expectExceptionMessage('The current builder cannot generate a valid URI.');
(new Builder())->scheme('https')->guard();
}
public function test_builder_can_work_with_directives(): void
{
self::assertSame(
'#:~:text=hello,wo%20rld',
(new Builder())->fragment(new TextDirective('hello', 'wo rld'))->build()->toString()
);
}
}