From 2f79840f3087a9c47c17210d8bcdb376ea07b537 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Thu, 11 Feb 2016 17:52:33 -0500 Subject: [PATCH] Fixed Router/tests with PSR-7 integration --- src/Ratchet/Http/Router.php | 22 +++++++------- tests/unit/Http/RouterTest.php | 55 ++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/Ratchet/Http/Router.php b/src/Ratchet/Http/Router.php index 834115a..8931b6e 100644 --- a/src/Ratchet/Http/Router.php +++ b/src/Ratchet/Http/Router.php @@ -5,6 +5,7 @@ use Psr\Http\Message\RequestInterface; use Symfony\Component\Routing\Matcher\UrlMatcherInterface; use Symfony\Component\Routing\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Exception\ResourceNotFoundException; +use GuzzleHttp\Psr7 as gPsr; class Router implements HttpServerInterface { use CloseResponseTrait; @@ -49,18 +50,15 @@ class Router implements HttpServerInterface { throw new \UnexpectedValueException('All routes must implement Ratchet\Http\HttpServerInterface'); } -// TODO: Apply Symfony default params to request -// $parameters = []; -// foreach($route as $key => $value) { -// if ((is_string($key)) && ('_' !== substr($key, 0, 1))) { -// $parameters[$key] = $value; -// } -// } -// $parameters = array_merge($parameters, gPsr\parse_query($uri->getQuery())); -// -// $url = Url::factory($request->getPath()); -// $url->setQuery($parameters); -// $request->setUrl($url); + $parameters = []; + foreach($route as $key => $value) { + if ((is_string($key)) && ('_' !== substr($key, 0, 1))) { + $parameters[$key] = $value; + } + } + $parameters = array_merge($parameters, gPsr\parse_query($uri->getQuery() ?: '')); + + $request = $request->withUri($uri->withQuery(gPsr\build_query($parameters))); $conn->controller = $route['_controller']; $conn->controller->onOpen($conn, $request); diff --git a/tests/unit/Http/RouterTest.php b/tests/unit/Http/RouterTest.php index 5a1128e..0799519 100644 --- a/tests/unit/Http/RouterTest.php +++ b/tests/unit/Http/RouterTest.php @@ -1,6 +1,5 @@ getMock('Guzzle\Http\QueryString'); - $queryMock - ->expects($this->any()) - ->method('getAll') - ->will($this->returnValue(array())); - - $this->_conn = $this->getMock('\Ratchet\ConnectionInterface'); - $this->_req = $this->getMock('\Guzzle\Http\Message\RequestInterface'); + $this->_conn = $this->getMock('\Ratchet\ConnectionInterface'); + $this->_uri = $this->getMock('Psr\Http\Message\UriInterface'); + $this->_req = $this->getMock('\Psr\Http\Message\RequestInterface'); $this->_req ->expects($this->any()) - ->method('getQuery') - ->will($this->returnValue($queryMock)); + ->method('getUri') + ->will($this->returnValue($this->_uri)); $this->_matcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface'); $this->_matcher ->expects($this->any()) @@ -34,7 +29,14 @@ class RouterTest extends \PHPUnit_Framework_TestCase { ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext'))); $this->_router = new Router($this->_matcher); - $this->_req->expects($this->any())->method('getPath')->will($this->returnValue('/whatever')); + $this->_uri->expects($this->any())->method('getPath')->will($this->returnValue('ws://doesnt.matter/')); + $this->_uri->expects($this->any())->method('withQuery')->with($this->callback(function($val) { + $this->setResult($val); + + return true; + }))->will($this->returnSelf()); + $this->_uri->expects($this->any())->method('getQuery')->will($this->returnCallback([$this, 'getResult'])); + $this->_req->expects($this->any())->method('withUri')->will($this->returnSelf()); } public function testFourOhFour() { @@ -103,38 +105,39 @@ class RouterTest extends \PHPUnit_Framework_TestCase { $controller = $this->getMockBuilder('\Ratchet\WebSocket\WsServer')->disableOriginalConstructor()->getMock(); /** @var $matcher UrlMatcherInterface */ $this->_matcher->expects($this->any())->method('match')->will( - $this->returnValue(array('_controller' => $controller, 'foo' => 'bar', 'baz' => 'qux')) + $this->returnValue(['_controller' => $controller, 'foo' => 'bar', 'baz' => 'qux']) ); $conn = $this->getMock('Ratchet\Mock\Connection'); - $request = $this->getMock('Guzzle\Http\Message\Request', array('getPath'), array('GET', 'ws://random.url'), '', false); - $request->expects($this->any())->method('getPath')->will($this->returnValue('ws://doesnt.matter/')); - - $request->setHeaderFactory($this->getMock('Guzzle\Http\Message\Header\HeaderFactoryInterface')); - $request->setUrl('ws://doesnt.matter/'); - $router = new Router($this->_matcher); - $router->onOpen($conn, $request); + $router->onOpen($conn, $this->_req); - $this->assertEquals(array('foo' => 'bar', 'baz' => 'qux'), $request->getQuery()->getAll()); + $this->assertEquals('foo=bar&baz=qux', $this->_req->getUri()->getQuery()); } public function testQueryParams() { $controller = $this->getMockBuilder('\Ratchet\WebSocket\WsServer')->disableOriginalConstructor()->getMock(); $this->_matcher->expects($this->any())->method('match')->will( - $this->returnValue(array('_controller' => $controller, 'foo' => 'bar', 'baz' => 'qux')) + $this->returnValue(['_controller' => $controller, 'foo' => 'bar', 'baz' => 'qux']) ); $conn = $this->getMock('Ratchet\Mock\Connection'); - $request = $this->getMock('Guzzle\Http\Message\Request', array('getPath'), array('GET', ''), '', false); + $request = $this->getMock('Psr\Http\Message\RequestInterface'); + $uri = new \GuzzleHttp\Psr7\Uri('ws://doesnt.matter/endpoint?hello=world&foo=nope'); - $request->setHeaderFactory($this->getMock('Guzzle\Http\Message\Header\HeaderFactoryInterface')); - $request->setUrl('ws://doesnt.matter?hello=world&foo=nope'); + $request->expects($this->any())->method('getUri')->will($this->returnCallback(function() use (&$uri) { + return $uri; + })); + $request->expects($this->any())->method('withUri')->with($this->callback(function($url) use (&$uri) { + $uri = $url; + + return true; + }))->will($this->returnSelf()); $router = new Router($this->_matcher); $router->onOpen($conn, $request); - $this->assertEquals(array('foo' => 'nope', 'baz' => 'qux', 'hello' => 'world'), $request->getQuery()->getAll()); + $this->assertEquals('foo=nope&baz=qux&hello=world', $request->getUri()->getQuery()); } }