Skip to content

Commit 4e28440

Browse files
committed
add test
1 parent 2240da7 commit 4e28440

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Tests/View/ViewHandlerTest.php

Lines changed: 44 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;
@@ -187,6 +188,49 @@ public function testCreateResponseWithRoute()
187188
$this->assertEquals('/id/2/', $returnedResponse->headers->get('location'));
188189
}
189190

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+
190234
/**
191235
* @dataProvider createResponseWithoutLocationDataProvider
192236
*/

0 commit comments

Comments
 (0)