Fixed Router/tests with PSR-7 integration
This commit is contained in:
parent
e986a76cbb
commit
2f79840f30
@ -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);
|
||||
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
namespace Ratchet\Http;
|
||||
use Ratchet\Http\Router;
|
||||
use Ratchet\WebSocket\WsServerInterface;
|
||||
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
|
||||
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
|
||||
@ -12,21 +11,17 @@ class RouterTest extends \PHPUnit_Framework_TestCase {
|
||||
protected $_router;
|
||||
protected $_matcher;
|
||||
protected $_conn;
|
||||
protected $_uri;
|
||||
protected $_req;
|
||||
|
||||
public function setUp() {
|
||||
$queryMock = $this->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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user