request = new ServerRequest(); $this->response = new Response(); $this->next = new NextMock(); } public function testDispatchesCallableThatReturnsResponse() { $middleware = function ($request, $response, $next) { return $next($request, $response->withStatus(200)); }; $dispatcher = new Dispatcher(); $response = $dispatcher->dispatch($middleware, $this->request, $this->response, $this->next); $this->assertEquals(200, $response->getStatusCode()); } public function testDispatchesMiddlewareInstanceFromCallable() { $middleware = function () { return new DispatcherTest_Middleware(); }; $dispatcher = new Dispatcher(); $response = $dispatcher->dispatch($middleware, $this->request, $this->response, $this->next); $this->assertEquals(200, $response->getStatusCode()); } public function testDispatchesMiddlewareFromClassNameString() { $middleware = __NAMESPACE__ . '\DispatcherTest_Middleware'; $dispatcher = new Dispatcher(); $response = $dispatcher->dispatch($middleware, $this->request, $this->response, $this->next); $this->assertEquals(200, $response->getStatusCode()); } public function testDispatchesMiddlewareInstance() { $middleware = new DispatcherTest_Middleware(); $dispatcher = new Dispatcher(); $response = $dispatcher->dispatch($middleware, $this->request, $this->response, $this->next); $this->assertEquals(200, $response->getStatusCode()); } public function testDispatchesArrayAsDispatchStack() { $middleware = new DispatcherTest_Middleware(); $dispatcher = new Dispatcher(); $response = $dispatcher->dispatch([$middleware], $this->request, $this->response, $this->next); $this->assertEquals(200, $response->getStatusCode()); } /** * @expectedException \WellRESTed\Dispatching\DispatchException */ public function testThrowsExceptionWhenUnableToDispatch() { $middleware = null; $dispatcher = new Dispatcher(); $dispatcher->dispatch($middleware, $this->request, $this->response, $this->next); } } class DispatcherTest_Middleware implements MiddlewareInterface { public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next) { $response = $response->withStatus(200); return $next($request, $response); } }