From 506c37ffdd398b1712254e964eb3e8f3d3135e7b Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Thu, 2 Apr 2015 19:54:49 -0400 Subject: [PATCH] Add MiddlewareInterface and Dispatcher --- src/Routing/Dispatcher.php | 27 ++++++++ src/Routing/MiddlewareInterface.php | 11 ++++ test/tests/unit/Routing/DispatcherTest.php | 77 ++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 src/Routing/Dispatcher.php create mode 100644 src/Routing/MiddlewareInterface.php create mode 100644 test/tests/unit/Routing/DispatcherTest.php diff --git a/src/Routing/Dispatcher.php b/src/Routing/Dispatcher.php new file mode 100644 index 0000000..0869c08 --- /dev/null +++ b/src/Routing/Dispatcher.php @@ -0,0 +1,27 @@ +dispatch($request, $response); + } + } +} diff --git a/src/Routing/MiddlewareInterface.php b/src/Routing/MiddlewareInterface.php new file mode 100644 index 0000000..50b3c9a --- /dev/null +++ b/src/Routing/MiddlewareInterface.php @@ -0,0 +1,11 @@ +request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface"); + $this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface"); + $this->response->withStatus(Argument::any())->willReturn($this->response->reveal()); + } + + public function testDispatchedCallable() + { + $middleware = function ($request, &$response) { + $response = $response->withStatus(200); + }; + $dispatcher = new Dispatcher(); + $response = $this->response->reveal(); + $dispatcher->dispatch($middleware, $this->request->reveal(), $response); + $this->response->withStatus(200)->shouldHaveBeenCalled(); + $this->assertSame($this->response->reveal(), $response); + } + + public function testDispatchedFromCallable() + { + $middleware = function () { + return new DispatcherTest_Middleware(); + }; + $response = $this->response->reveal(); + $dispatcher = new Dispatcher(); + $dispatcher->dispatch($middleware, $this->request->reveal(), $response); + $this->response->withStatus(200)->shouldHaveBeenCalled(); + $this->assertSame($this->response->reveal(), $response); + } + + public function testDispatchedFromString() + { + $middleware = __NAMESPACE__ . "\\DispatcherTest_Middleware"; + $response = $this->response->reveal(); + $dispatcher = new Dispatcher(); + $dispatcher->dispatch($middleware, $this->request->reveal(), $response); + $this->response->withStatus(200)->shouldHaveBeenCalled(); + $this->assertSame($this->response->reveal(), $response); + } + + public function testDispatchedInstance() + { + $middleware = new DispatcherTest_Middleware(); + $dispatcher = new Dispatcher(); + $response = $this->response->reveal(); + $dispatcher->dispatch($middleware, $this->request->reveal(), $response); + $this->response->withStatus(200)->shouldHaveBeenCalled(); + $this->assertSame($this->response->reveal(), $response); + } +} + +class DispatcherTest_Middleware implements MiddlewareInterface +{ + public function dispatch(ServerRequestInterface $request, ResponseInterface &$response) + { + $response = $response->withStatus(200); + } +}