From 67d18a224a78f0ee12a17422db51483732e7f2a8 Mon Sep 17 00:00:00 2001 From: fenric Date: Sun, 7 Oct 2018 17:34:34 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=8D=D0=BA?= =?UTF-8?q?=D1=81=D0=B5=D0=BF=D1=88=D0=BD,=20=D0=B0=D0=B2=D1=82=D0=BE?= =?UTF-8?q?=D0=BC=D0=B0=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=BE=D0=B5=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B5=D0=BE=D0=B1=D1=80=D0=B0=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5=20=D1=81=D1=85=D0=B5=D0=BC=D1=8B=20?= =?UTF-8?q?=D0=B8=20=D1=85=D0=BE=D1=81=D1=82=D0=B0=20=D0=B2=20=D0=BD=D0=B8?= =?UTF-8?q?=D0=B6=D0=BD=D0=B8=D0=B9=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82?= =?UTF-8?q?=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Uri.php | 36 ++++++++--------- src/UriException.php | 25 ++++++++++++ tests/UriTest.php | 96 ++++++++++++++++++++++++++------------------ 3 files changed, 101 insertions(+), 56 deletions(-) create mode 100644 src/UriException.php diff --git a/src/Uri.php b/src/Uri.php index 122ab01..490d691 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -126,7 +126,7 @@ public function __construct(string $uri) /** * {@inheritDoc} * - * @throws \InvalidArgumentException + * @throws \Sunrise\Uri\UriException * * @link https://tools.ietf.org/html/rfc3986#section-3.1 */ @@ -136,10 +136,10 @@ public function setScheme(string $scheme) : UriInterface if (! \preg_match($regex, $scheme)) { - throw new \InvalidArgumentException('Invalid URI component "scheme"'); + throw new UriException('Invalid URI component "scheme"'); } - $this->scheme = $scheme; + $this->scheme = \strtolower($scheme); return $this; } @@ -147,7 +147,7 @@ public function setScheme(string $scheme) : UriInterface /** * {@inheritDoc} * - * @throws \InvalidArgumentException + * @throws \Sunrise\Uri\UriException * * @link https://tools.ietf.org/html/rfc3986#section-3.2.1 */ @@ -157,7 +157,7 @@ public function setUsername(string $username) : UriInterface if (! \preg_match($regex, $username)) { - throw new \InvalidArgumentException('Invalid URI component "username"'); + throw new UriException('Invalid URI component "username"'); } $this->username = $username; @@ -168,7 +168,7 @@ public function setUsername(string $username) : UriInterface /** * {@inheritDoc} * - * @throws \InvalidArgumentException + * @throws \Sunrise\Uri\UriException * * @link https://tools.ietf.org/html/rfc3986#section-3.2.1 */ @@ -178,7 +178,7 @@ public function setPassword(string $password) : UriInterface if (! \preg_match($regex, $password)) { - throw new \InvalidArgumentException('Invalid URI component "password"'); + throw new UriException('Invalid URI component "password"'); } $this->password = $password; @@ -189,7 +189,7 @@ public function setPassword(string $password) : UriInterface /** * {@inheritDoc} * - * @throws \InvalidArgumentException + * @throws \Sunrise\Uri\UriException * * @link https://tools.ietf.org/html/rfc3986#section-3.2.2 */ @@ -199,10 +199,10 @@ public function setHost(string $host) : UriInterface if (! \preg_match($regex, $host)) { - throw new \InvalidArgumentException('Invalid URI component "host"'); + throw new UriException('Invalid URI component "host"'); } - $this->host = $host; + $this->host = \strtolower($host); return $this; } @@ -210,7 +210,7 @@ public function setHost(string $host) : UriInterface /** * {@inheritDoc} * - * @throws \InvalidArgumentException + * @throws \Sunrise\Uri\UriException */ public function setPort(?int $port) : UriInterface { @@ -219,7 +219,7 @@ public function setPort(?int $port) : UriInterface if (! ($port === null || ($port >= $min && $port <= $max))) { - throw new \InvalidArgumentException('Invalid URI component "port"'); + throw new UriException('Invalid URI component "port"'); } $this->port = $port; @@ -230,7 +230,7 @@ public function setPort(?int $port) : UriInterface /** * {@inheritDoc} * - * @throws \InvalidArgumentException + * @throws \Sunrise\Uri\UriException * * @link https://tools.ietf.org/html/rfc3986#section-3.3 */ @@ -240,7 +240,7 @@ public function setPath(string $path) : UriInterface if (! \preg_match($regex, $path)) { - throw new \InvalidArgumentException('Invalid URI component "path"'); + throw new UriException('Invalid URI component "path"'); } $this->path = $path; @@ -251,7 +251,7 @@ public function setPath(string $path) : UriInterface /** * {@inheritDoc} * - * @throws \InvalidArgumentException + * @throws \Sunrise\Uri\UriException * * @link https://tools.ietf.org/html/rfc3986#section-3.4 */ @@ -261,7 +261,7 @@ public function setQuery(string $query) : UriInterface if (! \preg_match($regex, $query)) { - throw new \InvalidArgumentException('Invalid URI component "query"'); + throw new UriException('Invalid URI component "query"'); } $this->query = $query; @@ -272,7 +272,7 @@ public function setQuery(string $query) : UriInterface /** * {@inheritDoc} * - * @throws \InvalidArgumentException + * @throws \Sunrise\Uri\UriException * * @link https://tools.ietf.org/html/rfc3986#section-3.5 */ @@ -282,7 +282,7 @@ public function setFragment(string $fragment) : UriInterface if (! \preg_match($regex, $fragment)) { - throw new \InvalidArgumentException('Invalid URI component "fragment"'); + throw new UriException('Invalid URI component "fragment"'); } $this->fragment = $fragment; diff --git a/src/UriException.php b/src/UriException.php new file mode 100644 index 0000000..d1c6e52 --- /dev/null +++ b/src/UriException.php @@ -0,0 +1,25 @@ + + * @copyright Copyright (c) 2018, Anatoly Fenric + * @license https://github.com/sunrise-php/uri/blob/master/LICENSE + * @link https://github.com/sunrise-php/uri + */ + +namespace Sunrise\Uri; + +/** + * Import classes + */ +use RuntimeException; + +/** + * UriException + * + * @package Sunrise\Uri + */ +class UriException extends RuntimeException +{} diff --git a/tests/UriTest.php b/tests/UriTest.php index fc99836..62b5972 100644 --- a/tests/UriTest.php +++ b/tests/UriTest.php @@ -3,11 +3,11 @@ namespace Sunrise\Uri\Tests; use PHPUnit\Framework\TestCase; -use Sunrise\Uri\{Uri, UriInterface}; +use Sunrise\Uri\{Uri, UriException, UriInterface}; class UriTest extends TestCase { - public const TEST_URI = 'scheme://username:password@localhost:8080/path?query#fragment'; + public const TEST_URI = 'scheme://username:password@localhost:3000/path?query#fragment'; // Constructor... @@ -52,7 +52,7 @@ public function testGetPort() { $uri = new Uri(self::TEST_URI); - $this->assertEquals(8080, $uri->getPort()); + $this->assertEquals(3000, $uri->getPort()); } public function testGetPath() @@ -82,72 +82,72 @@ public function testSetScheme() { $uri = new Uri(self::TEST_URI); - $uri->setScheme('redefinedScheme'); + $uri->setScheme('new-scheme'); - $this->assertEquals('redefinedScheme', $uri->getScheme()); + $this->assertEquals('new-scheme', $uri->getScheme()); } public function testSetUsername() { $uri = new Uri(self::TEST_URI); - $uri->setUsername('redefinedUsername'); + $uri->setUsername('new-username'); - $this->assertEquals('redefinedUsername', $uri->getUsername()); + $this->assertEquals('new-username', $uri->getUsername()); } public function testSetPassword() { $uri = new Uri(self::TEST_URI); - $uri->setPassword('redefinedPassword'); + $uri->setPassword('new-password'); - $this->assertEquals('redefinedPassword', $uri->getPassword()); + $this->assertEquals('new-password', $uri->getPassword()); } public function testSetHost() { $uri = new Uri(self::TEST_URI); - $uri->setHost('redefinedHost'); + $uri->setHost('new-host'); - $this->assertEquals('redefinedHost', $uri->getHost()); + $this->assertEquals('new-host', $uri->getHost()); } public function testSetPort() { $uri = new Uri(self::TEST_URI); - $uri->setPort(666); + $uri->setPort(80); - $this->assertEquals(666, $uri->getPort()); + $this->assertEquals(80, $uri->getPort()); } public function testSetPath() { $uri = new Uri(self::TEST_URI); - $uri->setPath('/redefinedPath'); + $uri->setPath('/new-path'); - $this->assertEquals('/redefinedPath', $uri->getPath()); + $this->assertEquals('/new-path', $uri->getPath()); } public function testSetQuery() { $uri = new Uri(self::TEST_URI); - $uri->setQuery('redefinedQuery'); + $uri->setQuery('new-query'); - $this->assertEquals('redefinedQuery', $uri->getQuery()); + $this->assertEquals('new-query', $uri->getQuery()); } public function testSetFragment() { $uri = new Uri(self::TEST_URI); - $uri->setFragment('redefinedFragment'); + $uri->setFragment('new-fragment'); - $this->assertEquals('redefinedFragment', $uri->getFragment()); + $this->assertEquals('new-fragment', $uri->getFragment()); } // Setters with empty data... @@ -228,43 +228,43 @@ public function testSetEmptyFragment() public function testSetInvalidScheme() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UriException::class); $uri = new Uri(self::TEST_URI); - $uri->setScheme(':invalidScheme:'); + $uri->setScheme('scheme://'); } public function testSetInvalidUsername() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UriException::class); $uri = new Uri(self::TEST_URI); - $uri->setUsername(':invalidUsername:'); + $uri->setUsername('username:password'); } public function testSetInvalidPassword() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UriException::class); $uri = new Uri(self::TEST_URI); - $uri->setPassword(':invalidPassword:'); + $uri->setPassword('username:password:'); } public function testSetInvalidHost() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UriException::class); $uri = new Uri(self::TEST_URI); - $uri->setHost(':invalidHost:'); + $uri->setHost('localhost:80'); } public function testSetInvalidPortWhichIsLessThanZero() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UriException::class); $uri = new Uri(self::TEST_URI); @@ -273,7 +273,7 @@ public function testSetInvalidPortWhichIsLessThanZero() public function testSetInvalidPortWhichIsZero() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UriException::class); $uri = new Uri(self::TEST_URI); @@ -282,7 +282,7 @@ public function testSetInvalidPortWhichIsZero() public function testSetInvalidPortWhichIsGreaterThanTheMaximum() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UriException::class); $uri = new Uri(self::TEST_URI); @@ -291,29 +291,29 @@ public function testSetInvalidPortWhichIsGreaterThanTheMaximum() public function testSetInvalidPath() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UriException::class); $uri = new Uri(self::TEST_URI); - $uri->setPath('?invalidPath?'); + $uri->setPath('/path?query'); } public function testSetInvalidQuery() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UriException::class); $uri = new Uri(self::TEST_URI); - $uri->setQuery('#invalidQuery#'); + $uri->setQuery('query#fragment'); } public function testSetInvalidFragment() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UriException::class); $uri = new Uri(self::TEST_URI); - $uri->setFragment('#invalidFragment#'); + $uri->setFragment('fragment#another-fragment'); } // Builds... @@ -329,14 +329,14 @@ public function testBuildHostPort() { $uri = new Uri(self::TEST_URI); - $this->assertEquals('localhost:8080', $uri->getHostPort()); + $this->assertEquals('localhost:3000', $uri->getHostPort()); } public function testBuildAuthority() { $uri = new Uri(self::TEST_URI); - $this->assertEquals('username:password@localhost:8080', $uri->getAuthority()); + $this->assertEquals('username:password@localhost:3000', $uri->getAuthority()); } public function testBuildFullUri() @@ -345,4 +345,24 @@ public function testBuildFullUri() $this->assertEquals(self::TEST_URI, $uri->toString()); } + + // Normalizes... + + public function testNormalizeSchemeToLowerCase() + { + $uri = new Uri(self::TEST_URI); + + $uri->setScheme('SCHEME'); + + $this->assertEquals('scheme', $uri->getScheme()); + } + + public function testNormalizeHostToLowerCase() + { + $uri = new Uri(self::TEST_URI); + + $uri->setHost('LOCALHOST'); + + $this->assertEquals('localhost', $uri->getHost()); + } }