/** * @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); }
/** * @expectedException \Symfony\Component\HttpKernel\Exception\HttpException */ public function testHandleNotSupported() { $viewHandler = new ViewHandler(array()); $container = $this->getMock('\\Symfony\\Component\\DependencyInjection\\Container', array('get')); $container->expects($this->once())->method('get')->with('request')->will($this->returnValue(new Request())); $viewHandler->setContainer($container); $data = array('foo' => 'bar'); $view = new View($data); $viewHandler->handle($view); }
/** * @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()->setGroups(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(); $container = $this->getMock('Symfony\\Component\\DependencyInjection\\Container', array('get')); $container->expects($this->once())->method('get')->with('fos_rest.serializer')->will($this->returnValue($serializer)); $viewHandler = new ViewHandler(array()); $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->once())->method('get')->with('fos_rest.serializer')->will($this->returnValue($serializer2)); $viewHandler = new ViewHandler(array()); $viewHandler->setContainer($container2); $view2 = new View($exceptionWrapper); $response2 = $viewHandler->createResponse($view2, new Request(), $format); $this->assertEquals($response->getContent(), $response2->getContent()); }
/** * @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(['status_code' => 400, 'message' => 'Validation Failed', 'errors' => $form]); $view = new View($exceptionWrapper); $view->getSerializationContext()->addGroups(['Custom']); $wrapperHandler = new ExceptionWrapperSerializeHandler(); $translatorMock = $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface', ['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', ['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([]); $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', ['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([]); $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()); }