From 795b18b0f36d38fcd47bef7b3a645347575d5789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Jensen?= Date: Sat, 3 Feb 2024 16:53:31 +0100 Subject: [PATCH] Tests --- src/Uri.php | 4 +-- tests/UriExtensionsTest.php | 64 +++++++++++++++++++++++++++++++++++++ tests/UriFactoryTest.php | 12 +++++-- 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/src/Uri.php b/src/Uri.php index 4d0da8d..2e707ca 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -361,7 +361,7 @@ public function getComponents(int $flags = 0): array return array_filter([ 'scheme' => $this->getScheme($flags), 'host' => $this->getHost($flags), - 'port' => $this->getPort($flags), + 'port' => $this->getPort($flags | self::REQUIRE_PORT), 'user' => $this->user, 'pass' => $this->pass, 'path' => $this->getPath($flags), @@ -626,7 +626,7 @@ private function merge(array $a, array $b): array foreach ($b as $key => $value) { if (is_int($key)) { $a[] = $value; - } elseif (array_key_exists($key, $a) && is_array($a[$key])) { + } elseif (array_key_exists($key, $a) && is_array($a[$key]) && is_array($b[$key])) { $a[$key] = $this->merge($a[$key], $b[$key]); } else { $a[$key] = $b[$key]; diff --git a/tests/UriExtensionsTest.php b/tests/UriExtensionsTest.php index b7ef316..02e99fd 100644 --- a/tests/UriExtensionsTest.php +++ b/tests/UriExtensionsTest.php @@ -12,6 +12,8 @@ use InvalidArgumentException; use PHPUnit\Framework\TestCase; use Psr\Http\Message\UriInterface; +use JsonSerializable; +use Stringable; class UriExtensionsTest extends TestCase { @@ -181,4 +183,66 @@ public function testWithMethodInvalidComponent(): void 'invalid' => 'invalid', ]); } + + public function testStringable(): void + { + $uri = new Uri('http://domain.tld:80/path?query=1#fragment'); + $this->assertInstanceOf(Stringable::class, $uri); + $this->assertSame('http://domain.tld/path?query=1#fragment', $uri->__toString()); + } + + public function testJsonSerializable(): void + { + $uri = new Uri('http://domain.tld:80/path?query=1#fragment'); + $this->assertInstanceOf(JsonSerializable::class, $uri); + $this->assertSame('http://domain.tld/path?query=1#fragment', $uri->jsonSerialize()); + $this->assertSame('"http:\/\/domain.tld\/path?query=1#fragment"', json_encode($uri)); + } + + public function testComponents(): void + { + $uri_str = 'http://domain.tld:80/path?query=1#fragment'; + $uri = new Uri($uri_str); + $this->assertEquals([ + 'scheme' => 'http', + 'host' => 'domain.tld', + 'port' => 80, + 'path' => '/path', + 'query' => 'query=1', + 'fragment' => 'fragment', + ], $uri->getComponents()); + $this->assertEquals(parse_url($uri_str), $uri->getComponents()); + } + + public function testQueryHelpers(): void + { + $uri = new Uri('http://domain.tld:80/path?arr%5B0%5D=arr1&arr%5B1%5D=arr2#fragment'); + $this->assertEquals([ + 'arr' => ['arr1', 'arr2'] + ], $uri->getQueryItems()); + $this->assertEquals(['arr1', 'arr2'], $uri->getQueryItem('arr')); + $uri = $uri->withQueryItems([ + 'arr' => ['arr3'], + 'assarr' => ['ass1' => 'ass1', 'ass2' => 'ass2'], + 'str' => 'str1', + ]); + $this->assertEquals([ + 'arr' => ['arr1', 'arr2', 'arr3'], + 'assarr' => ['ass1' => 'ass1', 'ass2' => 'ass2'], + 'str' => 'str1', + ], $uri->getQueryItems()); + $uri = $uri->withQueryItem('assarr', ['ass1' => 'ass1-new', 'ass3' => 'ass3']); + $this->assertEquals([ + 'arr' => ['arr1', 'arr2', 'arr3'], + 'assarr' => ['ass1' => 'ass1-new', 'ass2' => 'ass2', 'ass3' => 'ass3'], + 'str' => 'str1', + ], $uri->getQueryItems()); + $uri = $uri->withQueryItems([ + 'assarr' => null, + 'str' => null, + ]); + $this->assertEquals([ + 'arr' => ['arr1', 'arr2', 'arr3'], + ], $uri->getQueryItems()); + } } diff --git a/tests/UriFactoryTest.php b/tests/UriFactoryTest.php index 35fedce..5f13359 100644 --- a/tests/UriFactoryTest.php +++ b/tests/UriFactoryTest.php @@ -29,7 +29,7 @@ public function testEmpty(): void $this->assertInstanceOf(UriInterface::class, $uri); } - public function tesNontEmpty(): void + public function testNotEmpty(): void { $factory = new UriFactory(); $this->assertInstanceOf(UriFactoryInterface::class, $factory); @@ -38,7 +38,15 @@ public function tesNontEmpty(): void $this->assertInstanceOf(UriInterface::class, $uri); } - public function tesError(): void + public function testInterface(): void + { + $factory = new UriFactory(); + $src = $factory->createUri('http://user:pass@domain.tld:123/path/page.html?q=query#fragment'); + $uri = $factory->createUriFromInterface($src); + $this->assertInstanceOf(UriInterface::class, $uri); + } + + public function testError(): void { $factory = new UriFactory(); $this->assertInstanceOf(UriFactoryInterface::class, $factory);