|
11 | 11 |
|
12 | 12 | namespace FOS\RestBundle\Tests\View; |
13 | 13 |
|
| 14 | +use FOS\RestBundle\Serializer\ExceptionWrapperSerializeHandler; |
| 15 | +use FOS\RestBundle\Util\ExceptionWrapper; |
14 | 16 | use FOS\RestBundle\View\ExceptionWrapperHandler; |
15 | 17 | use FOS\RestBundle\View\View; |
16 | 18 | use FOS\RestBundle\View\ViewHandler; |
| 19 | +use JMS\Serializer\EventDispatcher\EventDispatcher; |
| 20 | +use JMS\Serializer\Exclusion\GroupsExclusionStrategy; |
| 21 | +use JMS\Serializer\Handler\FormErrorHandler; |
| 22 | +use JMS\Serializer\Handler\HandlerRegistry; |
| 23 | +use JMS\Serializer\SerializerBuilder; |
17 | 24 | use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; |
18 | 25 | use FOS\RestBundle\Util\Codes; |
19 | 26 | use Symfony\Component\DependencyInjection\Container; |
| 27 | +use Symfony\Component\Form\Form; |
| 28 | +use Symfony\Component\Form\FormBuilder; |
| 29 | +use Symfony\Component\Form\FormError; |
| 30 | +use Symfony\Component\Form\Forms; |
20 | 31 | use Symfony\Component\HttpFoundation\Request; |
21 | 32 | use Symfony\Component\HttpFoundation\Response; |
22 | 33 | use Symfony\Component\Form\FormView; |
@@ -546,4 +557,92 @@ public function testConfigurableViewHandlerInterface() |
546 | 557 | $this->assertEquals('1.1', $context->attributes->get('version')->getOrThrow(new \Exception('Serialization version not set as expected'))); |
547 | 558 | $this->assertTrue($context->shouldSerializeNull()); |
548 | 559 | } |
| 560 | + |
| 561 | + /** |
| 562 | + * @dataProvider exceptionWrapperSerializeResponseContentProvider |
| 563 | + * @param string $format |
| 564 | + */ |
| 565 | + public function testCreateResponseWithFormErrorsAndSerializationGroups($format) |
| 566 | + { |
| 567 | + $form = Forms::createFormFactory()->createBuilder() |
| 568 | + ->add('name', 'text') |
| 569 | + ->add('description', 'text') |
| 570 | + ->getForm(); |
| 571 | + |
| 572 | + $form->get('name')->addError(new FormError('Invalid name')); |
| 573 | + |
| 574 | + $exceptionWrapper = new ExceptionWrapper( |
| 575 | + array( |
| 576 | + 'status_code' => 400, |
| 577 | + 'message' => 'Validation Failed', |
| 578 | + 'errors' => $form |
| 579 | + ) |
| 580 | + ); |
| 581 | + |
| 582 | + $view = new View($exceptionWrapper); |
| 583 | + $view->getSerializationContext()->setGroups(array('Custom')); |
| 584 | + |
| 585 | + $wrapperHandler = new ExceptionWrapperSerializeHandler(); |
| 586 | + $translatorMock = $this->getMock( |
| 587 | + 'Symfony\\Component\\Translation\\TranslatorInterface', |
| 588 | + array('trans', 'transChoice', 'setLocale', 'getLocale') |
| 589 | + ); |
| 590 | + $translatorMock |
| 591 | + ->expects($this->any()) |
| 592 | + ->method('trans') |
| 593 | + ->will($this->returnArgument(0)); |
| 594 | + |
| 595 | + $formErrorHandler = new FormErrorHandler($translatorMock); |
| 596 | + |
| 597 | + $serializer = SerializerBuilder::create() |
| 598 | + ->configureHandlers(function (HandlerRegistry $handlerRegistry) use ($wrapperHandler, $formErrorHandler) { |
| 599 | + $handlerRegistry->registerSubscribingHandler($wrapperHandler); |
| 600 | + $handlerRegistry->registerSubscribingHandler($formErrorHandler); |
| 601 | + }) |
| 602 | + ->build(); |
| 603 | + |
| 604 | + $container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get')); |
| 605 | + $container |
| 606 | + ->expects($this->once()) |
| 607 | + ->method('get') |
| 608 | + ->with('fos_rest.serializer') |
| 609 | + ->will($this->returnValue($serializer)); |
| 610 | + |
| 611 | + $viewHandler = new ViewHandler(array()); |
| 612 | + $viewHandler->setContainer($container); |
| 613 | + |
| 614 | + $response = $viewHandler->createResponse($view, new Request(), $format); |
| 615 | + |
| 616 | + $serializer2 = SerializerBuilder::create() |
| 617 | + ->configureHandlers(function (HandlerRegistry $handlerRegistry) use ($wrapperHandler, $formErrorHandler) { |
| 618 | + $handlerRegistry->registerSubscribingHandler($formErrorHandler); |
| 619 | + }) |
| 620 | + ->build(); |
| 621 | + |
| 622 | + $container2 = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get')); |
| 623 | + $container2 |
| 624 | + ->expects($this->once()) |
| 625 | + ->method('get') |
| 626 | + ->with('fos_rest.serializer') |
| 627 | + ->will($this->returnValue($serializer2)); |
| 628 | + |
| 629 | + $viewHandler = new ViewHandler(array()); |
| 630 | + $viewHandler->setContainer($container2); |
| 631 | + |
| 632 | + $view2 = new View($exceptionWrapper); |
| 633 | + $response2 = $viewHandler->createResponse($view2, new Request(), $format); |
| 634 | + |
| 635 | + $this->assertEquals($response->getContent(), $response2->getContent()); |
| 636 | + } |
| 637 | + |
| 638 | + /** |
| 639 | + * @return array |
| 640 | + */ |
| 641 | + public function exceptionWrapperSerializeResponseContentProvider() |
| 642 | + { |
| 643 | + return array( |
| 644 | + 'json' => array('json'), |
| 645 | + 'xml' => array('xml') |
| 646 | + ); |
| 647 | + } |
549 | 648 | } |
0 commit comments