Skip to content

Commit 7a8b93f

Browse files
committed
Fix form error response code
1 parent aa5a6ee commit 7a8b93f

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

Tests/View/ViewHandlerTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use FOS\RestBundle\View\ViewHandler;
1717
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
1818
use FOS\Rest\Util\Codes;
19+
use Symfony\Component\DependencyInjection\Container;
1920
use Symfony\Component\HttpFoundation\Request;
2021
use Symfony\Component\HttpFoundation\Response;
2122
use Symfony\Component\Form\FormView;
@@ -288,6 +289,58 @@ function ($method) use ($serializer) {
288289
->will($this->returnValueMap($map));
289290
}
290291

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+
291344
public static function createResponseWithoutLocationDataProvider()
292345
{
293346
return array(

View/ViewHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ private function getDataFromView(View $view)
454454
return $exceptionWrapperHandler->wrap(
455455
array(
456456
'status' => 'error',
457-
'status_code' => $view->getResponse()->getStatusCode(),
457+
'status_code' => $this->failedValidationCode,
458458
'status_text' => Response::$statusTexts[$view->getResponse()->getStatusCode()],
459459
'currentContent' => '',
460460
'message' => 'Validation Failed',

0 commit comments

Comments
 (0)