public function testRequestMethodGetSet() { $request = new Request(); $this->assertEquals(RequestMethod::GET, $request->getMethod(), 'Default should be GET'); $request->setMethod(RequestMethod::PATCH); $this->assertEquals(RequestMethod::PATCH, $request->getMethod()); }
/** * Based on the current configuration, begins handling the incoming request. * This function should result in data being output. * * @return Response The response that was sent. */ public function start() { $this->beforeStart(); // Begin output buffering and begin building the HTTP response $this->isBuffering = true; ob_start(); $this->response = new Response(); $this->context->registerInstance($this->response); try { // Dispatch the request to the router if (!$this->filters->trigger(Filters::BEFORE_ROUTE, $this->context)) { return $this->response; } $routingResult = $this->router->route($this->request); if ($routingResult != null) { $this->context->registerInstance($routingResult); $this->response->setResponseCode(ResponseCode::HTTP_OK); $returnValue = $this->dispatch($routingResult); // If a Response object was returned by the target code, use that object from now on if ($returnValue instanceof Response) { if ($this->isBuffering) { ob_clean(); } $this->response = $returnValue; } } else { $anyOptionsAvailable = $this->router->getOptions($this->request); if (!$anyOptionsAvailable) { // No options at all for this route, this is a true 404 $this->prepareNotFoundResponse(); } else { if ($this->request->getMethod() == RequestMethod::OPTIONS) { // Options available, no custom OPTIONS handler, generate an OPTIONS ("Allow") response. $this->prepareOptionsResponse(); } else { // There are options, but no match, this is a 405 $this->prepareNotAllowedResponse(); } } } $this->filters->trigger(Filters::AFTER_ROUTE, $this->context); } catch (\Exception $ex) { $this->prepareErrorResponse($ex); } finally { $this->sendResponse(); } return $this->response; }
public function testRouteActionClosure() { $request = new Request(); $request->setRequestUri('/dir/sample.html'); $request->setMethod(RequestMethod::GET); $route = new Route('/dir/sample.html', function (Request $request) { // Our closure should receive our use variable ($request) // Our closure should also have access to the Context via $this echo $request->getMethod(); return 'test'; }); $route->setAcceptableMethods([RequestMethod::GET]); $context = new Context(); $context->registerInstance($request); $this->assertTrue($route->matches($request)); $this->expectOutputString('GET'); $this->assertEquals('test', $route->action($context)); }