Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sirn-se committed Feb 3, 2024
1 parent b2bcc83 commit 795b18b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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];
Expand Down
64 changes: 64 additions & 0 deletions tests/UriExtensionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UriInterface;
use JsonSerializable;
use Stringable;

class UriExtensionsTest extends TestCase
{
Expand Down Expand Up @@ -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());
}
}
12 changes: 10 additions & 2 deletions tests/UriFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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:[email protected]: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);
Expand Down

0 comments on commit 795b18b

Please sign in to comment.