18 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
32 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
33 $this->assertEquals(
'https', $uri->getScheme());
34 $this->assertEquals(
'user:pass', $uri->getUserInfo());
35 $this->assertEquals(
'local.example.com', $uri->getHost());
36 $this->assertEquals(3001, $uri->getPort());
37 $this->assertEquals(
'user:pass@local.example.com:3001', $uri->getAuthority());
38 $this->assertEquals(
'/foo', $uri->getPath());
39 $this->assertEquals(
'bar=baz', $uri->getQuery());
40 $this->assertEquals(
'quz', $uri->getFragment());
48 $url =
'https://user:pass@local.example.com:3001/foo?bar=baz#quz';
49 $uri =
new Uri($url);
50 $this->assertEquals($url, (
string)$uri);
58 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
59 $new = $uri->withScheme(
'http');
60 $this->assertNotSame($uri, $new);
61 $this->assertEquals(
'http', $new->getScheme());
62 $this->assertEquals(
'http://user:pass@local.example.com:3001/foo?bar=baz#quz', (
string)$new);
70 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
71 $new = $uri->withUserInfo(
'matthew');
72 $this->assertNotSame($uri, $new);
73 $this->assertEquals(
'matthew', $new->getUserInfo());
74 $this->assertEquals(
'https://matthew@local.example.com:3001/foo?bar=baz#quz', (
string)$new);
82 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
83 $new = $uri->withUserInfo(
'matthew',
'zf2');
84 $this->assertNotSame($uri, $new);
85 $this->assertEquals(
'matthew:zf2', $new->getUserInfo());
86 $this->assertEquals(
'https://matthew:zf2@local.example.com:3001/foo?bar=baz#quz', (
string)$new);
94 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
95 $new = $uri->withHost(
'framework.zend.com');
96 $this->assertNotSame($uri, $new);
97 $this->assertEquals(
'framework.zend.com', $new->getHost());
98 $this->assertEquals(
'https://user:pass@framework.zend.com:3001/foo?bar=baz#quz', (
string)$new);
106 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
107 $new = $uri->withPort(
null);
109 'https://user:pass@local.example.com/foo?bar=baz#quz',
130 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
131 $new = $uri->withPort($port);
132 $this->assertNotSame($uri, $new);
133 $this->assertEquals($port, $new->getPort());
135 sprintf(
'https://user:pass@local.example.com:%d/foo?bar=baz#quz', $port),
147 'string' => [
'string'],
149 'object' => [(object)[3000]],
159 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
160 $this->expectException(\InvalidArgumentException::class);
161 $this->expectExceptionCode(1436717324);
162 $uri->withPort($port);
173 'too-big' => [65536],
186 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
187 $new = $uri->withPort(
true);
188 $this->assertNotSame($uri, $new);
189 $this->assertEquals(1, $new->getPort());
198 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
199 $this->expectException(\InvalidArgumentException::class);
200 $this->expectExceptionCode(1436717326);
201 $uri->withPort($port);
209 $subject =
new Uri(
'http://www.example.com:80');
210 $this->assertEquals(
'http://www.example.com', (
string)$subject);
218 $subject =
new Uri(
'www.example.com:80');
219 $this->assertEquals(
'//www.example.com:80', (
string)$subject);
227 $subject =
new Uri(
'www.example.com');
228 $this->assertEquals(
'/www.example.com', (
string)$subject);
236 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
237 $new = $uri->withPath(
'/bar/baz');
238 $this->assertNotSame($uri, $new);
239 $this->assertEquals(
'/bar/baz', $new->getPath());
240 $this->assertEquals(
'https://user:pass@local.example.com:3001/bar/baz?bar=baz#quz', (
string)$new);
252 'array' => [[
'/bar/baz']],
253 'object' => [(object)[
'/bar/baz']],
263 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
264 $this->expectException(\InvalidArgumentException::class);
265 $this->expectExceptionCode(1436717328);
266 $uri->withPath($path);
274 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
275 $this->expectException(\InvalidArgumentException::class);
276 $this->expectExceptionCode(1436717330);
277 $uri->withPath(
'/bar/baz?bat=quz');
285 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
286 $this->expectException(\InvalidArgumentException::class);
287 $this->expectExceptionCode(1436717332);
288 $uri->withPath(
'/bar/baz#bat');
296 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
297 $new = $uri->withQuery(
'baz=bat');
298 $this->assertNotSame($uri, $new);
299 $this->assertEquals(
'baz=bat', $new->getQuery());
300 $this->assertEquals(
'https://user:pass@local.example.com:3001/foo?baz=bat#quz', (
string)$new);
312 'array' => [[
'baz=bat']],
313 'object' => [(object)[
'baz=bat']],
323 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
324 $this->expectException(\InvalidArgumentException::class);
325 $this->expectExceptionCode(1436717334);
326 $uri->withQuery($query);
334 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
335 $this->expectException(\InvalidArgumentException::class);
336 $this->expectExceptionCode(1436717336);
337 $uri->withQuery(
'baz=bat#quz');
345 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
346 $new = $uri->withFragment(
'qat');
347 $this->assertNotSame($uri, $new);
348 $this->assertEquals(
'qat', $new->getFragment());
349 $this->assertEquals(
'https://user:pass@local.example.com:3001/foo?bar=baz#qat', (
string)$new);
358 'host-only' => [
'http://foo.com/bar',
'foo.com'],
359 'host-port' => [
'http://foo.com:3000/bar',
'foo.com:3000'],
360 'user-host' => [
'http://me@foo.com/bar',
'me@foo.com'],
361 'user-host-port' => [
'http://me@foo.com:3000/bar',
'me@foo.com:3000'],
371 $uri =
new Uri($url);
372 $this->assertEquals($expected, $uri->getAuthority());
380 $url =
'/foo/bar?baz=bat';
381 $uri =
new Uri($url);
382 $this->assertEquals($url, (
string)$uri);
390 $uri =
new Uri(
'http://example.com/foo');
391 $new = $uri->withPath(
'');
392 $this->assertEquals(
'', $new->getPath());
400 $uri =
new Uri(
'http://example.com');
401 $this->assertEquals(
'http://example.com', (
string)$uri);
409 $uri =
new Uri(
'?foo=bar');
410 $this->assertEquals(
'', $uri->getPath());
418 $uri =
new Uri(
'?foo=bar');
419 $this->assertEquals(
'?foo=bar', (
string)$uri);
433 'array' => [[
'http://example.com/']],
434 'object' => [(object)[
'uri' =>
'http://example.com/']],
443 $this->expectException(\InvalidArgumentException::class);
452 $this->expectException(\InvalidArgumentException::class);
453 new Uri(
'http:///www.php-fig.org/');
461 $uri =
new Uri(
'http://example.com');
462 $new = $uri->withScheme(
'https://');
463 $this->assertEquals(
'https', $new->getScheme());
472 'mailto' => [
'mailto'],
474 'telnet' => [
'telnet'],
486 $this->expectException(\InvalidArgumentException::class);
487 $this->expectExceptionCode(1436717338);
488 new Uri($scheme .
'://example.com');
497 $uri =
new Uri(
'http://example.com');
498 $this->expectException(\InvalidArgumentException::class);
499 $this->expectExceptionCode(1436717338);
500 $uri->withScheme($scheme);
508 $uri =
new Uri(
'http://example.com');
509 $new = $uri->withPath(
'foo/bar');
510 $this->assertEquals(
'foo/bar', $new->getPath());
518 $uri =
new Uri(
'https://user:pass@local.example.com:3001/foo?bar=baz#quz');
519 $new = $uri->withScheme(
'');
520 $this->assertNotSame($uri, $new);
521 $this->assertNotSame((
string)$uri, (
string)$new);
522 $this->assertEquals(
'', $new->getScheme());
523 $this->assertEquals(
'//user:pass@local.example.com:3001/foo?bar=baz#quz', (
string)$new);
531 $uri =
new Uri(
'http://example.com');
532 $new = $uri->withPath(
'foo/bar');
533 $this->assertEquals(
'http://example.com/foo/bar', $new->__toString());
541 $uri =
new Uri(
'http://example.com');
542 $new = $uri->withQuery(
'?foo=bar');
543 $this->assertEquals(
'foo=bar', $new->getQuery());
551 $uri =
new Uri(
'http://example.com');
552 $new = $uri->withFragment(
'#/foo/bar');
553 $this->assertEquals(
'/foo/bar', $new->getFragment());
562 'http' => [
'http', 80],
563 'https' => [
'https', 443],
574 ->withHost(
'example.com')
575 ->withScheme($scheme)
577 $this->assertEquals(
'example.com', $uri->getAuthority());
585 $uri = (
new Uri())->withPath(
'/foo^bar');
586 $expected =
'/foo%5Ebar';
587 $this->assertEquals($expected, $uri->getPath());
595 $uri = (
new Uri())->withPath(
'/foo%5Ebar');
596 $expected =
'/foo%5Ebar';
597 $this->assertEquals($expected, $uri->getPath());
606 'key-only' => [
'k^ey',
'k%5Eey'],
607 'key-value' => [
'k^ey=valu`',
'k%5Eey=valu%60'],
608 'array-key-only' => [
'key[]',
'key%5B%5D'],
609 'array-key-value' => [
'key[]=valu`',
'key%5B%5D=valu%60'],
610 'complex' => [
'k^ey&key[]=valu`&f<>=`bar',
'k%5Eey&key%5B%5D=valu%60&f%3C%3E=%60bar'],
620 $uri = (
new Uri())->withQuery($query);
621 $this->assertEquals($expected, $uri->getQuery());
630 $uri = (
new Uri())->withQuery($expected);
631 $this->assertEquals($expected, $uri->getQuery());
639 $uri = (
new Uri())->withFragment(
'/p^th?key^=`bar#b@z');
640 $expected =
'/p%5Eth?key%5E=%60bar%23b@z';
641 $this->assertEquals($expected, $uri->getFragment());
649 $expected =
'/p%5Eth?key%5E=%60bar%23b@z';
650 $uri = (
new Uri())->withFragment($expected);
651 $this->assertEquals($expected, $uri->getFragment());