TYPO3 CMS  TYPO3_8-7
UriTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
24 class UriTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
25 {
29  public function constructorSetsAllProperties()
30  {
31  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
32  $this->assertEquals('https', $uri->getScheme());
33  $this->assertEquals('user:pass', $uri->getUserInfo());
34  $this->assertEquals('local.example.com', $uri->getHost());
35  $this->assertEquals(3001, $uri->getPort());
36  $this->assertEquals('user:pass@local.example.com:3001', $uri->getAuthority());
37  $this->assertEquals('/foo', $uri->getPath());
38  $this->assertEquals('bar=baz', $uri->getQuery());
39  $this->assertEquals('quz', $uri->getFragment());
40  }
41 
45  public function canSerializeToString()
46  {
47  $url = 'https://user:pass@local.example.com:3001/foo?bar=baz#quz';
48  $uri = new Uri($url);
49  $this->assertEquals($url, (string)$uri);
50  }
51 
56  {
57  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
58  $new = $uri->withScheme('http');
59  $this->assertNotSame($uri, $new);
60  $this->assertEquals('http', $new->getScheme());
61  $this->assertEquals('http://user:pass@local.example.com:3001/foo?bar=baz#quz', (string)$new);
62  }
63 
68  {
69  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
70  $new = $uri->withUserInfo('matthew');
71  $this->assertNotSame($uri, $new);
72  $this->assertEquals('matthew', $new->getUserInfo());
73  $this->assertEquals('https://matthew@local.example.com:3001/foo?bar=baz#quz', (string)$new);
74  }
75 
80  {
81  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
82  $new = $uri->withUserInfo('matthew', 'zf2');
83  $this->assertNotSame($uri, $new);
84  $this->assertEquals('matthew:zf2', $new->getUserInfo());
85  $this->assertEquals('https://matthew:zf2@local.example.com:3001/foo?bar=baz#quz', (string)$new);
86  }
87 
92  {
93  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
94  $new = $uri->withHost('framework.zend.com');
95  $this->assertNotSame($uri, $new);
96  $this->assertEquals('framework.zend.com', $new->getHost());
97  $this->assertEquals('https://user:pass@framework.zend.com:3001/foo?bar=baz#quz', (string)$new);
98  }
99 
104  {
105  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
106  $new = $uri->withPort(null);
107  $this->assertEquals(
108  'https://user:pass@local.example.com/foo?bar=baz#quz',
109  (string)$new
110  );
111  }
115  public function validPortsDataProvider()
116  {
117  return [
118  'int' => [3000],
119  'string' => ['3000']
120  ];
121  }
122 
128  {
129  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
130  $new = $uri->withPort($port);
131  $this->assertNotSame($uri, $new);
132  $this->assertEquals($port, $new->getPort());
133  $this->assertEquals(
134  sprintf('https://user:pass@local.example.com:%d/foo?bar=baz#quz', $port),
135  (string)$new
136  );
137  }
138 
143  {
144  return [
145  'false' => [false],
146  'string' => ['string'],
147  'array' => [[3000]],
148  'object' => [(object)[3000]],
149  ];
150  }
151 
157  {
158  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
159  $this->expectException(\InvalidArgumentException::class);
160  $this->expectExceptionCode(1436717324);
161  $uri->withPort($port);
162  }
163 
168  {
169  return [
170  'zero' => [0],
171  'too-small' => [-1],
172  'too-big' => [65536],
173  ];
174  }
175 
184  {
185  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
186  $new = $uri->withPort(true);
187  $this->assertNotSame($uri, $new);
188  $this->assertEquals(1, $new->getPort());
189  }
190 
196  {
197  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
198  $this->expectException(\InvalidArgumentException::class);
199  $this->expectExceptionCode(1436717326);
200  $uri->withPort($port);
201  }
202 
207  {
208  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
209  $new = $uri->withPath('/bar/baz');
210  $this->assertNotSame($uri, $new);
211  $this->assertEquals('/bar/baz', $new->getPath());
212  $this->assertEquals('https://user:pass@local.example.com:3001/bar/baz?bar=baz#quz', (string)$new);
213  }
214 
218  public function invalidPathsDataProvider()
219  {
220  return [
221  'null' => [null],
222  'true' => [true],
223  'false' => [false],
224  'array' => [['/bar/baz']],
225  'object' => [(object)['/bar/baz']],
226  ];
227  }
228 
234  {
235  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
236  $this->expectException(\InvalidArgumentException::class);
237  $this->expectExceptionCode(1436717328);
238  $uri->withPath($path);
239  }
240 
245  {
246  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
247  $this->expectException(\InvalidArgumentException::class);
248  $this->expectExceptionCode(1436717330);
249  $uri->withPath('/bar/baz?bat=quz');
250  }
251 
256  {
257  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
258  $this->expectException(\InvalidArgumentException::class);
259  $this->expectExceptionCode(1436717332);
260  $uri->withPath('/bar/baz#bat');
261  }
262 
267  {
268  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
269  $new = $uri->withQuery('baz=bat');
270  $this->assertNotSame($uri, $new);
271  $this->assertEquals('baz=bat', $new->getQuery());
272  $this->assertEquals('https://user:pass@local.example.com:3001/foo?baz=bat#quz', (string)$new);
273  }
274 
279  {
280  return [
281  'null' => [null],
282  'true' => [true],
283  'false' => [false],
284  'array' => [['baz=bat']],
285  'object' => [(object)['baz=bat']],
286  ];
287  }
288 
294  {
295  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
296  $this->expectException(\InvalidArgumentException::class);
297  $this->expectExceptionCode(1436717334);
298  $uri->withQuery($query);
299  }
300 
305  {
306  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
307  $this->expectException(\InvalidArgumentException::class);
308  $this->expectExceptionCode(1436717336);
309  $uri->withQuery('baz=bat#quz');
310  }
311 
316  {
317  $uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
318  $new = $uri->withFragment('qat');
319  $this->assertNotSame($uri, $new);
320  $this->assertEquals('qat', $new->getFragment());
321  $this->assertEquals('https://user:pass@local.example.com:3001/foo?bar=baz#qat', (string)$new);
322  }
323 
327  public function authorityInfoDataProvider()
328  {
329  return [
330  'host-only' => ['http://foo.com/bar', 'foo.com'],
331  'host-port' => ['http://foo.com:3000/bar', 'foo.com:3000'],
332  'user-host' => ['http://me@foo.com/bar', 'me@foo.com'],
333  'user-host-port' => ['http://me@foo.com:3000/bar', 'me@foo.com:3000'],
334  ];
335  }
336 
341  public function getAuthorityReturnsExpectedValues($url, $expected)
342  {
343  $uri = new Uri($url);
344  $this->assertEquals($expected, $uri->getAuthority());
345  }
346 
350  public function canEmitOriginFormUrl()
351  {
352  $url = '/foo/bar?baz=bat';
353  $uri = new Uri($url);
354  $this->assertEquals($url, (string)$uri);
355  }
356 
361  {
362  $uri = new Uri('http://example.com/foo');
363  $new = $uri->withPath('');
364  $this->assertEquals('', $new->getPath());
365  }
366 
371  {
372  $uri = new Uri('http://example.com');
373  $this->assertEquals('http://example.com', (string)$uri);
374  }
375 
380  {
381  $uri = new Uri('?foo=bar');
382  $this->assertEquals('', $uri->getPath());
383  }
384 
389  {
390  $uri = new Uri('?foo=bar');
391  $this->assertEquals('?foo=bar', (string)$uri);
392  }
393 
398  {
399  return [
400  'null' => [null],
401  'true' => [true],
402  'false' => [false],
403  'int' => [1],
404  'float' => [1.1],
405  'array' => [['http://example.com/']],
406  'object' => [(object)['uri' => 'http://example.com/']],
407  ];
408  }
409 
414  {
415  $this->expectException(\InvalidArgumentException::class);
416  new Uri($uri);
417  }
418 
423  {
424  $this->expectException(\InvalidArgumentException::class);
425  new Uri('http:///www.php-fig.org/');
426  }
427 
432  {
433  $uri = new Uri('http://example.com');
434  $new = $uri->withScheme('https://');
435  $this->assertEquals('https', $new->getScheme());
436  }
437 
441  public function invalidSchemesDataProvider()
442  {
443  return [
444  'mailto' => ['mailto'],
445  'ftp' => ['ftp'],
446  'telnet' => ['telnet'],
447  'ssh' => ['ssh'],
448  'git' => ['git'],
449  ];
450  }
451 
457  {
458  $this->expectException(\InvalidArgumentException::class);
459  $this->expectExceptionCode(1436717338);
460  new Uri($scheme . '://example.com');
461  }
462 
468  {
469  $uri = new Uri('http://example.com');
470  $this->expectException(\InvalidArgumentException::class);
471  $this->expectExceptionCode(1436717338);
472  $uri->withScheme($scheme);
473  }
474 
479  {
480  $uri = new Uri('http://example.com');
481  $new = $uri->withPath('foo/bar');
482  $this->assertEquals('foo/bar', $new->getPath());
483  }
484 
489  {
490  $uri = new Uri('http://example.com');
491  $new = $uri->withPath('foo/bar');
492  $this->assertEquals('http://example.com/foo/bar', $new->__toString());
493  }
494 
499  {
500  $uri = new Uri('http://example.com');
501  $new = $uri->withQuery('?foo=bar');
502  $this->assertEquals('foo=bar', $new->getQuery());
503  }
504 
509  {
510  $uri = new Uri('http://example.com');
511  $new = $uri->withFragment('#/foo/bar');
512  $this->assertEquals('/foo/bar', $new->getFragment());
513  }
514 
519  {
520  return [
521  'http' => ['http', 80],
522  'https' => ['https', 443],
523  ];
524  }
525 
531  {
532  $uri = (new Uri())
533  ->withHost('example.com')
534  ->withScheme($scheme)
535  ->withPort($port);
536  $this->assertEquals('example.com', $uri->getAuthority());
537  }
538 
542  public function getPathIsProperlyEncoded()
543  {
544  $uri = (new Uri())->withPath('/foo^bar');
545  $expected = '/foo%5Ebar';
546  $this->assertEquals($expected, $uri->getPath());
547  }
548 
553  {
554  $uri = (new Uri())->withPath('/foo%5Ebar');
555  $expected = '/foo%5Ebar';
556  $this->assertEquals($expected, $uri->getPath());
557  }
558 
563  {
564  return [
565  'key-only' => ['k^ey', 'k%5Eey'],
566  'key-value' => ['k^ey=valu`', 'k%5Eey=valu%60'],
567  'array-key-only' => ['key[]', 'key%5B%5D'],
568  'array-key-value' => ['key[]=valu`', 'key%5B%5D=valu%60'],
569  'complex' => ['k^ey&key[]=valu`&f<>=`bar', 'k%5Eey&key%5B%5D=valu%60&f%3C%3E=%60bar'],
570  ];
571  }
572 
577  public function getQueryIsProperlyEncoded($query, $expected)
578  {
579  $uri = (new Uri())->withQuery($query);
580  $this->assertEquals($expected, $uri->getQuery());
581  }
582 
587  public function getQueryIsNotDoubleEncoded($query, $expected)
588  {
589  $uri = (new Uri())->withQuery($expected);
590  $this->assertEquals($expected, $uri->getQuery());
591  }
592 
597  {
598  $uri = (new Uri())->withFragment('/p^th?key^=`bar#b@z');
599  $expected = '/p%5Eth?key%5E=%60bar%23b@z';
600  $this->assertEquals($expected, $uri->getFragment());
601  }
602 
607  {
608  $expected = '/p%5Eth?key%5E=%60bar%23b@z';
609  $uri = (new Uri())->withFragment($expected);
610  $this->assertEquals($expected, $uri->getFragment());
611  }
612 }
getQueryIsProperlyEncoded($query, $expected)
Definition: UriTest.php:577
withPathNotSlashPrefixedIsEmittedWithSlashDelimiterWhenUriIsCastToString()
Definition: UriTest.php:488
getAuthorityReturnsExpectedValues($url, $expected)
Definition: UriTest.php:341
withQueryRaisesExceptionForInvalidQueryStringsByType($query)
Definition: UriTest.php:293
constructWithUnsupportedSchemeRaisesAnException($scheme)
Definition: UriTest.php:456
withSchemeUsingUnsupportedSchemeRaisesAnException($scheme)
Definition: UriTest.php:467
getQueryIsNotDoubleEncoded($query, $expected)
Definition: UriTest.php:587
getAuthorityOmitsPortForStandardSchemePortCombinations($scheme, $port)
Definition: UriTest.php:530