Skip to content

Commit

Permalink
Adding missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 14, 2025
1 parent f537e28 commit 56c43b2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
20 changes: 20 additions & 0 deletions FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

namespace League\Uri;

use InvalidArgumentException;
use League\Uri\Exceptions\SyntaxError;
use Nyholm\Psr7\Uri as NyholmUri;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Stringable;

Expand Down Expand Up @@ -549,4 +551,22 @@ public function testICanBeInstantiateFromRFC6750(): void
Uri::fromTemplate($template, $params)->getPath()
);
}

#[Test]
#[DataProvider('invalidUriWithWhitespaceProvider')]
public function it_fails_parsing_uri_string_with_whitespace(string $uri): void
{
$this->expectException(InvalidArgumentException::class);

Uri::new($uri);
}

public static function invalidUriWithWhitespaceProvider(): iterable
{
yield 'uri containing only whitespaces' => ['uri' => ' '];
yield 'uri starting with whitespaces' => ['uri' => ' https://a/b?c'];
yield 'uri ending with whitespaces' => ['uri' => 'https://a/b?c '];
yield 'uri surrounded by whitespaces' => ['uri' => ' https://a/b?c '];
yield 'uri containing whitespaces' => ['uri' => 'https://a/b ?c'];
}
}
30 changes: 28 additions & 2 deletions HttpFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,42 @@

namespace League\Uri;

use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UriFactoryInterface;

final class HttpFactoryTest extends TestCase
{
protected function createUriFactory(): UriFactoryInterface
{
return new HttpFactory();
}

public function testCreateUri(): void
{
$factory = new HttpFactory();
$uri = $factory->createUri('https://nyholm.tech/foo');
$uri = $this->createUriFactory()->createUri('https://nyholm.tech/foo');

self::assertInstanceOf(Http::class, $uri);
self::assertEquals('https://nyholm.tech/foo', $uri->__toString());
}

#[Test]
#[DataProvider('invalidUriWithWhitespaceProvider')]
public function it_fails_parsing_uri_string_with_whitespace(string $uri): void
{
$this->expectException(InvalidArgumentException::class);

$this->createUriFactory()->createUri($uri);
}

public static function invalidUriWithWhitespaceProvider(): iterable
{
yield 'uri containing only whitespaces' => ['uri' => ' '];
yield 'uri starting with whitespaces' => ['uri' => ' https://a/b?c'];
yield 'uri ending with whitespaces' => ['uri' => 'https://a/b?c '];
yield 'uri surrounded with whitespaces' => ['uri' => ' https://a/b?c '];
yield 'uri containing whitespaces' => ['uri' => 'https://a/b ?c'];
}
}

0 comments on commit 56c43b2

Please sign in to comment.