|
16 | 16 | use FOS\RestBundle\View\ViewHandler; |
17 | 17 | use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; |
18 | 18 | use FOS\Rest\Util\Codes; |
| 19 | +use Symfony\Component\DependencyInjection\Container; |
19 | 20 | use Symfony\Component\HttpFoundation\Request; |
20 | 21 | use Symfony\Component\HttpFoundation\Response; |
21 | 22 | use Symfony\Component\Form\FormView; |
@@ -187,6 +188,49 @@ public function testCreateResponseWithRoute() |
187 | 188 | $this->assertEquals('/id/2/', $returnedResponse->headers->get('location')); |
188 | 189 | } |
189 | 190 |
|
| 191 | + public function testShouldReturnErrorResponseWhenDataContainsFormAndFormIsNotValid() |
| 192 | + { |
| 193 | + $container = new Container(); |
| 194 | + |
| 195 | + $serializer = $this->getMock('JMS\Serializer\Serializer', array(), array(), '', false); |
| 196 | + $serializer |
| 197 | + ->expects($this->once()) |
| 198 | + ->method('serialize') |
| 199 | + ->will($this->returnCallback(function($data) { |
| 200 | + return serialize($data); |
| 201 | + })) |
| 202 | + ; |
| 203 | + |
| 204 | + $container->set('fos_rest.serializer', $serializer); |
| 205 | + $container->set('fos_rest.view.exception_wrapper_handler', new ExceptionWrapperHandler()); |
| 206 | + $container->setParameter('fos_rest.serializer.exclusion_strategy.groups', 'foo'); |
| 207 | + $container->setParameter('fos_rest.serializer.exclusion_strategy.version', '1.0'); |
| 208 | + $container->setParameter('fos_rest.serializer.serialize_null', false); |
| 209 | + |
| 210 | + //test |
| 211 | + $viewHandler = new ViewHandler(null, $expectedFailedValidationCode = Codes::HTTP_I_AM_A_TEAPOT); |
| 212 | + $viewHandler->setContainer($container); |
| 213 | + |
| 214 | + $form = $this->getMock('Symfony\\Component\\Form\\Form', array('createView', 'getData', 'isValid', 'isBound'), array(), '', false); |
| 215 | + $form |
| 216 | + ->expects($this->any()) |
| 217 | + ->method('isValid') |
| 218 | + ->will($this->returnValue(false)); |
| 219 | + $form |
| 220 | + ->expects($this->any()) |
| 221 | + ->method('isBound') |
| 222 | + ->will($this->returnValue(true)); |
| 223 | + |
| 224 | + $view = new View($form); |
| 225 | + $response = $viewHandler->createResponse($view, new Request, 'json'); |
| 226 | + |
| 227 | + $data = unserialize($response->getContent()); |
| 228 | + $this->assertInstanceOf('FOS\\RestBundle\\Util\\ExceptionWrapper', $data); |
| 229 | + $this->assertEquals('Validation Failed', $this->readAttribute($data, 'message')); |
| 230 | + $this->assertInstanceOf('Symfony\\Component\\Form\\Form', $this->readAttribute($data, 'errors')); |
| 231 | + $this->assertEquals($expectedFailedValidationCode, $this->readAttribute($data, 'code')); |
| 232 | + } |
| 233 | + |
190 | 234 | /** |
191 | 235 | * @dataProvider createResponseWithoutLocationDataProvider |
192 | 236 | */ |
|
0 commit comments