From 4d533773f0884703e003de3e799b95a788fc4b8e Mon Sep 17 00:00:00 2001 From: adriansuter Date: Tue, 30 Apr 2019 11:16:56 +0200 Subject: [PATCH] Add test cases for \Slim\Psr7\Uri --- tests/UriTest.php | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/UriTest.php b/tests/UriTest.php index a1c1ea6..bef85ce 100644 --- a/tests/UriTest.php +++ b/tests/UriTest.php @@ -12,6 +12,7 @@ use InvalidArgumentException; use PHPUnit\Framework\TestCase; use Slim\Psr7\Uri; +use stdClass; class UriTest extends TestCase { @@ -119,6 +120,26 @@ public function testWithHost() $this->assertEquals('slimframework.com', $uri->getHost()); } + public function testWithHostValidObject() + { + $mock = $this->getMockBuilder('UriTestHost')->setMethods(['__toString'])->getMock(); + $mock->expects($this->once()) + ->method('__toString') + ->will($this->returnValue('host.test')); + + $uri = $this->uriFactory()->withHost($mock); + $this->assertEquals('host.test', $uri->getHost()); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage Uri host must be a string + */ + public function testWithHostInvalidObject() + { + $this->uriFactory()->withHost(new stdClass()); + } + public function testFilterHost() { $uri = new Uri('http', '2001:db8::1'); @@ -261,6 +282,17 @@ public function testWithQueryEmpty() $this->assertEquals('', $uri->getQuery()); } + public function testWithQueryValidObject() + { + $mock = $this->getMockBuilder('UriTestQuery')->setMethods(['__toString'])->getMock(); + $mock->expects($this->once()) + ->method('__toString') + ->will($this->returnValue('xyz=123')); + + $uri = $this->uriFactory()->withQuery($mock); + $this->assertEquals('xyz=123', $uri->getQuery()); + } + public function testFilterQuery() { $uri = $this->uriFactory()->withQuery('?foobar=%match'); @@ -303,6 +335,24 @@ public function testWithFragmentEmpty() $this->assertEquals('', $uri->getFragment()); } + public function testWithFragmentValidObject() + { + $mock = $this->getMockBuilder('UriTestFragment')->setMethods(['__toString'])->getMock(); + $mock->expects($this->once()) + ->method('__toString') + ->will($this->returnValue('other-fragment')); + + $uri = $this->uriFactory()->withFragment($mock); + $this->assertEquals('other-fragment', $uri->getFragment()); + } + + public function testWithFragmentUrlEncode() + { + $uri = $this->uriFactory()->withFragment('^a'); + + $this->assertEquals('%5Ea', $uri->getFragment()); + } + /** * @expectedException InvalidArgumentException * @expectedExceptionMessage Uri fragment must be a string