|
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; |
@@ -288,6 +289,58 @@ function ($method) use ($serializer) { |
288 | 289 | ->will($this->returnValueMap($map)); |
289 | 290 | } |
290 | 291 |
|
| 292 | + public function testShouldReturnErrorResponseWhenDataContainsFormAndFormIsNotValid() |
| 293 | + { |
| 294 | + $container = new Container(); |
| 295 | + |
| 296 | + $serializer = $this->getMock('JMS\Serializer\Serializer', array(), array(), '', false); |
| 297 | + $serializer |
| 298 | + ->expects($this->once()) |
| 299 | + ->method('serialize') |
| 300 | + ->will( |
| 301 | + $this->returnCallback( |
| 302 | + function ($data) { |
| 303 | + return serialize($data); |
| 304 | + } |
| 305 | + ) |
| 306 | + ); |
| 307 | + |
| 308 | + $container->set('fos_rest.serializer', $serializer); |
| 309 | + $container->set('fos_rest.view.exception_wrapper_handler', new ExceptionWrapperHandler()); |
| 310 | + $container->setParameter('fos_rest.serializer.exclusion_strategy.groups', 'foo'); |
| 311 | + $container->setParameter('fos_rest.serializer.exclusion_strategy.version', '1.0'); |
| 312 | + $container->setParameter('fos_rest.serializer.serialize_null', false); |
| 313 | + |
| 314 | + //test |
| 315 | + $viewHandler = new ViewHandler(null, $expectedFailedValidationCode = Codes::HTTP_I_AM_A_TEAPOT); |
| 316 | + $viewHandler->setContainer($container); |
| 317 | + |
| 318 | + $form = $this->getMock( |
| 319 | + 'Symfony\\Component\\Form\\Form', |
| 320 | + array('createView', 'getData', 'isValid', 'isBound'), |
| 321 | + array(), |
| 322 | + '', |
| 323 | + false |
| 324 | + ); |
| 325 | + $form |
| 326 | + ->expects($this->any()) |
| 327 | + ->method('isValid') |
| 328 | + ->will($this->returnValue(false)); |
| 329 | + $form |
| 330 | + ->expects($this->any()) |
| 331 | + ->method('isBound') |
| 332 | + ->will($this->returnValue(true)); |
| 333 | + |
| 334 | + $view = new View($form); |
| 335 | + $response = $viewHandler->createResponse($view, new Request, 'json'); |
| 336 | + |
| 337 | + $data = unserialize($response->getContent()); |
| 338 | + $this->assertInstanceOf('FOS\\RestBundle\\Util\\ExceptionWrapper', $data); |
| 339 | + $this->assertEquals('Validation Failed', $this->readAttribute($data, 'message')); |
| 340 | + $this->assertInstanceOf('Symfony\\Component\\Form\\Form', $this->readAttribute($data, 'errors')); |
| 341 | + $this->assertEquals($expectedFailedValidationCode, $this->readAttribute($data, 'code')); |
| 342 | + } |
| 343 | + |
291 | 344 | public static function createResponseWithoutLocationDataProvider() |
292 | 345 | { |
293 | 346 | return array( |
|
0 commit comments