|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Purl\Test; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Purl\Fragment; |
|
9
|
|
|
use Purl\ParserInterface; |
|
10
|
|
|
use Purl\Path; |
|
11
|
|
|
use Purl\Query; |
|
12
|
|
|
use Purl\Url; |
|
13
|
|
|
use function count; |
|
14
|
|
|
|
|
15
|
|
|
class UrlTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
public function testConstruct() : void |
|
18
|
|
|
{ |
|
19
|
|
|
$url = new Url(); |
|
20
|
|
|
$url->setUrl('http://jwage.com'); |
|
21
|
|
|
$this->assertEquals('http://jwage.com/', $url->getUrl()); |
|
22
|
|
|
$this->assertInstanceOf('Purl\Parser', $url->getParser()); |
|
23
|
|
|
|
|
24
|
|
|
$parser = new TestParser(); |
|
25
|
|
|
$url = new Url('http://jwage.com', $parser); |
|
26
|
|
|
$this->assertSame($parser, $url->getParser()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testSetParser() : void |
|
30
|
|
|
{ |
|
31
|
|
|
$parser = new TestParser(); |
|
32
|
|
|
$url = new Url(); |
|
33
|
|
|
$url->setParser($parser); |
|
34
|
|
|
$this->assertSame($parser, $url->getParser()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testParseSanity() : void |
|
38
|
|
|
{ |
|
39
|
|
|
$url = new Url('https://host.com:443/path with spaces?param1 with spaces=value1 with spaces¶m2=value2#fragment1/fragment2 with spaces?param1=value1¶m2 with spaces=value2 with spaces'); |
|
40
|
|
|
$this->assertEquals('https', $url->scheme); |
|
41
|
|
|
$this->assertEquals('host.com', $url->host); |
|
42
|
|
|
$this->assertEquals('443', $url->port); |
|
43
|
|
|
$this->assertInstanceOf('Purl\Path', $url->path); |
|
44
|
|
|
$this->assertEquals('/path%20with%20spaces', (string) $url->path); |
|
45
|
|
|
$this->assertInstanceOf('Purl\Query', $url->query); |
|
46
|
|
|
$this->assertEquals('param1_with_spaces=value1+with+spaces¶m2=value2', (string) $url->query); |
|
47
|
|
|
$this->assertInstanceOf('Purl\Fragment', $url->fragment); |
|
48
|
|
|
$this->assertEquals('fragment1/fragment2%20with%20spaces?param1=value1¶m2_with_spaces=value2+with+spaces', (string) $url->fragment); |
|
49
|
|
|
$this->assertInstanceOf('Purl\Path', $url->fragment->path); |
|
50
|
|
|
$this->assertInstanceOf('Purl\Query', $url->fragment->query); |
|
51
|
|
|
$this->assertEquals('param1=value1¶m2_with_spaces=value2+with+spaces', (string) $url->fragment->query); |
|
52
|
|
|
$this->assertEquals('fragment1/fragment2%20with%20spaces', (string) $url->fragment->path); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testParseStaticMethod() : void |
|
56
|
|
|
{ |
|
57
|
|
|
$url = Url::parse('http://google.com'); |
|
58
|
|
|
$this->assertInstanceOf('Purl\Url', $url); |
|
59
|
|
|
$this->assertEquals('http://google.com/', (string) $url); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testBuild() : void |
|
63
|
|
|
{ |
|
64
|
|
|
$url = Url::parse('http://jwage.com') |
|
65
|
|
|
->set('port', '443') |
|
66
|
|
|
->set('scheme', 'https'); |
|
67
|
|
|
|
|
68
|
|
|
$url->query |
|
69
|
|
|
->set('param1', 'value1') |
|
70
|
|
|
->set('param2', 'value2'); |
|
71
|
|
|
|
|
72
|
|
|
$url->path->add('about'); |
|
73
|
|
|
$url->path->add('me'); |
|
74
|
|
|
|
|
75
|
|
|
$url->fragment->path->add('fragment1'); |
|
76
|
|
|
$url->fragment->path->add('fragment2'); |
|
77
|
|
|
|
|
78
|
|
|
$url->fragment->query |
|
79
|
|
|
->set('param1', 'value1') |
|
80
|
|
|
->set('param2', 'value2'); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertEquals('https://jwage.com:443/about/me?param1=value1¶m2=value2#/fragment1/fragment2?param1=value1¶m2=value2', (string) $url); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function testJoin() : void |
|
86
|
|
|
{ |
|
87
|
|
|
$url = new Url('http://jwage.com/about?param=value#fragment'); |
|
88
|
|
|
$this->assertEquals('http://jwage.com/about?param=value#fragment', (string) $url); |
|
89
|
|
|
$url->join(new Url('http://about.me/jwage')); |
|
90
|
|
|
$this->assertEquals('http://about.me/jwage?param=value#fragment', (string) $url); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testSetPath() : void |
|
94
|
|
|
{ |
|
95
|
|
|
$url = new Url('http://jwage.com'); |
|
96
|
|
|
$url->path = 'about'; |
|
97
|
|
|
$this->assertInstanceOf('Purl\Path', $url->path); |
|
98
|
|
|
$this->assertEquals('about', (string) $url->path); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testGetPath() : void |
|
102
|
|
|
{ |
|
103
|
|
|
$url = new Url('http://jwage.com'); |
|
104
|
|
|
$url->path = 'about'; |
|
105
|
|
|
$this->assertInstanceOf('Purl\Path', $url->path); |
|
106
|
|
|
$this->assertEquals('about', (string) $url->getPath()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testSetQuery() : void |
|
110
|
|
|
{ |
|
111
|
|
|
$url = new Url('http://jwage.com'); |
|
112
|
|
|
$url->query->set('param1', 'value1'); |
|
113
|
|
|
$this->assertInstanceOf('Purl\Query', $url->query); |
|
114
|
|
|
$this->assertEquals('param1=value1', (string) $url->query); |
|
115
|
|
|
$this->assertEquals(['param1' => 'value1'], $url->query->getData()); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function testGetQuery() : void |
|
119
|
|
|
{ |
|
120
|
|
|
$url = new Url('http://jwage.com'); |
|
121
|
|
|
$url->query->set('param1', 'value1'); |
|
122
|
|
|
$this->assertInstanceOf('Purl\Query', $url->query); |
|
123
|
|
|
$this->assertEquals('param1=value1', $url->getQuery()); |
|
124
|
|
|
$this->assertEquals(['param1' => 'value1'], $url->getQuery()->getData()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function testSetFragment() : void |
|
128
|
|
|
{ |
|
129
|
|
|
$url = new Url('http://jwage.com'); |
|
130
|
|
|
$url->fragment->path = 'about'; |
|
131
|
|
|
$url->fragment->query->set('param1', 'value1'); |
|
132
|
|
|
$this->assertEquals('http://jwage.com/#about?param1=value1', (string) $url); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testProtocolRelativeUrl() : void |
|
136
|
|
|
{ |
|
137
|
|
|
$url = new Url('https://example.com'); |
|
138
|
|
|
$this->assertEquals('https', $url->join('//code.jquery.com/jquery-3.10.js')->scheme); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function testGetFragment() : void |
|
142
|
|
|
{ |
|
143
|
|
|
$url = new Url('http://jwage.com'); |
|
144
|
|
|
$url->fragment->path = 'about'; |
|
145
|
|
|
$url->fragment->query->set('param1', 'value1'); |
|
146
|
|
|
$this->assertEquals('about?param1=value1', (string) $url->getFragment()); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function testGetNetloc() : void |
|
150
|
|
|
{ |
|
151
|
|
|
$url = new Url('https://user:[email protected]:443'); |
|
152
|
|
|
$this->assertEquals('user:[email protected]:443', $url->getNetloc()); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function testGetUrl() : void |
|
156
|
|
|
{ |
|
157
|
|
|
$url = new Url('http://jwage.com'); |
|
158
|
|
|
$this->assertEquals('http://jwage.com/', $url->getUrl()); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function testSetUrl() : void |
|
162
|
|
|
{ |
|
163
|
|
|
$url = new Url('http://jwage.com'); |
|
164
|
|
|
$this->assertEquals('http://jwage.com/', $url->getUrl()); |
|
165
|
|
|
$url->setUrl('http://google.com'); |
|
166
|
|
|
$this->assertEquals('http://google.com/', $url->getUrl()); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function testArrayAccess() : void |
|
170
|
|
|
{ |
|
171
|
|
|
$url = new Url('http://jwage.com'); |
|
172
|
|
|
$url['path'] = 'about'; |
|
173
|
|
|
$this->assertEquals('http://jwage.com/about', (string) $url); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function testCanonicalization() : void |
|
177
|
|
|
{ |
|
178
|
|
|
$url = new Url('http://jwage.com'); |
|
179
|
|
|
$this->assertEquals('com.jwage', $url->canonical); |
|
180
|
|
|
|
|
181
|
|
|
$url = new Url('http://sub.domain.jwage.com/index.php?param1=value1'); |
|
182
|
|
|
$this->assertEquals('com.jwage.domain.sub/index.php?param1=value1', $url->canonical); |
|
183
|
|
|
|
|
184
|
|
|
$url = new Url('http://sub.domain.jwage.co.uk/index.php?param1=value1'); |
|
185
|
|
|
$this->assertEquals('uk.co.jwage.domain.sub/index.php?param1=value1', $url->canonical); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function testPath() : void |
|
189
|
|
|
{ |
|
190
|
|
|
$url = new Url('http://jwage.com'); |
|
191
|
|
|
$url->path->add('about')->add('me'); |
|
192
|
|
|
$this->assertEquals('http://jwage.com/about/me', (string) $url); |
|
193
|
|
|
$url->path->setPath('new/path'); |
|
194
|
|
|
$this->assertEquals('http://jwage.com/new/path', (string) $url); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public function testFragment() : void |
|
198
|
|
|
{ |
|
199
|
|
|
$url = new Url('http://jwage.com'); |
|
200
|
|
|
$url->fragment = 'test'; |
|
201
|
|
|
$url->fragment->path->add('about')->add('me'); |
|
202
|
|
|
$url->fragment->query->set('param1', 'value1'); |
|
203
|
|
|
$this->assertEquals('http://jwage.com/#test/about/me?param1=value1', (string) $url); |
|
204
|
|
|
|
|
205
|
|
|
$url->fragment = 'test/aboutme?param1=value1'; |
|
206
|
|
|
$this->assertEquals('test/aboutme', (string) $url->fragment->path); |
|
207
|
|
|
$this->assertEquals('param1=value1', (string) $url->fragment->query); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function testQuery() : void |
|
211
|
|
|
{ |
|
212
|
|
|
$url = new Url('http://jwage.com'); |
|
213
|
|
|
$url->query = 'param1=value1¶m2=value2'; |
|
214
|
|
|
$this->assertEquals(['param1' => 'value1', 'param2' => 'value2'], $url->query->getData()); |
|
215
|
|
|
$url->query->set('param3', 'value3'); |
|
216
|
|
|
$this->assertEquals('param1=value1¶m2=value2¶m3=value3', (string) $url->query); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function testIsAbsolute() : void |
|
220
|
|
|
{ |
|
221
|
|
|
$url1 = new Url('http://jwage.com'); |
|
222
|
|
|
$this->assertTrue($url1->isAbsolute()); |
|
223
|
|
|
|
|
224
|
|
|
$url2 = new Url('/about/me'); |
|
225
|
|
|
$this->assertFalse($url2->isAbsolute()); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
public function testGetResource() : void |
|
229
|
|
|
{ |
|
230
|
|
|
$url = new Url('http://jwage.com/about?query=value'); |
|
231
|
|
|
$this->assertEquals('/about?query=value', $url->resource); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
public function testPort() : void |
|
235
|
|
|
{ |
|
236
|
|
|
$url = new Url('http://jwage.com:443'); |
|
237
|
|
|
$this->assertEquals('443', $url->port); |
|
238
|
|
|
$this->assertEquals('http://jwage.com:443/', (string) $url); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
public function testAuth() : void |
|
242
|
|
|
{ |
|
243
|
|
|
$url = new Url('http://user:[email protected]'); |
|
244
|
|
|
$this->assertEquals('user', $url->user); |
|
245
|
|
|
$this->assertEquals('pass', $url->pass); |
|
246
|
|
|
$this->assertEquals('http://user:[email protected]/', (string) $url); |
|
247
|
|
|
|
|
248
|
|
|
$url = new Url('http://user:@jwage.com'); |
|
249
|
|
|
$this->assertEquals('user', $url->user); |
|
250
|
|
|
$this->assertEquals(null, $url->pass); |
|
251
|
|
|
$this->assertEquals('http://[email protected]/', (string) $url); |
|
252
|
|
|
|
|
253
|
|
|
$url = new Url('http://[email protected]'); |
|
254
|
|
|
$this->assertEquals('user', $url->user); |
|
255
|
|
|
$this->assertEquals(null, $url->pass); |
|
256
|
|
|
$this->assertEquals('http://[email protected]/', (string) $url); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
public function testExtract() : void |
|
260
|
|
|
{ |
|
261
|
|
|
$urls = Url::extract("test\nmore test https://google.com ftp://jwage.com ftps://jwage.com http://google.com\ntesting this out http://jwage.com more text https://we-are-a-professional-studio-of.photography"); |
|
262
|
|
|
$this->assertEquals(6, count($urls)); |
|
263
|
|
|
$this->assertEquals('https://google.com/', (string) $urls[0]); |
|
264
|
|
|
$this->assertEquals('ftp://jwage.com/', (string) $urls[1]); |
|
265
|
|
|
$this->assertEquals('ftps://jwage.com/', (string) $urls[2]); |
|
266
|
|
|
$this->assertEquals('http://google.com/', (string) $urls[3]); |
|
267
|
|
|
$this->assertEquals('http://jwage.com/', (string) $urls[4]); |
|
268
|
|
|
$this->assertEquals('https://we-are-a-professional-studio-of.photography/', (string) $urls[5]); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
public function testManualObjectConstruction() : void |
|
272
|
|
|
{ |
|
273
|
|
|
$url = new Url('http://jwage.com'); |
|
274
|
|
|
$url->set('path', new Path('about')); |
|
275
|
|
|
$url->set('query', new Query('param=value')); |
|
276
|
|
|
$url->set('fragment', new Fragment(new Path('about'), new Query('param=value'))); |
|
277
|
|
|
$this->assertEquals('http://jwage.com/about?param=value#about?param=value', (string) $url); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
public function testIdeGettersAndSetters() : void |
|
281
|
|
|
{ |
|
282
|
|
|
$url = new Url('http://jwage.com'); |
|
283
|
|
|
$url->setPath(new Path('about')); |
|
284
|
|
|
$url->setQuery(new Query('param=value')); |
|
285
|
|
|
$url->setFragment(new Fragment(new Path('about'), new Query('param=value'))); |
|
286
|
|
|
$this->assertEquals('http://jwage.com/about?param=value#about?param=value', (string) $url); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
public function testFromCurrentServerVariables() : void |
|
290
|
|
|
{ |
|
291
|
|
|
$_SERVER['HTTP_HOST'] = 'jwage.com'; |
|
292
|
|
|
$_SERVER['SERVER_PORT'] = 80; |
|
293
|
|
|
$_SERVER['REQUEST_URI'] = '/about'; |
|
294
|
|
|
|
|
295
|
|
|
$url = Url::fromCurrent(); |
|
296
|
|
|
$this->assertEquals('http://jwage.com/about', (string) $url); |
|
297
|
|
|
|
|
298
|
|
|
$_SERVER['REQUEST_URI'] = '/about?param=value'; |
|
299
|
|
|
|
|
300
|
|
|
$url = Url::fromCurrent(); |
|
301
|
|
|
$this->assertEquals('http://jwage.com/about?param=value', (string) $url); |
|
302
|
|
|
|
|
303
|
|
|
$_SERVER['HTTPS'] = 'off'; |
|
304
|
|
|
$_SERVER['HTTP_HOST'] = 'jwage.com'; |
|
305
|
|
|
unset($_SERVER['SERVER_PORT']); |
|
306
|
|
|
unset($_SERVER['REQUEST_URI']); |
|
307
|
|
|
|
|
308
|
|
|
$url = Url::fromCurrent(); |
|
309
|
|
|
$this->assertEquals('http://jwage.com/', (string) $url); |
|
310
|
|
|
|
|
311
|
|
|
$_SERVER['HTTPS'] = 'on'; |
|
312
|
|
|
$_SERVER['HTTP_HOST'] = 'jwage.com'; |
|
313
|
|
|
$_SERVER['SERVER_PORT'] = 443; |
|
314
|
|
|
unset($_SERVER['REQUEST_URI']); |
|
315
|
|
|
|
|
316
|
|
|
$url = Url::fromCurrent(); |
|
317
|
|
|
$this->assertEquals('https://jwage.com/', (string) $url); |
|
318
|
|
|
|
|
319
|
|
|
unset($_SERVER['HTTPS']); |
|
320
|
|
|
$_SERVER['HTTP_HOST'] = 'jwage.com'; |
|
321
|
|
|
$_SERVER['SERVER_PORT'] = 8080; |
|
322
|
|
|
unset($_SERVER['REQUEST_URI']); |
|
323
|
|
|
|
|
324
|
|
|
$url = Url::fromCurrent(); |
|
325
|
|
|
$this->assertEquals('http://jwage.com:8080/', (string) $url); |
|
326
|
|
|
|
|
327
|
|
|
unset($_SERVER['HTTPS']); |
|
328
|
|
|
$_SERVER['HTTP_HOST'] = 'jwage.com'; |
|
329
|
|
|
$_SERVER['SERVER_PORT'] = 80; |
|
330
|
|
|
unset($_SERVER['REQUEST_URI']); |
|
331
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'user'; |
|
332
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'passwd123'; |
|
333
|
|
|
|
|
334
|
|
|
$url = Url::fromCurrent(); |
|
335
|
|
|
$this->assertEquals('http://user:[email protected]/', (string) $url); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
public function testRelativeUrl() : void |
|
339
|
|
|
{ |
|
340
|
|
|
// test all resource parts |
|
341
|
|
|
$url = new Url('/path1/path2?x=1&y=2#frag'); |
|
342
|
|
|
$this->assertFalse($url->isAbsolute()); |
|
343
|
|
|
$this->assertEquals('/path1/path2?x=1&y=2#frag', (string) $url); |
|
344
|
|
|
|
|
345
|
|
|
// test base path |
|
346
|
|
|
$url = new Url('/path1'); |
|
347
|
|
|
$this->assertEquals('/path1', (string) $url); |
|
348
|
|
|
|
|
349
|
|
|
// test minimal path |
|
350
|
|
|
$url = new Url('/'); |
|
351
|
|
|
$this->assertEquals('/', (string) $url); |
|
352
|
|
|
|
|
353
|
|
|
// test feature request |
|
354
|
|
|
$url = new Url('/events'); |
|
355
|
|
|
$url->query->set('param1', 'value1'); |
|
356
|
|
|
$this->assertEquals('/events?param1=value1', (string) $url); |
|
357
|
|
|
} |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
class TestParser implements ParserInterface |
|
361
|
|
|
{ |
|
362
|
|
|
/** |
|
363
|
|
|
* @param string|Url|null $url |
|
364
|
|
|
* |
|
365
|
|
|
* @return mixed[] |
|
366
|
|
|
*/ |
|
367
|
|
|
public function parseUrl($url) : array |
|
368
|
|
|
{ |
|
369
|
|
|
return []; |
|
370
|
|
|
} |
|
371
|
|
|
} |
|
372
|
|
|
|