Fixed Router/tests with PSR-7 integration

This commit is contained in:
Chris Boden 2016-02-11 17:52:33 -05:00
parent e986a76cbb
commit 2f79840f30
2 changed files with 39 additions and 38 deletions

View File

@ -5,6 +5,7 @@ use Psr\Http\Message\RequestInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface; use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use GuzzleHttp\Psr7 as gPsr;
class Router implements HttpServerInterface { class Router implements HttpServerInterface {
use CloseResponseTrait; use CloseResponseTrait;
@ -49,18 +50,15 @@ class Router implements HttpServerInterface {
throw new \UnexpectedValueException('All routes must implement Ratchet\Http\HttpServerInterface'); throw new \UnexpectedValueException('All routes must implement Ratchet\Http\HttpServerInterface');
} }
// TODO: Apply Symfony default params to request $parameters = [];
// $parameters = []; foreach($route as $key => $value) {
// foreach($route as $key => $value) { if ((is_string($key)) && ('_' !== substr($key, 0, 1))) {
// if ((is_string($key)) && ('_' !== substr($key, 0, 1))) { $parameters[$key] = $value;
// $parameters[$key] = $value; }
// } }
// } $parameters = array_merge($parameters, gPsr\parse_query($uri->getQuery() ?: ''));
// $parameters = array_merge($parameters, gPsr\parse_query($uri->getQuery()));
// $request = $request->withUri($uri->withQuery(gPsr\build_query($parameters)));
// $url = Url::factory($request->getPath());
// $url->setQuery($parameters);
// $request->setUrl($url);
$conn->controller = $route['_controller']; $conn->controller = $route['_controller'];
$conn->controller->onOpen($conn, $request); $conn->controller->onOpen($conn, $request);

View File

@ -1,6 +1,5 @@
<?php <?php
namespace Ratchet\Http; namespace Ratchet\Http;
use Ratchet\Http\Router;
use Ratchet\WebSocket\WsServerInterface; use Ratchet\WebSocket\WsServerInterface;
use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface; use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
@ -12,21 +11,17 @@ class RouterTest extends \PHPUnit_Framework_TestCase {
protected $_router; protected $_router;
protected $_matcher; protected $_matcher;
protected $_conn; protected $_conn;
protected $_uri;
protected $_req; protected $_req;
public function setUp() { public function setUp() {
$queryMock = $this->getMock('Guzzle\Http\QueryString'); $this->_conn = $this->getMock('\Ratchet\ConnectionInterface');
$queryMock $this->_uri = $this->getMock('Psr\Http\Message\UriInterface');
->expects($this->any()) $this->_req = $this->getMock('\Psr\Http\Message\RequestInterface');
->method('getAll')
->will($this->returnValue(array()));
$this->_conn = $this->getMock('\Ratchet\ConnectionInterface');
$this->_req = $this->getMock('\Guzzle\Http\Message\RequestInterface');
$this->_req $this->_req
->expects($this->any()) ->expects($this->any())
->method('getQuery') ->method('getUri')
->will($this->returnValue($queryMock)); ->will($this->returnValue($this->_uri));
$this->_matcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface'); $this->_matcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
$this->_matcher $this->_matcher
->expects($this->any()) ->expects($this->any())
@ -34,7 +29,14 @@ class RouterTest extends \PHPUnit_Framework_TestCase {
->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext'))); ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')));
$this->_router = new Router($this->_matcher); $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() { public function testFourOhFour() {
@ -103,38 +105,39 @@ class RouterTest extends \PHPUnit_Framework_TestCase {
$controller = $this->getMockBuilder('\Ratchet\WebSocket\WsServer')->disableOriginalConstructor()->getMock(); $controller = $this->getMockBuilder('\Ratchet\WebSocket\WsServer')->disableOriginalConstructor()->getMock();
/** @var $matcher UrlMatcherInterface */ /** @var $matcher UrlMatcherInterface */
$this->_matcher->expects($this->any())->method('match')->will( $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'); $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 = 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() { public function testQueryParams() {
$controller = $this->getMockBuilder('\Ratchet\WebSocket\WsServer')->disableOriginalConstructor()->getMock(); $controller = $this->getMockBuilder('\Ratchet\WebSocket\WsServer')->disableOriginalConstructor()->getMock();
$this->_matcher->expects($this->any())->method('match')->will( $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'); $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->expects($this->any())->method('getUri')->will($this->returnCallback(function() use (&$uri) {
$request->setUrl('ws://doesnt.matter?hello=world&foo=nope'); 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 = new Router($this->_matcher);
$router->onOpen($conn, $request); $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());
} }
} }