|
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\Handler\FormErrorHandler; |
| 21 | +use JMS\Serializer\Handler\HandlerRegistry; |
| 22 | +use JMS\Serializer\SerializerBuilder; |
17 | 23 | use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; |
18 | 24 | use FOS\RestBundle\Util\Codes; |
19 | 25 | use Symfony\Component\DependencyInjection\Container; |
| 26 | +use Symfony\Component\Form\Form; |
| 27 | +use Symfony\Component\Form\FormBuilder; |
| 28 | +use Symfony\Component\Form\FormError; |
| 29 | +use Symfony\Component\Form\Forms; |
20 | 30 | use Symfony\Component\HttpFoundation\Request; |
21 | 31 | use Symfony\Component\HttpFoundation\Response; |
22 | 32 | use Symfony\Component\Form\FormView; |
@@ -546,4 +556,92 @@ public function testConfigurableViewHandlerInterface() |
546 | 556 | $this->assertEquals('1.1', $context->attributes->get('version')->getOrThrow(new \Exception('Serialization version not set as expected'))); |
547 | 557 | $this->assertTrue($context->shouldSerializeNull()); |
548 | 558 | } |
| 559 | + |
| 560 | + public function testCreateResponseWithFormErrorsAndSerializationGroups() |
| 561 | + { |
| 562 | + $builder = Forms::createFormFactory()->createBuilder(); |
| 563 | + $form = $builder |
| 564 | + ->add('name', 'text') |
| 565 | + ->add('description', 'text') |
| 566 | + ->getForm(); |
| 567 | + |
| 568 | + $exceptionWrapper = new ExceptionWrapper( |
| 569 | + array( |
| 570 | + 'status_code' => 400, |
| 571 | + 'message' => 'Validation Failed', |
| 572 | + 'errors' => $form |
| 573 | + ) |
| 574 | + ); |
| 575 | + |
| 576 | + $view = new View($exceptionWrapper); |
| 577 | + $view->getSerializationContext()->setGroups(array('Custom')); |
| 578 | + |
| 579 | + $handler = new ExceptionWrapperSerializeHandler(); |
| 580 | + $formErrorHandlerMock = $this->getMock( |
| 581 | + 'JMS\Serializer\Handler\FormErrorHandler', |
| 582 | + array('serializeFormToJson'), |
| 583 | + array(), |
| 584 | + '', |
| 585 | + false |
| 586 | + ); |
| 587 | + $formErrorHandlerMock |
| 588 | + ->expects($this->once()) |
| 589 | + ->method('serializeFormToJson') |
| 590 | + ->with( |
| 591 | + $this->isInstanceOf('JMS\\Serializer\\JsonSerializationVisitor'), |
| 592 | + $form, |
| 593 | + array( |
| 594 | + 'name' => 'Symfony\Component\Form\Form', |
| 595 | + 'params' => array() |
| 596 | + ) |
| 597 | + ) |
| 598 | + ->will( |
| 599 | + $this->returnValue( |
| 600 | + array( |
| 601 | + 'children' => array( |
| 602 | + 'name' => array( |
| 603 | + 'errors' => array( |
| 604 | + 'Invalid name' |
| 605 | + ) |
| 606 | + ), |
| 607 | + ) |
| 608 | + ) |
| 609 | + ) |
| 610 | + ); |
| 611 | + |
| 612 | + $serializer = SerializerBuilder::create() |
| 613 | + ->configureHandlers(function (HandlerRegistry $handlerRegistry) use ($handler, $formErrorHandlerMock) { |
| 614 | + $handlerRegistry->registerSubscribingHandler($handler); |
| 615 | + $handlerRegistry->registerSubscribingHandler($formErrorHandlerMock); |
| 616 | + }) |
| 617 | + ->build(); |
| 618 | + |
| 619 | + $container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get')); |
| 620 | + $container |
| 621 | + ->expects($this->once()) |
| 622 | + ->method('get') |
| 623 | + ->with('fos_rest.serializer') |
| 624 | + ->will($this->returnValue($serializer)); |
| 625 | + |
| 626 | + $viewHandler = new ViewHandler(array()); |
| 627 | + $viewHandler->setContainer($container); |
| 628 | + |
| 629 | + $response = $viewHandler->createResponse($view, new Request(), 'json'); |
| 630 | + |
| 631 | + $expected = array( |
| 632 | + 'code' => 400, |
| 633 | + 'message' => 'Validation Failed', |
| 634 | + 'errors' => array( |
| 635 | + 'children' => array( |
| 636 | + 'name' => array( |
| 637 | + 'errors' => array( |
| 638 | + 'Invalid name' |
| 639 | + ) |
| 640 | + ) |
| 641 | + ) |
| 642 | + ) |
| 643 | + ); |
| 644 | + |
| 645 | + $this->assertEquals($expected, json_decode($response->getContent(), true)); |
| 646 | + } |
549 | 647 | } |
0 commit comments