/**
  * @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']);
     $viewHandler->setSerializationContextAdapter($this->getMock(SerializationContextAdapterInterface::class));
     $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);
 }
 /**
  * @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('FOS\\RestBundle\\Context\\Adapter\\SerializationContextAdapterInterface'));
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\Container', ['get', 'getParameter']);
     $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);
 }
Esempio n. 3
0
 /**
  * @dataProvider exceptionWrapperSerializeResponseContentProvider
  * @param string $format
  */
 public function testCreateResponseWithFormErrorsAndSerializationGroups($format)
 {
     $form = Forms::createFormFactory()->createBuilder()->add('name', 'text')->add('description', 'text')->getForm();
     $form->get('name')->addError(new FormError('Invalid name'));
     $exceptionWrapper = new ExceptionWrapper(array('status_code' => 400, 'message' => 'Validation Failed', 'errors' => $form));
     $view = new View($exceptionWrapper);
     $view->getSerializationContext()->addGroups(array('Custom'));
     $wrapperHandler = new ExceptionWrapperSerializeHandler();
     $translatorMock = $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface', array('trans', 'transChoice', 'setLocale', 'getLocale'));
     $translatorMock->expects($this->any())->method('trans')->will($this->returnArgument(0));
     $formErrorHandler = new FormErrorHandler($translatorMock);
     $serializer = SerializerBuilder::create()->configureHandlers(function (HandlerRegistry $handlerRegistry) use($wrapperHandler, $formErrorHandler) {
         $handlerRegistry->registerSubscribingHandler($wrapperHandler);
         $handlerRegistry->registerSubscribingHandler($formErrorHandler);
     })->build();
     $adapter = $this->getMock('FOS\\RestBundle\\Context\\Adapter\\SerializationContextAdapterInterface');
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\Container', array('get'));
     $container->expects($this->any())->method('get')->with($this->logicalOr($this->equalTo('fos_rest.serializer'), $this->equalTo('fos_rest.context.adapter.chain_context_adapter')))->will($this->returnCallback(function ($method) use($serializer, $adapter) {
         switch ($method) {
             case 'fos_rest.serializer':
                 return $serializer;
             case 'fos_rest.context.adapter.chain_context_adapter':
                 return $adapter;
         }
     }));
     $viewHandler = new ViewHandler(array());
     $viewHandler->setSerializationContextAdapter($this->getMock('FOS\\RestBundle\\Context\\Adapter\\SerializationContextAdapterInterface'));
     $viewHandler->setContainer($container);
     $response = $viewHandler->createResponse($view, new Request(), $format);
     $serializer2 = SerializerBuilder::create()->configureHandlers(function (HandlerRegistry $handlerRegistry) use($wrapperHandler, $formErrorHandler) {
         $handlerRegistry->registerSubscribingHandler($formErrorHandler);
     })->build();
     $container2 = $this->getMock('Symfony\\Component\\DependencyInjection\\Container', array('get'));
     $container2->expects($this->any())->method('get')->with($this->logicalOr($this->equalTo('fos_rest.serializer'), $this->equalTo('fos_rest.context.adapter.chain_context_adapter')))->will($this->returnCallback(function ($method) use($serializer2, $adapter) {
         switch ($method) {
             case 'fos_rest.serializer':
                 return $serializer2;
             case 'fos_rest.context.adapter.chain_context_adapter':
                 return $adapter;
         }
     }));
     $viewHandler = new ViewHandler(array());
     $viewHandler->setSerializationContextAdapter($this->getMock('FOS\\RestBundle\\Context\\Adapter\\SerializationContextAdapterInterface'));
     $viewHandler->setContainer($container2);
     $view2 = new View($exceptionWrapper);
     $response2 = $viewHandler->createResponse($view2, new Request(), $format);
     $this->assertEquals($response->getContent(), $response2->getContent());
 }