Router implements MiddlewareInterface; cleanup Router test

This commit is contained in:
PJ Dietz 2020-08-14 07:49:33 -04:00
parent 56503da35e
commit 997582f8d7
2 changed files with 35 additions and 24 deletions

View File

@ -6,10 +6,11 @@ use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use WellRESTed\Dispatching\Dispatcher; use WellRESTed\Dispatching\Dispatcher;
use WellRESTed\Dispatching\DispatcherInterface; use WellRESTed\Dispatching\DispatcherInterface;
use WellRESTed\MiddlewareInterface;
use WellRESTed\Routing\Route\Route; use WellRESTed\Routing\Route\Route;
use WellRESTed\Routing\Route\RouteFactory; use WellRESTed\Routing\Route\RouteFactory;
class Router class Router implements MiddlewareInterface
{ {
/** @var string|null Attribute name for matched path variables */ /** @var string|null Attribute name for matched path variables */
private $pathVariablesAttributeName; private $pathVariablesAttributeName;
@ -61,10 +62,16 @@ class Router
$this->stack = []; $this->stack = [];
} }
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
* @return ResponseInterface
*/
public function __invoke( public function __invoke(
ServerRequestInterface $request, ServerRequestInterface $request,
ResponseInterface $response, ResponseInterface $response,
callable $next $next
): ResponseInterface { ): ResponseInterface {
// Use only the path for routing. // Use only the path for routing.
$requestTarget = parse_url($request->getRequestTarget(), PHP_URL_PATH); $requestTarget = parse_url($request->getRequestTarget(), PHP_URL_PATH);

View File

@ -51,7 +51,7 @@ class RouterTest extends TestCase
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// Construction // Construction
public function testCreatesInstance() public function testCreatesInstance(): void
{ {
$router = new Router(); $router = new Router();
$this->assertNotNull($router); $this->assertNotNull($router);
@ -60,14 +60,14 @@ class RouterTest extends TestCase
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// Populating // Populating
public function testCreatesRouteForTarget() public function testCreatesRouteForTarget(): void
{ {
$this->router->register('GET', '/', 'middleware'); $this->router->register('GET', '/', 'middleware');
$this->factory->create('/')->shouldHaveBeenCalled(); $this->factory->create('/')->shouldHaveBeenCalled();
} }
public function testDoesNotRecreateRouteForExistingTarget() public function testDoesNotRecreateRouteForExistingTarget(): void
{ {
$this->router->register('GET', '/', 'middleware'); $this->router->register('GET', '/', 'middleware');
$this->router->register('POST', '/', 'middleware'); $this->router->register('POST', '/', 'middleware');
@ -75,7 +75,7 @@ class RouterTest extends TestCase
$this->factory->create('/')->shouldHaveBeenCalledTimes(1); $this->factory->create('/')->shouldHaveBeenCalledTimes(1);
} }
public function testPassesMethodAndMiddlewareToRoute() public function testPassesMethodAndMiddlewareToRoute(): void
{ {
$this->router->register('GET', '/', 'middleware'); $this->router->register('GET', '/', 'middleware');
@ -85,7 +85,7 @@ class RouterTest extends TestCase
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// Dispatching // Dispatching
public function testDispatchesStaticRoute() public function testDispatchesStaticRoute(): void
{ {
$target = '/'; $target = '/';
$this->request = $this->request->withRequestTarget($target); $this->request = $this->request->withRequestTarget($target);
@ -100,7 +100,7 @@ class RouterTest extends TestCase
->shouldHaveBeenCalled(); ->shouldHaveBeenCalled();
} }
public function testDispatchesPrefixRoute() public function testDispatchesPrefixRoute(): void
{ {
$target = '/animals/cats/*'; $target = '/animals/cats/*';
$this->request = $this->request->withRequestTarget('/animals/cats/molly'); $this->request = $this->request->withRequestTarget('/animals/cats/molly');
@ -115,7 +115,7 @@ class RouterTest extends TestCase
->shouldHaveBeenCalled(); ->shouldHaveBeenCalled();
} }
public function testDispatchesPatternRoute() public function testDispatchesPatternRoute(): void
{ {
$target = '/'; $target = '/';
$this->request = $this->request->withRequestTarget($target); $this->request = $this->request->withRequestTarget($target);
@ -131,7 +131,7 @@ class RouterTest extends TestCase
->shouldHaveBeenCalled(); ->shouldHaveBeenCalled();
} }
public function testDispatchesStaticRouteBeforePrefixRoute() public function testDispatchesStaticRouteBeforePrefixRoute(): void
{ {
$staticRoute = $this->prophesize(Route::class); $staticRoute = $this->prophesize(Route::class);
$staticRoute->register(Argument::cetera()); $staticRoute->register(Argument::cetera());
@ -158,7 +158,7 @@ class RouterTest extends TestCase
->shouldHaveBeenCalled(); ->shouldHaveBeenCalled();
} }
public function testDispatchesLongestMatchingPrefixRoute() public function testDispatchesLongestMatchingPrefixRoute(): void
{ {
// Note: The longest route is also good for 2 points in Settlers of Catan. // Note: The longest route is also good for 2 points in Settlers of Catan.
@ -188,7 +188,7 @@ class RouterTest extends TestCase
->shouldHaveBeenCalled(); ->shouldHaveBeenCalled();
} }
public function testDispatchesPrefixRouteBeforePatternRoute() public function testDispatchesPrefixRouteBeforePatternRoute(): void
{ {
$prefixRoute = $this->prophesize(Route::class); $prefixRoute = $this->prophesize(Route::class);
$prefixRoute->register(Argument::cetera()); $prefixRoute->register(Argument::cetera());
@ -215,7 +215,7 @@ class RouterTest extends TestCase
->shouldHaveBeenCalled(); ->shouldHaveBeenCalled();
} }
public function testDispatchesFirstMatchingPatternRoute() public function testDispatchesFirstMatchingPatternRoute(): void
{ {
$patternRoute1 = $this->prophesize(Route::class); $patternRoute1 = $this->prophesize(Route::class);
$patternRoute1->register(Argument::cetera()); $patternRoute1->register(Argument::cetera());
@ -246,7 +246,7 @@ class RouterTest extends TestCase
->shouldHaveBeenCalled(); ->shouldHaveBeenCalled();
} }
public function testStopsTestingPatternsAfterFirstSuccessfulMatch() public function testStopsTestingPatternsAfterFirstSuccessfulMatch(): void
{ {
$patternRoute1 = $this->prophesize(Route::class); $patternRoute1 = $this->prophesize(Route::class);
$patternRoute1->register(Argument::cetera()); $patternRoute1->register(Argument::cetera());
@ -277,7 +277,7 @@ class RouterTest extends TestCase
->shouldNotHaveBeenCalled(); ->shouldNotHaveBeenCalled();
} }
public function testMatchesPathAgainstRouteWithoutQuery() public function testMatchesPathAgainstRouteWithoutQuery(): void
{ {
$target = '/my/path?cat=molly&dog=bear'; $target = '/my/path?cat=molly&dog=bear';
@ -296,8 +296,12 @@ class RouterTest extends TestCase
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// Path Variables // Path Variables
/** @dataProvider pathVariableProvider */ /**
public function testSetPathVariablesAttributeIndividually($name, $value) * @dataProvider pathVariableProvider
* @param string $name
* @param string $value
*/
public function testSetPathVariablesAttributeIndividually(string $name, string $value): void
{ {
$target = '/'; $target = '/';
$variables = [ $variables = [
@ -325,7 +329,7 @@ class RouterTest extends TestCase
)->shouldHaveBeenCalled(); )->shouldHaveBeenCalled();
} }
public function pathVariableProvider() public function pathVariableProvider(): array
{ {
return [ return [
['id', '1024'], ['id', '1024'],
@ -333,7 +337,7 @@ class RouterTest extends TestCase
]; ];
} }
public function testSetPathVariablesAttributeAsArray() public function testSetPathVariablesAttributeAsArray(): void
{ {
$attributeName = 'pathVariables'; $attributeName = 'pathVariables';
@ -367,21 +371,21 @@ class RouterTest extends TestCase
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// No Match // No Match
public function testWhenNoRouteMatchesByDefaultResponds404() public function testWhenNoRouteMatchesByDefaultResponds404(): void
{ {
$this->request = $this->request->withRequestTarget('/no/match'); $this->request = $this->request->withRequestTarget('/no/match');
$response = $this->router->__invoke($this->request, $this->response, $this->next); $response = $this->router->__invoke($this->request, $this->response, $this->next);
$this->assertEquals(404, $response->getStatusCode()); $this->assertEquals(404, $response->getStatusCode());
} }
public function testWhenNoRouteMatchesByDefaultDoesNotPropagatesToNextMiddleware() public function testWhenNoRouteMatchesByDefaultDoesNotPropagatesToNextMiddleware(): void
{ {
$this->request = $this->request->withRequestTarget('/no/match'); $this->request = $this->request->withRequestTarget('/no/match');
$this->router->__invoke($this->request, $this->response, $this->next); $this->router->__invoke($this->request, $this->response, $this->next);
$this->assertFalse($this->next->called); $this->assertFalse($this->next->called);
} }
public function testWhenNoRouteMatchesAndContinueModePropagatesToNextMiddleware() public function testWhenNoRouteMatchesAndContinueModePropagatesToNextMiddleware(): void
{ {
$this->request = $this->request->withRequestTarget('/no/match'); $this->request = $this->request->withRequestTarget('/no/match');
$this->router->continueOnNotFound(); $this->router->continueOnNotFound();
@ -392,7 +396,7 @@ class RouterTest extends TestCase
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// Middleware for Routes // Middleware for Routes
public function testCallsRouterMiddlewareBeforeRouteMiddleware() public function testCallsRouterMiddlewareBeforeRouteMiddleware(): void
{ {
$middlewareRequest = new ServerRequest(); $middlewareRequest = new ServerRequest();
$middlewareResponse = new Response(); $middlewareResponse = new Response();
@ -416,7 +420,7 @@ class RouterTest extends TestCase
)->shouldHaveBeenCalled(); )->shouldHaveBeenCalled();
} }
public function testDoesNotCallRouterMiddlewareWhenNoRouteMatches() public function testDoesNotCallRouterMiddlewareWhenNoRouteMatches(): void
{ {
$middlewareCalled = false; $middlewareCalled = false;
$middlewareRequest = new ServerRequest(); $middlewareRequest = new ServerRequest();