Skip to content

Commit

Permalink
Merge 73bdcf4 into 6239022
Browse files Browse the repository at this point in the history
  • Loading branch information
pgk committed Feb 7, 2019
2 parents 6239022 + 73bdcf4 commit fb1ce4e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/Dispatcher.php
Expand Up @@ -28,7 +28,9 @@ class Dispatcher extends GroupCountBasedDispatcher implements
*/
public function dispatchRequest(ServerRequestInterface $request) : ResponseInterface
{
$match = $this->dispatch($request->getMethod(), $request->getUri()->getPath());
$httpMethod = $request->getMethod();
$uri = $request->getUri()->getPath();
$match = $this->dispatch($httpMethod, $uri);

switch ($match[0]) {
case FastRoute::NOT_FOUND:
Expand All @@ -39,14 +41,32 @@ public function dispatchRequest(ServerRequestInterface $request) : ResponseInter
$this->setMethodNotAllowedDecoratorMiddleware($allowed);
break;
case FastRoute::FOUND:
$match[1]->setVars($match[2]);
$this->setFoundMiddleware($match[1]);
$route = $this->ensureHandlerIsRoute($match[1], $httpMethod, $uri)->setVars($match[2]);
$this->setFoundMiddleware($route);
break;
}

return $this->handle($request);
}

/**
* Ensure handler is a Route, honoring the contract of dispatchRequest.
*
* @param Route|mixed $matchingHandler
* @param string $httpMethod
* @param string $uri
*
* @return Route
*
*/
private function ensureHandlerIsRoute($matchingHandler, $httpMethod, $uri) : Route
{
if (is_a($matchingHandler, Route::class)) {
return $matchingHandler;
}
return new Route($httpMethod, $uri, $matchingHandler);
}

/**
* {@inheritdoc}
*/
Expand Down
44 changes: 44 additions & 0 deletions tests/DispatchIntegrationTest.php
Expand Up @@ -901,4 +901,48 @@ public function process(

$router->dispatch($request);
}

public function testDispatchDoesNotThrowWhenUsingAddRoute()
{
$request = $this->createMock(ServerRequestInterface::class);
$response = $this->createMock(ResponseInterface::class);
$uri = $this->createMock(UriInterface::class);

$uri
->expects($this->exactly(2))
->method('getPath')
->will($this->returnValue('/example/route'))
;

$request
->expects($this->once())
->method('getMethod')
->will($this->returnValue('GET'))
;

$request
->expects($this->exactly(2))
->method('getUri')
->will($this->returnValue($uri))
;

$router = new Router;

$router->addRoute(['GET', 'POST'], '/example/{something}', function (
ServerRequestInterface $request,
array $args
) use (
$response
) : ResponseInterface {
$this->assertSame([
'something' => 'route'
], $args);

return $response;
});

$returnedResponse = $router->dispatch($request);

$this->assertSame($response, $returnedResponse);
}
}

0 comments on commit fb1ce4e

Please sign in to comment.