From 1d30fcbbba6547c37d808b3bc6859936f288b2c5 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Fri, 8 May 2015 01:03:07 -0400 Subject: [PATCH] Remove RouteTable, revise RouteFactory, --- src/Routing/Route/RouteFactory.php | 35 ++-- src/Routing/Route/RouteFactoryInterface.php | 8 + src/Routing/RouteTable.php | 103 ---------- src/Routing/RouteTableInterface.php | 16 -- .../unit/Routing/Route/RouteFactoryTest.php | 38 ++-- .../unit/Routing/Route/TemplateRouteTest.php | 8 +- test/tests/unit/Routing/RouteMapTest.php | 6 +- test/tests/unit/Routing/RouteTableTest.php | 179 ------------------ test/tests/unit/Routing/RouterTest.php | 121 +++++------- 9 files changed, 87 insertions(+), 427 deletions(-) delete mode 100644 src/Routing/RouteTable.php delete mode 100644 src/Routing/RouteTableInterface.php delete mode 100644 test/tests/unit/Routing/RouteTableTest.php diff --git a/src/Routing/Route/RouteFactory.php b/src/Routing/Route/RouteFactory.php index f77d519..bb31b66 100644 --- a/src/Routing/Route/RouteFactory.php +++ b/src/Routing/Route/RouteFactory.php @@ -2,7 +2,7 @@ namespace WellRESTed\Routing\Route; -use WellRESTed\Routing\RouteTableInterface; +use WellRESTed\Routing\MethodMap; /** * Class for creating routes @@ -10,20 +10,17 @@ use WellRESTed\Routing\RouteTableInterface; class RouteFactory implements RouteFactoryInterface { /** - * The method will determine the most appropriate route subclass to use - * and will forward the arguments on to the subclass's constructor. + * Creates a route for the given target. * - * - Paths with no special characters will register StaticRoutes - * - Paths ending with * will register PrefixRoutes - * - Paths containing URI variables (e.g., {id}) will register TemplateRoutes - * - Regular exressions will register RegexRoutes + * - Target with no special characters will create StaticRoutes + * - Target ending with * will create PrefixRoutes + * - Target containing URI variables (e.g., {id}) will create TemplateRoutes + * - Regular exressions will create RegexRoutes * - * @param RouteTableInterface $routeTable Table to add the route to - * @param string $target Path, prefix, or pattern to match - * @param mixed $middleware Middleware to dispatch - * @param mixed $extra Additional options to pass to a route constructor + * @param string $target Route target or target pattern + * @return RouteInterface */ - public function registerRoute(RouteTableInterface $routeTable, $target, $middleware, $extra = null) + public function create($target) { if ($target[0] === "/") { @@ -31,25 +28,19 @@ class RouteFactory implements RouteFactoryInterface // PrefixRoutes end with * if (substr($target, -1) === "*") { - // Remove the trailing *, since the PrefixRoute constructor doesn't expect it. - $target = substr($target, 0, -1); - $route = new PrefixRoute($target, $middleware); - $routeTable->addPrefixRoute($route); + return new PrefixRoute($target, new MethodMap()); } // TempalateRoutes contain {variable} if (preg_match(TemplateRoute::URI_TEMPLATE_EXPRESSION_RE, $target)) { - $route = new TemplateRoute($target, $middleware, $extra); - $routeTable->addRoute($route); + return new TemplateRoute($target, new MethodMap()); } // StaticRoute - $route = new StaticRoute($target, $middleware); - $routeTable->addStaticRoute($route); + return new StaticRoute($target, new MethodMap()); } // Regex - $route = new RegexRoute($target, $middleware); - $routeTable->addRoute($route); + return new RegexRoute($target, new MethodMap()); } } diff --git a/src/Routing/Route/RouteFactoryInterface.php b/src/Routing/Route/RouteFactoryInterface.php index 9e47ca5..87301ff 100644 --- a/src/Routing/Route/RouteFactoryInterface.php +++ b/src/Routing/Route/RouteFactoryInterface.php @@ -6,6 +6,14 @@ interface RouteFactoryInterface { /** * Creates a route for the given target. + * + * - Target with no special characters will create StaticRoutes + * - Target ending with * will create PrefixRoutes + * - Target containing URI variables (e.g., {id}) will create TemplateRoutes + * - Regular exressions will create RegexRoutes + * + * @param string $target Route target or target pattern + * @return RouteInterface */ public function create($target); } diff --git a/src/Routing/RouteTable.php b/src/Routing/RouteTable.php deleted file mode 100644 index 7f1d5f1..0000000 --- a/src/Routing/RouteTable.php +++ /dev/null @@ -1,103 +0,0 @@ -routes = []; - $this->staticRoutes = []; - $this->prefixRoutes = []; - } - - public function addRoute(RouteInterface $route) - { - $this->routes[] = $route; - } - - public function addStaticRoute(StaticRouteInterface $staticRoute) - { - $this->staticRoutes[$staticRoute->getPath()] = $staticRoute; - } - - public function addPrefixRoute(PrefixRouteInterface $prefxRoute) - { - $this->prefixRoutes[$prefxRoute->getPrefix()] = $prefxRoute; - } - - public function dispatch(ServerRequestInterface $request, ResponseInterface &$response) - { - $requestTarget = $request->getRequestTarget(); - - $route = $this->getStaticRoute($requestTarget); - if ($route) { - $route->dispatch($request, $response); - return; - } - - $route = $this->getPrefixRoute($requestTarget); - if ($route) { - $route->dispatch($request, $response); - return; - } - - // Try each of the routes. - foreach ($this->routes as $route) { - if ($route->matchesRequestTarget($requestTarget, $captures)) { - if (is_array($captures)) { - foreach ($captures as $key => $value) { - $request = $request->withAttribute($key, $value); - } - } - $route->dispatch($request, $response); - } - } - } - - private function getStaticRoute($requestTarget) - { - if (isset($this->staticRoutes[$requestTarget])) { - return $this->staticRoutes[$requestTarget]; - } - return null; - } - - private function getPrefixRoute($requestTarget) - { - // Find all prefixes that match the start of this path. - $prefixes = array_keys($this->prefixRoutes); - $matches = array_filter( - $prefixes, - function ($prefix) use ($requestTarget) { - return (strrpos($requestTarget, $prefix, -strlen($requestTarget)) !== false); - } - ); - - if ($matches) { - if (count($matches) > 0) { - // If there are multiple matches, sort them to find the one with the longest string length. - $compareByLength = function ($a, $b) { - return strlen($b) - strlen($a); - }; - usort($matches, $compareByLength); - } - $route = $this->prefixRoutes[$matches[0]]; - return $route; - } - return null; - } -} diff --git a/src/Routing/RouteTableInterface.php b/src/Routing/RouteTableInterface.php deleted file mode 100644 index fb7a826..0000000 --- a/src/Routing/RouteTableInterface.php +++ /dev/null @@ -1,16 +0,0 @@ -routeTable = $this->prophesize("\\WellRESTed\\Routing\\RouteTableInterface"); - $this->routeTable->addStaticRoute(Argument::cetera())->willReturn(); - $this->routeTable->addPrefixRoute(Argument::cetera())->willReturn(); - $this->routeTable->addRoute(Argument::cetera())->willReturn(); - } - - public function testRegistersStaticRoute() + public function testCreatesStaticRoute() { $factory = new RouteFactory(); - $factory->registerRoute($this->routeTable->reveal(), "/cats/", null); - $this->routeTable->addStaticRoute(Argument::any())->shouldHaveBeenCalled(); + $route = $factory->create("/cats/"); + $this->assertSame(RouteInterface::TYPE_STATIC, $route->getType()); } - public function testRegistersPrefixRoute() + public function testCreatesPrefixRoute() { $factory = new RouteFactory(); - $factory->registerRoute($this->routeTable->reveal(), "/cats/*", null); - $this->routeTable->addPrefixRoute(Argument::any())->shouldHaveBeenCalled(); + $route = $factory->create("/cats/*"); + $this->assertSame(RouteInterface::TYPE_PREFIX, $route->getType()); } - public function testRegistersTemplateRoute() + public function testCreatesRegexRoute() { $factory = new RouteFactory(); - $factory->registerRoute($this->routeTable->reveal(), "/cats/{catId}", null); - $this->routeTable->addRoute(Argument::type("\\WellRESTed\\Routing\\Route\\TemplateRoute"))->shouldHaveBeenCalled(); + $route = $factory->create("~/cat/[0-9]+~"); + $this->assertSame(RouteInterface::TYPE_PATTERN, $route->getType()); } - public function testRegistersRegexRoute() + public function testCreatesTemplateRoute() { $factory = new RouteFactory(); - $factory->registerRoute($this->routeTable->reveal(), "~/cat/[0-9]+~", null); - $this->routeTable->addRoute(Argument::type("\\WellRESTed\\Routing\\Route\\RegexRoute"))->shouldHaveBeenCalled(); - $this->routeTable->addRoute(Argument::type("\\WellRESTed\\Routing\\Route\\TemplateRoute"))->shouldNotHaveBeenCalled(); + $route = $factory->create("/cat/{id}"); + $this->assertSame(RouteInterface::TYPE_PATTERN, $route->getType()); } } diff --git a/test/tests/unit/Routing/Route/TemplateRouteTest.php b/test/tests/unit/Routing/Route/TemplateRouteTest.php index c2ee63c..0a89102 100644 --- a/test/tests/unit/Routing/Route/TemplateRouteTest.php +++ b/test/tests/unit/Routing/Route/TemplateRouteTest.php @@ -39,7 +39,7 @@ class TemplateRouteTest extends \PHPUnit_Framework_TestCase } /** - * @dataProvider matchingRouteProvider + * @dataProvider matchingTemplateProvider */ public function testProvidesCapturesAsRequestAttributes($template, $path, $expectedCaptures) { @@ -52,7 +52,11 @@ class TemplateRouteTest extends \PHPUnit_Framework_TestCase $route->matchesRequestTarget($path); $route->dispatch($request->reveal(), $responseReveal); - $request->withAttribute("path", $expectedCaptures)->shouldHaveBeenCalled(); + $request->withAttribute("path", Argument::that(function ($path) use ($expectedCaptures) { + return array_intersect_assoc($path, $expectedCaptures) == $expectedCaptures; + }))->shouldHaveBeenCalled(); + + //$request->withAttribute("path", $expectedCaptures)->shouldHaveBeenCalled(); } public function matchingTemplateProvider() diff --git a/test/tests/unit/Routing/RouteMapTest.php b/test/tests/unit/Routing/RouteMapTest.php index 07a0891..811c422 100644 --- a/test/tests/unit/Routing/RouteMapTest.php +++ b/test/tests/unit/Routing/RouteMapTest.php @@ -3,12 +3,12 @@ namespace WellRESTed\Test\Unit\Routing; use Prophecy\Argument; -use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\ServerRequestInterface; -use WellRESTed\Routing\MethodMapInterface; use WellRESTed\Routing\Route\RouteInterface; use WellRESTed\Routing\RouteMap; +// Test dispatch orders (static before prefix, prefix before pattern) +// Test dispatches first matching pattern route + /** * @coversDefaultClass WellRESTed\Routing\RouteMap * @uses WellRESTed\Routing\RouteMap diff --git a/test/tests/unit/Routing/RouteTableTest.php b/test/tests/unit/Routing/RouteTableTest.php deleted file mode 100644 index 0f1b04e..0000000 --- a/test/tests/unit/Routing/RouteTableTest.php +++ /dev/null @@ -1,179 +0,0 @@ -request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface"); - $this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface"); - } - - public function testMatchesStaticRoute() - { - $route = $this->prophesize("\\WellRESTed\\Routing\\Route\\StaticRouteInterface"); - $route->getPath()->willReturn("/cats/"); - $route->dispatch(Argument::cetera())->willReturn(); - - $this->request->getRequestTarget()->willReturn("/cats/"); - - $table = new RouteTable(); - $table->addStaticRoute($route->reveal()); - - $request = $this->request->reveal(); - $response = $this->response->reveal(); - $table->dispatch($request, $response); - $route->dispatch($request, $response)->shouldHaveBeenCalled(); - } - - public function testMatchesPrefixRoute() - { - $route = $this->prophesize("\\WellRESTed\\Routing\\Route\\PrefixRouteInterface"); - $route->getPrefix()->willReturn("/cats/"); - $route->dispatch(Argument::cetera())->willReturn(); - - $this->request->getRequestTarget()->willReturn("/cats/molly"); - - $table = new RouteTable(); - $table->addPrefixRoute($route->reveal()); - - $request = $this->request->reveal(); - $response = $this->response->reveal(); - $table->dispatch($request, $response); - $route->dispatch($request, $response)->shouldHaveBeenCalled(); - } - - public function testMatchesBestPrefixRoute() - { - $route1 = $this->prophesize("\\WellRESTed\\Routing\\Route\\PrefixRouteInterface"); - $route1->getPrefix()->willReturn("/animals/"); - $route1->dispatch(Argument::cetera())->willReturn(); - - $route2 = $this->prophesize("\\WellRESTed\\Routing\\Route\\PrefixRouteInterface"); - $route2->getPrefix()->willReturn("/animals/cats/"); - $route2->dispatch(Argument::cetera())->willReturn(); - - $this->request->getRequestTarget()->willReturn("/animals/cats/molly"); - - $table = new RouteTable(); - $table->addPrefixRoute($route1->reveal()); - $table->addPrefixRoute($route2->reveal()); - - $request = $this->request->reveal(); - $response = $this->response->reveal(); - $table->dispatch($request, $response); - - $route1->dispatch(Argument::cetera())->shouldNotHaveBeenCalled(); - $route2->dispatch(Argument::cetera())->shouldHaveBeenCalled(); - } - - public function testMatchesStaticRouteBeforePrefixRoute() - { - $route1 = $this->prophesize("\\WellRESTed\\Routing\\Route\\PrefixRouteInterface"); - $route1->getPrefix()->willReturn("/animals/cats/"); - $route1->dispatch(Argument::cetera())->willReturn(); - - $route2 = $this->prophesize("\\WellRESTed\\Routing\\Route\\StaticRouteInterface"); - $route2->getPath()->willReturn("/animals/cats/molly"); - $route2->dispatch(Argument::cetera())->willReturn(); - - $this->request->getRequestTarget()->willReturn("/animals/cats/molly"); - - $table = new RouteTable(); - $table->addPrefixRoute($route1->reveal()); - $table->addStaticRoute($route2->reveal()); - - $request = $this->request->reveal(); - $response = $this->response->reveal(); - $table->dispatch($request, $response); - - $route1->dispatch(Argument::cetera())->shouldNotHaveBeenCalled(); - $route2->dispatch(Argument::cetera())->shouldHaveBeenCalled(); - } - - public function testMatchesPrefixRouteBeforeRoute() - { - $route1 = $this->prophesize("\\WellRESTed\\Routing\\Route\\PrefixRouteInterface"); - $route1->getPrefix()->willReturn("/animals/cats/"); - $route1->dispatch(Argument::cetera())->willReturn(); - - $route2 = $this->prophesize("\\WellRESTed\\Routing\\Route\\RouteInterface"); - $route2->matchesRequestTarget(Argument::cetera())->willReturn(true); - - $this->request->getRequestTarget()->willReturn("/animals/cats/molly"); - - $table = new RouteTable(); - $table->addPrefixRoute($route1->reveal()); - $table->addRoute($route2->reveal()); - - $request = $this->request->reveal(); - $response = $this->response->reveal(); - $table->dispatch($request, $response); - - $route1->dispatch(Argument::cetera())->shouldHaveBeenCalled(); - $route2->dispatch(Argument::cetera())->shouldNotHaveBeenCalled(); - } - - /** - * @uses WellRESTed\Routing\Route\TemplateRoute - * @uses WellRESTed\Routing\Route\RegexRoute - * @uses WellRESTed\Routing\Route\Route - */ - public function testAddsCapturesAsRequestAttributes() - { - // This test needs to read the result of the $captures parameter which - // is passed by reference. This is not so eary so mock, so the test - // will use an actual TemplateRoute. - - $middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface"); - $route = new TemplateRoute("/cats/{id}", $middleware->reveal()); - - $this->request->withAttribute(Argument::cetera())->willReturn($this->request->reveal()); - $this->request->getRequestTarget()->willReturn("/cats/molly"); - - $table = new RouteTable(); - $table->addRoute($route); - - $request = $this->request->reveal(); - $response = $this->response->reveal(); - $table->dispatch($request, $response); - - $this->request->withAttribute("id", "molly")->shouldHaveBeenCalled(); - } - - public function testDispatchedFirstMatchingRoute() - { - $route1 = $this->prophesize("\\WellRESTed\\Routing\\Route\\RouteInterface"); - $route1->matchesRequestTarget(Argument::cetera())->willReturn(false); - - $route2 = $this->prophesize("\\WellRESTed\\Routing\\Route\\RouteInterface"); - $route2->matchesRequestTarget(Argument::cetera())->willReturn(true); - $route2->dispatch(Argument::cetera())->willReturn(); - - $route3 = $this->prophesize("\\WellRESTed\\Routing\\Route\\RouteInterface"); - $route3->matchesRequestTarget(Argument::cetera())->willReturn(false); - - $this->request->getRequestTarget()->willReturn("/"); - - $table = new RouteTable(); - $table->addRoute($route1->reveal()); - $table->addRoute($route2->reveal()); - $table->addRoute($route3->reveal()); - - $request = $this->request->reveal(); - $response = $this->response->reveal(); - $table->dispatch($request, $response); - - $route1->dispatch(Argument::cetera())->shouldNotHaveBeenCalled(); - $route2->dispatch(Argument::cetera())->shouldHaveBeenCalled(); - $route3->dispatch(Argument::cetera())->shouldNotHaveBeenCalled(); - } -} diff --git a/test/tests/unit/Routing/RouterTest.php b/test/tests/unit/Routing/RouterTest.php index d023fa2..996dd2d 100644 --- a/test/tests/unit/Routing/RouterTest.php +++ b/test/tests/unit/Routing/RouterTest.php @@ -21,14 +21,31 @@ class RouterTest extends \PHPUnit_Framework_TestCase { private $request; private $response; + private $routeMap; + private $router; public function setUp() { parent::setUp(); + $this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface'); $this->response = $this->prophesize('Psr\Http\Message\ResponseInterface'); + $this->response->hasHeader("Content-length")->willReturn(true); $this->response->getStatusCode()->willReturn(200); + + $this->routeMap = $this->prophesize('WellRESTed\Routing\RouteMapInterface'); + $this->routeMap->add(Argument::cetera())->willReturn(); + $this->routeMap->dispatch(Argument::cetera())->willReturn(); + + $this->router = $this->getMockBuilder('WellRESTed\Routing\Router') + ->setMethods(["getRouteMap"]) + ->disableOriginalConstructor() + ->getMock(); + $this->router->expects($this->any()) + ->method("getRouteMap") + ->will($this->returnValue($this->routeMap->reveal())); + $this->router->__construct(); } // ------------------------------------------------------------------------ @@ -57,24 +74,12 @@ class RouterTest extends \PHPUnit_Framework_TestCase */ public function testAddRegistersRouteWithRouteMap() { - $routeMap = $this->prophesize('WellRESTed\Routing\RouteMapInterface'); - $routeMap->add(Argument::cetera())->willReturn(); - - $router = $this->getMockBuilder('WellRESTed\Routing\Router') - ->setMethods(["getRouteMap"]) - ->disableOriginalConstructor() - ->getMock(); - $router->expects($this->any()) - ->method("getRouteMap") - ->will($this->returnValue($routeMap->reveal())); - $router->__construct(); - $method = "GET"; $target = "/path/{id}"; $middleware = "Middleware"; - $router->add($method, $target, $middleware); - $routeMap->add($method, $target, $middleware)->shouldHaveBeenCalled(); + $this->router->add($method, $target, $middleware); + $this->routeMap->add($method, $target, $middleware)->shouldHaveBeenCalled(); } /** @@ -82,23 +87,10 @@ class RouterTest extends \PHPUnit_Framework_TestCase */ public function testDispatchesRouteMap() { - $routeMap = $this->prophesize('WellRESTed\Routing\RouteMapInterface'); - $routeMap->dispatch(Argument::cetera())->willReturn(); - - $router = $this->getMockBuilder('WellRESTed\Routing\Router') - ->setMethods(["getRouteMap"]) - ->disableOriginalConstructor() - ->getMock(); - $router->expects($this->any()) - ->method("getRouteMap") - ->will($this->returnValue($routeMap->reveal())); - $router->__construct(); - $request = $this->request->reveal(); $resonse = $this->response->reveal(); - $router->dispatch($request, $resonse); - - $routeMap->dispatch($request, Argument::any())->shouldHaveBeenCalled(); + $this->router->dispatch($request, $resonse); + $this->routeMap->dispatch($request, Argument::any())->shouldHaveBeenCalled(); } // ------------------------------------------------------------------------ @@ -113,12 +105,11 @@ class RouterTest extends \PHPUnit_Framework_TestCase $hook = $this->prophesize('\WellRESTed\Routing\MiddlewareInterface'); $hook->dispatch(Argument::cetera())->willReturn(); - $router = new Router(); - $router->addPreRouteHook($hook->reveal()); + $this->router->addPreRouteHook($hook->reveal()); $request = $this->request->reveal(); $response = $this->response->reveal(); - $router->dispatch($request, $response); + $this->router->dispatch($request, $response); $hook->dispatch(Argument::cetera())->shouldHaveBeenCalled(); } @@ -135,12 +126,11 @@ class RouterTest extends \PHPUnit_Framework_TestCase $statusMiddleware = $this->prophesize('\WellRESTed\Routing\MiddlewareInterface'); $statusMiddleware->dispatch(Argument::cetera())->willReturn(); - $router = new Router(); - $router->setStatusHook(403, $statusMiddleware->reveal()); + $this->router->setStatusHook(403, $statusMiddleware->reveal()); $request = $this->request->reveal(); $response = $this->response->reveal(); - $router->dispatch($request, $response); + $this->router->dispatch($request, $response); $statusMiddleware->dispatch(Argument::cetera())->shouldHaveBeenCalled(); } @@ -150,13 +140,12 @@ class RouterTest extends \PHPUnit_Framework_TestCase * @covers ::dispatchStatusHooks * @covers ::setStatusHook */ - public function testDispatchesStatusHookForHttpException() + public function testConvertsHttpExceptionToResponse() { $statusMiddleware = $this->prophesize('\WellRESTed\Routing\MiddlewareInterface'); $statusMiddleware->dispatch(Argument::cetera())->willReturn(); - $routeMap = $this->prophesize('WellRESTed\Routing\RouteMapInterface'); - $routeMap->dispatch(Argument::cetera())->willThrow(new NotFoundException()); + $this->routeMap->dispatch(Argument::cetera())->willThrow(new NotFoundException()); $this->response->withStatus(Argument::any())->will( function ($args) { @@ -166,18 +155,9 @@ class RouterTest extends \PHPUnit_Framework_TestCase ); $this->response->withBody(Argument::any())->willReturn($this->response->reveal()); - $router = $this->getMockBuilder('WellRESTed\Routing\Router') - ->setMethods(["getRouteMap"]) - ->disableOriginalConstructor() - ->getMock(); - $router->expects($this->any()) - ->method("getRouteMap") - ->will($this->returnValue($routeMap->reveal())); - $router->__construct(); - $request = $this->request->reveal(); $response = $this->response->reveal(); - $router->dispatch($request, $response); + $this->router->dispatch($request, $response); $this->response->withStatus(404)->shouldHaveBeenCalled(); } @@ -191,12 +171,11 @@ class RouterTest extends \PHPUnit_Framework_TestCase $hook = $this->prophesize('\WellRESTed\Routing\MiddlewareInterface'); $hook->dispatch(Argument::cetera())->willReturn(); - $router = new Router(); - $router->addPostRouteHook($hook->reveal()); + $this->router->addPostRouteHook($hook->reveal()); $request = $this->request->reveal(); $response = $this->response->reveal(); - $router->dispatch($request, $response); + $this->router->dispatch($request, $response); $hook->dispatch(Argument::cetera())->shouldHaveBeenCalled(); } @@ -210,12 +189,11 @@ class RouterTest extends \PHPUnit_Framework_TestCase $hook = $this->prophesize('\WellRESTed\Routing\MiddlewareInterface'); $hook->dispatch(Argument::cetera())->willReturn(); - $router = new Router(); - $router->addFinalizationHook($hook->reveal()); + $this->router->addFinalizationHook($hook->reveal()); $request = $this->request->reveal(); $response = $this->response->reveal(); - $router->dispatch($request, $response); + $this->router->dispatch($request, $response); $hook->dispatch(Argument::cetera())->shouldHaveBeenCalled(); } @@ -228,34 +206,23 @@ class RouterTest extends \PHPUnit_Framework_TestCase // Each middleware will push a value onto this array. $stack = []; - $routeMap = $this->prophesize('WellRESTed\Routing\RouteMapInterface'); - $response = $this->response; - $routeMap->dispatch(Argument::cetera())->will(function () use ($response, &$stack) { + $this->routeMap->dispatch(Argument::cetera())->will(function () use ($response, &$stack) { $stack[] = "routeMap"; $response->getStatusCode()->willReturn(404); }); - $router = $this->getMockBuilder('WellRESTed\Routing\Router') - ->setMethods(["getRouteMap"]) - ->disableOriginalConstructor() - ->getMock(); - $router->expects($this->any()) - ->method("getRouteMap") - ->will($this->returnValue($routeMap->reveal())); - $router->__construct(); - - $router->addPreRouteHook($this->createStackHook("pre1", $stack)); - $router->addPreRouteHook($this->createStackHook("pre2", $stack)); - $router->addPostRouteHook($this->createStackHook("post1", $stack)); - $router->addPostRouteHook($this->createStackHook("post2", $stack)); - $router->addFinalizationHook($this->createStackHook("final1", $stack)); - $router->addFinalizationHook($this->createStackHook("final2", $stack)); - $router->setStatusHook(404, $this->createStackHook("404", $stack)); + $this->router->addPreRouteHook($this->createStackHook("pre1", $stack)); + $this->router->addPreRouteHook($this->createStackHook("pre2", $stack)); + $this->router->addPostRouteHook($this->createStackHook("post1", $stack)); + $this->router->addPostRouteHook($this->createStackHook("post2", $stack)); + $this->router->addFinalizationHook($this->createStackHook("final1", $stack)); + $this->router->addFinalizationHook($this->createStackHook("final2", $stack)); + $this->router->setStatusHook(404, $this->createStackHook("404", $stack)); $request = $this->request->reveal(); $response = $this->response->reveal(); - $router->dispatch($request, $response); + $this->router->dispatch($request, $response); $this->assertEquals(["pre1","pre2","routeMap","404","post1","post2","final1","final2"], $stack); } @@ -287,11 +254,10 @@ class RouterTest extends \PHPUnit_Framework_TestCase } ); $this->response->withBody(Argument::any())->willReturn($this->response->reveal()); - $router = new Router(); $request = $this->request->reveal(); $response = $this->response->reveal(); - $router->dispatch($request, $response); + $this->router->dispatch($request, $response); $this->response->withHeader(Argument::cetera())->shouldHaveBeenCalled(); } @@ -314,11 +280,10 @@ class RouterTest extends \PHPUnit_Framework_TestCase } ); $this->response->withBody(Argument::any())->willReturn($this->response->reveal()); - $router = new Router(); $request = $this->request->reveal(); $response = $this->response->reveal(); - $router->dispatch($request, $response); + $this->router->dispatch($request, $response); $this->response->withBody(Argument::that(function ($body) { return $body->getSize() === 0;