示例#1
0
文件: ViewTest.php 项目: hykz/Depot
 public function testSetFormat()
 {
     $view = new View();
     $format = 'bar';
     $view->setFormat($format);
     $this->assertEquals($format, $view->getFormat());
 }
 /**
  * @Route("/")
  */
 public function indexAction()
 {
     $view = new View();
     $view->setFormat('html');
     $view->setTemplate('TastdCoreBundle:Default:index.html.twig');
     return $view;
 }
 public function showAction(Request $request, $exception, DebugLoggerInterface $logger = null)
 {
     $view = new View();
     $view->setData(json_decode($exception->getMessage(), true));
     $view->setFormat('json');
     return $this->container->get('fos_rest.view_handler')->handle($view);
 }
 protected function createView($returnData)
 {
     $view = new View();
     $view->setData($returnData);
     $view->setStatusCode($returnData['status']);
     $view->setFormat('json');
     return $view;
 }
示例#5
0
 public function editSeccionAction($id)
 {
     $em = $this->getDoctrine()->getManager();
     $seccion = $em->getRepository('EtsiSiteBundle:Seccion')->find($id);
     $data = $this->getForm($seccion);
     $view = new View($data);
     $view->setTemplate('EtsiSiteBundle:Form:newSeccion.html.twig');
     $view->setFormat('html');
     return $this->get('fos_rest.view_handler')->handle($view);
 }
示例#6
0
 public function editContenidoAction($id)
 {
     $em = $this->getDoctrine()->getManager();
     $contenido = $em->getRepository('EtsiSiteBundle:Contenido')->find($id);
     $data = $this->getForm($contenido);
     $view = new View($data);
     $view->setTemplate('EtsiSiteBundle:Form:newContenido.html.twig');
     $view->setFormat('html');
     return $this->get('fos_rest.view_handler')->handle($view);
 }
 /**
  * Renders the parameters and template and initializes a new response object with the
  * rendered content.
  *
  * @param GetResponseForControllerResultEvent $event A GetResponseForControllerResultEvent instance
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     $configuration = $request->attributes->get('_view');
     $view = $event->getControllerResult();
     if (!$view instanceof View) {
         if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
             return parent::onKernelView($event);
         }
         $view = new View($view);
     }
     if ($configuration) {
         if ($configuration->getTemplateVar()) {
             $view->setTemplateVar($configuration->getTemplateVar());
         }
         if (null === $view->getStatusCode() && $configuration->getStatusCode()) {
             $view->setStatusCode($configuration->getStatusCode());
         }
         if ($configuration->getSerializerGroups()) {
             $context = $view->getSerializationContext() ?: new SerializationContext();
             $context->setGroups($configuration->getSerializerGroups());
             $view->setSerializationContext($context);
         }
     }
     if (null === $view->getFormat()) {
         $view->setFormat($request->getRequestFormat());
     }
     $vars = $request->attributes->get('_template_vars');
     if (!$vars) {
         $vars = $request->attributes->get('_template_default_vars');
     }
     $viewHandler = $this->container->get('fos_rest.view_handler');
     if ($viewHandler->isFormatTemplating($view->getFormat())) {
         if (!empty($vars)) {
             $parameters = (array) $viewHandler->prepareTemplateParameters($view);
             foreach ($vars as $var) {
                 if (!array_key_exists($var, $parameters)) {
                     $parameters[$var] = $request->attributes->get($var);
                 }
             }
             $view->setData($parameters);
         }
         $template = $request->attributes->get('_template');
         if ($template) {
             if ($template instanceof TemplateReference) {
                 $template->set('format', null);
             }
             $view->setTemplate($template);
         }
     }
     $response = $viewHandler->handle($view, $request);
     $event->setResponse($response);
 }
示例#8
0
 /**
  * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
  * @dataProvider getCallbackFailureDataProvider
  */
 public function testGetCallbackFailure(Request $request)
 {
     $data = ['foo' => 'bar'];
     $viewHandler = new ViewHandler($this->router, $this->serializer, $this->templating, $this->requestStack, $this->exceptionWrapperHandler, ['jsonp' => false]);
     $jsonpHandler = new JsonpHandler('callback');
     $viewHandler->registerHandler('jsonp', [$jsonpHandler, 'createResponse']);
     $this->serializer->expects($this->once())->method('serialize')->will($this->returnValue(var_export($data, true)));
     $data = ['foo' => 'bar'];
     $view = new View($data);
     $view->setFormat('jsonp');
     $viewHandler->handle($view, $request);
 }
示例#9
0
 /**
  * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
  * @dataProvider getCallbackFailureDataProvider
  */
 public function testGetCallbackFailure(Request $request)
 {
     $data = array('foo' => 'bar');
     $viewHandler = new ViewHandler(array('jsonp' => false));
     $jsonpHandler = new JsonpHandler('callback');
     $viewHandler->registerHandler('jsonp', array($jsonpHandler, 'createResponse'));
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\Container', array('get', 'getParameter'));
     $serializer = $this->getMock('stdClass', array('serialize', 'setVersion'));
     $serializer->expects($this->once())->method('serialize')->will($this->returnValue(var_export($data, true)));
     $container->expects($this->once())->method('get')->with('fos_rest.serializer')->will($this->returnValue($serializer));
     $container->expects($this->any())->method('getParameter')->will($this->onConsecutiveCalls('version', '1.0'));
     $viewHandler->setContainer($container);
     $data = array('foo' => 'bar');
     $view = new View($data);
     $view->setFormat('jsonp');
     $viewHandler->handle($view, $request);
 }
 /**
  * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
  * @dataProvider getCallbackFailureDataProvider
  */
 public function testGetCallbackFailure(Request $request)
 {
     $data = ['foo' => 'bar'];
     $viewHandler = new ViewHandler(['jsonp' => false]);
     $jsonpHandler = new JsonpHandler('callback');
     $viewHandler->registerHandler('jsonp', [$jsonpHandler, 'createResponse']);
     $viewHandler->setSerializationContextAdapter($this->getMock(SerializationContextAdapterInterface::class));
     $container = $this->getMock(ContainerInterface::class);
     $serializer = $this->getMock('stdClass', ['serialize', 'setVersion']);
     $serializer->expects($this->once())->method('serialize')->will($this->returnValue(var_export($data, true)));
     $container->expects($this->any())->method('get')->with($this->equalTo('fos_rest.serializer'))->will($this->returnValue($serializer));
     $container->expects($this->any())->method('getParameter')->will($this->onConsecutiveCalls('version', '1.0'));
     $viewHandler->setContainer($container);
     $data = ['foo' => 'bar'];
     $view = new View($data);
     $view->setFormat('jsonp');
     $viewHandler->handle($view, $request);
 }
 /**
  * Renders the parameters and template and initializes a new response object with the
  * rendered content.
  *
  * @param GetResponseForControllerResultEvent $event A GetResponseForControllerResultEvent instance
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     $view = $event->getControllerResult();
     if (!$view instanceof View) {
         if (!$request->attributes->get('_view') && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
             return;
         }
         $view = new View($view);
     }
     if (null === $view->getFormat()) {
         $view->setFormat($request->getRequestFormat());
     }
     $vars = $request->attributes->get('_template_vars');
     if (!$vars) {
         $vars = $request->attributes->get('_template_default_vars');
     }
     if (!empty($vars)) {
         $parameters = $view->getData();
         if (null !== $parameters && !is_array($parameters)) {
             throw new \RuntimeException('View data must be an array if using a templating aware format.');
         }
         $parameters = (array) $parameters;
         foreach ($vars as $var) {
             if (!array_key_exists($var, $parameters)) {
                 $parameters[$var] = $request->attributes->get($var);
             }
         }
         $view->setData($parameters);
     }
     $viewHandler = $this->container->get('fos_rest.view_handler');
     if ($viewHandler->isFormatTemplating($view->getFormat())) {
         $template = $request->attributes->get('_template');
         if ($template) {
             if ($template instanceof TemplateReference) {
                 $template->set('format', null);
                 $template->set('engine', null);
             }
             $view->setTemplate($template);
         }
     }
     $response = $viewHandler->handle($view, $request);
     $event->setResponse($response);
 }
 /**
  * Renders the parameters and template and initializes a new response object with the
  * rendered content.
  *
  * @param GetResponseForControllerResultEvent $event
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     /** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
     $configuration = $request->attributes->get('_view');
     $view = $event->getControllerResult();
     $customViewDefined = true;
     if (!$view instanceof View) {
         if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
             return parent::onKernelView($event);
         }
         $view = new View($view);
         $customViewDefined = false;
     }
     if ($configuration) {
         if ($configuration->getTemplateVar()) {
             $view->setTemplateVar($configuration->getTemplateVar());
         }
         if ($configuration->getStatusCode() && (null === $view->getStatusCode() || Codes::HTTP_OK === $view->getStatusCode())) {
             $view->setStatusCode($configuration->getStatusCode());
         }
         if ($configuration->getSerializerGroups() && !$customViewDefined) {
             $context = $view->getSerializationContext() ?: new SerializationContext();
             $context->setGroups($configuration->getSerializerGroups());
             $view->setSerializationContext($context);
         }
         if ($configuration->getSerializerEnableMaxDepthChecks()) {
             $context = $view->getSerializationContext() ?: new SerializationContext();
             $context->enableMaxDepthChecks();
             $view->setSerializationContext($context);
         }
         $populateDefaultVars = $configuration->isPopulateDefaultVars();
     } else {
         $populateDefaultVars = true;
     }
     if (null === $view->getFormat()) {
         $view->setFormat($request->getRequestFormat());
     }
     $vars = $request->attributes->get('_template_vars');
     if (!$vars && $populateDefaultVars) {
         $vars = $request->attributes->get('_template_default_vars');
     }
     $viewHandler = $this->container->get('fos_rest.view_handler');
     if ($viewHandler->isFormatTemplating($view->getFormat())) {
         if (!empty($vars)) {
             $parameters = (array) $viewHandler->prepareTemplateParameters($view);
             foreach ($vars as $var) {
                 if (!array_key_exists($var, $parameters)) {
                     $parameters[$var] = $request->attributes->get($var);
                 }
             }
             $view->setData($parameters);
         }
         $template = $request->attributes->get('_template');
         if ($template) {
             if ($template instanceof TemplateReference) {
                 $template->set('format', null);
             }
             $view->setTemplate($template);
         }
     }
     $response = $viewHandler->handle($view, $request);
     $event->setResponse($response);
 }
示例#13
0
 /**
  * @dataProvider prepareTemplateParametersDataProvider
  */
 public function testPrepareTemplateParametersWithProvider($viewData, $templateData, $expected)
 {
     $handler = new ViewHandler(array('html' => true));
     $view = new View();
     $view->setFormat('html');
     $view->setData($viewData);
     if (null !== $templateData) {
         $view->setTemplateData($templateData);
     }
     $this->assertEquals($expected, $handler->prepareTemplateParameters($view));
 }
示例#14
0
 /**
  * @dataProvider prepareTemplateParametersDataProvider
  */
 public function testPrepareTemplateParametersWithProvider($viewData, $templateData, $expected)
 {
     $handler = $this->createViewHandler(['html' => true]);
     $handler->setSerializationContextAdapter($this->getMock('FOS\\RestBundle\\Context\\Adapter\\SerializationContextAdapterInterface'));
     $view = new View();
     $view->setFormat('html');
     $view->setData($viewData);
     if (null !== $templateData) {
         $view->setTemplateData($templateData);
     }
     $this->assertEquals($expected, $handler->prepareTemplateParameters($view));
 }
 public function jsonpAction()
 {
     $data = array('foo' => 'bar');
     $view = new View($data);
     $view->setFormat('jsonp');
     return $this->viewHandler->handle($view);
 }
 /**
  * Renders the parameters and template and initializes a new response object with the
  * rendered content.
  *
  * @param GetResponseForControllerResultEvent $event
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
         return false;
     }
     /** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
     $configuration = $request->attributes->get('_view');
     $view = $event->getControllerResult();
     $customViewDefined = true;
     if (!$view instanceof View) {
         if (!$configuration && !$this->forceView) {
             return;
         }
         $view = new View($view);
         $customViewDefined = false;
     }
     if ($configuration) {
         if ($configuration->getTemplateVar()) {
             $view->setTemplateVar($configuration->getTemplateVar());
         }
         if ($configuration->getStatusCode() && (null === $view->getStatusCode() || Response::HTTP_OK === $view->getStatusCode())) {
             $view->setStatusCode($configuration->getStatusCode());
         }
         $context = $view->getContext();
         if ($configuration->getSerializerGroups() && !$customViewDefined) {
             $context->addGroups($configuration->getSerializerGroups());
         }
         if ($configuration->getSerializerEnableMaxDepthChecks()) {
             $context->setMaxDepth(0);
         }
         $populateDefaultVars = $configuration->isPopulateDefaultVars();
     } else {
         $populateDefaultVars = true;
     }
     if (null === $view->getFormat()) {
         $view->setFormat($request->getRequestFormat());
     }
     $vars = $request->attributes->get('_template_vars');
     if (!$vars && $populateDefaultVars) {
         $vars = $request->attributes->get('_template_default_vars');
     }
     if ($this->viewHandler->isFormatTemplating($view->getFormat()) && !$view->getRoute() && !$view->getLocation()) {
         if (!empty($vars)) {
             $parameters = (array) $this->viewHandler->prepareTemplateParameters($view);
             foreach ($vars as $var) {
                 if (!array_key_exists($var, $parameters)) {
                     $parameters[$var] = $request->attributes->get($var);
                 }
             }
             $view->setData($parameters);
         }
         $template = null !== $configuration && $configuration->getTemplate() ? $configuration->getTemplate() : $request->attributes->get('_template');
         if ($template && !$view->getTemplate()) {
             if ($template instanceof TemplateReferenceInterface) {
                 $template->set('format', null);
             }
             $view->setTemplate($template);
         }
     }
     $response = $this->viewHandler->handle($view, $request);
     $event->setResponse($response);
 }