diff --git a/src/Uri.php b/src/Uri.php index 379d710..b0a14f0 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -82,6 +82,10 @@ public static function parse(string $uri): Uri $scheme = Str\toLower($parts['scheme'] ?? ''); + if ('' === $scheme) { + throw new InvalidUri('Uri must have a scheme'); + } + return new self( $scheme, $parts['user'] ?? '', diff --git a/tests/UriTest.php b/tests/UriTest.php index 862ebe4..c9698f3 100644 --- a/tests/UriTest.php +++ b/tests/UriTest.php @@ -27,7 +27,7 @@ class UriTest extends TestCase { /** - * @dataProvider getUris + * @dataProvider getValidUris * * @throws InvalidUri */ @@ -36,7 +36,18 @@ public function testItParsesUris(string $uri, string $parsed): void self::assertSame($parsed, Uri::parse($uri)->toStr()); } - public function getUris(): array + /** + * @dataProvider getInvalidUris + * + * @throws InvalidUri + */ + public function testInvalidUris(string $uri): void + { + $this->expectException(InvalidUri::class); + Uri::parse($uri); + } + + public function getValidUris(): array { return [ ['https://example.com', 'https://example.com'], @@ -46,4 +57,11 @@ public function getUris(): array ['mailto:John.Doe@example.com', 'mailto:John.Doe@example.com'], ]; } + + public function getInvalidUris(): array + { + return [ + ['://example.com/protocol-relative-url'], + ]; + } }