|
| 1 | +<?php |
| 2 | +namespace Wa72\Url\Tests; |
| 3 | + |
| 4 | +use Psr\Http\Message\UriInterface; |
| 5 | +use Wa72\Url\Psr7Uri; |
| 6 | +use Wa72\Url\Url; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | + |
| 9 | +class Psr7UriTest extends TestCase |
| 10 | +{ |
| 11 | + public function testToUrl() |
| 12 | + { |
| 13 | + $psr7uri = Psr7Uri::parse('http://www.test.test/index.html'); |
| 14 | + $url = $psr7uri->toUrl(); |
| 15 | + $this->assertInstanceOf(Url::class, $url); |
| 16 | + $this->assertEquals('http://www.test.test/index.html', (string) $url); |
| 17 | + } |
| 18 | + |
| 19 | + public function testGetUserInfo() |
| 20 | + { |
| 21 | + $psr7uri = Psr7Uri::parse('http://user:pass@www.test.test/index.html'); |
| 22 | + $this->assertEquals('user:pass', $psr7uri->getUserInfo()); |
| 23 | + } |
| 24 | + |
| 25 | + public function testGetPort() |
| 26 | + { |
| 27 | + $psr7uri = Psr7Uri::parse('http://user:pass@www.test.test/index.html'); |
| 28 | + $this->assertNull($psr7uri->getPort()); |
| 29 | + |
| 30 | + $psr7uri = Psr7Uri::parse('http://user:pass@www.test.test:81/index.html'); |
| 31 | + $this->assertEquals(81, $psr7uri->getPort()); |
| 32 | + } |
| 33 | + |
| 34 | + public function testGetAuthority() |
| 35 | + { |
| 36 | + $psr7uri = Psr7Uri::parse('http://user:pass@www.test.test/index.html'); |
| 37 | + $this->assertEquals('user:pass@www.test.test', $psr7uri->getAuthority()); |
| 38 | + } |
| 39 | + |
| 40 | + public function testWithScheme() |
| 41 | + { |
| 42 | + $psr7uri = Psr7Uri::parse('http://user:pass@www.test.test/index.html'); |
| 43 | + $uri2 = $psr7uri->withScheme('https'); |
| 44 | + $this->assertEquals('https://user:pass@www.test.test/index.html', (string) $uri2); |
| 45 | + } |
| 46 | + |
| 47 | + public function testWithFragment() |
| 48 | + { |
| 49 | + $psr7uri = Psr7Uri::parse('http://user:pass@www.test.test/index.html'); |
| 50 | + $uri2 = $psr7uri->withFragment('foo'); |
| 51 | + $this->assertEquals('http://user:pass@www.test.test/index.html#foo', (string) $uri2); |
| 52 | + } |
| 53 | + |
| 54 | + public function testGetQuery() |
| 55 | + { |
| 56 | + $psr7uri = Psr7Uri::parse('http://test.test/index.php?a=b'); |
| 57 | + $this->assertEquals('a=b', $psr7uri->getQuery()); |
| 58 | + } |
| 59 | + |
| 60 | + public function testGetFragment() |
| 61 | + { |
| 62 | + $psr7uri = Psr7Uri::parse('http://test.test/index.php#foo'); |
| 63 | + $this->assertEquals('foo', $psr7uri->getFragment()); |
| 64 | + } |
| 65 | + |
| 66 | + public function testGetPath() |
| 67 | + { |
| 68 | + $psr7uri = Psr7Uri::parse('http://test.test/index.php#foo'); |
| 69 | + $this->assertEquals('/index.php', $psr7uri->getPath()); |
| 70 | + } |
| 71 | + |
| 72 | + public function testGetScheme() |
| 73 | + { |
| 74 | + $psr7uri = Psr7Uri::parse('http://test.test/index.php#foo'); |
| 75 | + $this->assertEquals('http', $psr7uri->getScheme()); |
| 76 | + } |
| 77 | + |
| 78 | + public function testGetHost() |
| 79 | + { |
| 80 | + $psr7uri = Psr7Uri::parse('http://test.test/index.php#foo'); |
| 81 | + $this->assertEquals('test.test', $psr7uri->getHost()); |
| 82 | + } |
| 83 | + |
| 84 | + public function testWithPort() |
| 85 | + { |
| 86 | + $psr7uri = Psr7Uri::parse('http://user:pass@www.test.test/index.html'); |
| 87 | + $uri2 = $psr7uri->withPort(81); |
| 88 | + $this->assertEquals('http://user:pass@www.test.test:81/index.html', (string) $uri2); |
| 89 | + } |
| 90 | + |
| 91 | + public function testWithHost() |
| 92 | + { |
| 93 | + $psr7uri = Psr7Uri::parse('http://user:pass@www.test.test/index.html'); |
| 94 | + $uri2 = $psr7uri->withHost('another-host'); |
| 95 | + $this->assertEquals('http://user:pass@another-host/index.html', (string) $uri2); |
| 96 | + } |
| 97 | + |
| 98 | + public function testWithPath() |
| 99 | + { |
| 100 | + $psr7uri = Psr7Uri::parse('http://user:pass@www.test.test/index.html'); |
| 101 | + $uri2 = $psr7uri->withPath('/a/b/test.html'); |
| 102 | + $this->assertEquals('http://user:pass@www.test.test/a/b/test.html', (string) $uri2); |
| 103 | + } |
| 104 | + |
| 105 | + public function testWithUserInfo() |
| 106 | + { |
| 107 | + $psr7uri = Psr7Uri::parse('http://user:pass@www.test.test/index.html'); |
| 108 | + $uri2 = $psr7uri->withUserInfo('u2', 'secret'); |
| 109 | + $this->assertEquals('http://u2:secret@www.test.test/index.html', (string) $uri2); |
| 110 | + } |
| 111 | + |
| 112 | + public function testWithQuery() |
| 113 | + { |
| 114 | + $psr7uri = Psr7Uri::parse('http://user:pass@www.test.test/index.html'); |
| 115 | + $uri2 = $psr7uri->withQuery('a=b'); |
| 116 | + $this->assertEquals('http://user:pass@www.test.test/index.html?a=b', (string) $uri2); |
| 117 | + } |
| 118 | + |
| 119 | + public function testFromUrl() |
| 120 | + { |
| 121 | + $url = Url::parse('https://www.foo.bar/test.php?a=b'); |
| 122 | + $psr7uri = Psr7Uri::fromUrl($url); |
| 123 | + $this->assertInstanceOf(UriInterface::class, $psr7uri); |
| 124 | + $this->assertEquals('https://www.foo.bar/test.php?a=b', (string) $psr7uri); |
| 125 | + } |
| 126 | +} |
0 commit comments