$response = $d->dispatch('GET', '/closure/Mike'); expect($response->getContent())->toBe('Hello Mike'); }); it("dispatches a class based route", function () { $d = new Dispatcher(null, $this->routes); $response = $d->dispatch('GET', '/class/Mike'); expect($response->getContent())->toBe('Hello Mike'); }); it("throws a RuntimeException is a method is not found in a class name string", function () { $closure = function () { $d = new Dispatcher(null, $this->routes); return $d->dispatch('GET', '/class-no-method/Mike'); }; expect($closure)->toThrow(new RuntimeException()); }); it("throws a RouteNotFoundException for a route that is not in the collection", function () { $closure = function () { $d = new Dispatcher(null, $this->routes); return $d->dispatch('GET', '/not/found'); }; expect($closure)->toThrow(new \Siphon\Route\Exception\RouteNotFoundException()); }); it("throws a MethodNotAllowedException method type does not match", function () { $closure = function () { $d = new Dispatcher(null, $this->routes); return $d->dispatch('POST', '/class/Mike'); }; expect($closure)->toThrow(new \Siphon\Route\Exception\MethodNotAllowedException(['POST'])); }); }); });
/** * Handle the request * * @param \Symfony\Component\HttpFoundation\Request $request * @param int $type * @param bool $catch * @return \Symfony\Component\HttpFoundation\Response * * @throws \Exception */ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) { $this->set('Symfony\\Component\\HttpFoundation\\Request', $request); $dispatcher = new RouteDispatcher($this, $this['router']->getRoutes()); try { $this['event']->trigger('request.received', [$request]); $response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo()); $this['event']->trigger('response.created', [$request, $response]); return $response; } catch (\Exception $e) { if (!$catch) { throw $e; } return $this['exception.handler']->handleException($e); } }