/** * @covers ::__construct * @covers ::getResponse * @covers ::setResponse */ public function testControllerResponse() { $responseText = 'Lorem ipsum dolor sit amet'; $response = new ControllerResponse($responseText); $this->assertEquals($responseText, $response->getResponse()); $responseText2 = 'Lipsum dolor amet adipiscit'; $response->setResponse($responseText2); $this->assertEquals($responseText2, $response->getResponse()); }
/** * Execute the given controller. * * @param string $name Name of the controller or route assigned to this controller. * @param string $class Class name of the controller. * @param string $method Method name to execute on the controller. * @param array $arguments [optional] Arguments to execute the controller with. * @return Response */ protected function renderController($name, $class, $method, array $arguments = array(), Request $request = null) { $eventManager = $this->container->get('event_manager'); $controller = new $class($this->container); $willRespondEvent = new ControllerWillRespond($name, $controller, $method, $arguments); $eventManager->trigger($willRespondEvent); $method = $willRespondEvent->getMethod(); $arguments = $willRespondEvent->getArguments(); $controllerResponse = new ControllerResponse(call_user_func_array(array($controller, $method), $arguments)); $eventManager->trigger(new ControllerDidRespond($controllerResponse, $name, $controller, $method, $arguments, $request)); $response = $controllerResponse->getResponse(); $this->logger->debug('Executed controller: "{name}"', array('name' => $name, 'function' => $class . '::' . $method, 'arguments' => $arguments, '_timer' => $this->container->get('splot.timer')->step('Matched route'))); // special case, if the response is a string then automatically convert it to HttpResponse if (is_string($response)) { $response = new Response($response); } if (!is_object($response) || !$response instanceof Response) { throw new InvalidReturnValueException('Executed controller method must return ' . Response::class . ' instance or a string, "' . Debugger::getType($response) . '" given.'); } return $response; }