Skip to content

Commit 497b895

Browse files
committed
Added xml serialization in ExceptionWrapper serialize handler
1 parent 5bbf8f5 commit 497b895

File tree

2 files changed

+113
-65
lines changed

2 files changed

+113
-65
lines changed

Serializer/ExceptionWrapperSerializeHandler.php

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
use JMS\Serializer\Context;
77
use JMS\Serializer\GraphNavigator;
88
use JMS\Serializer\Handler\SubscribingHandlerInterface;
9-
use JMS\Serializer\VisitorInterface;
9+
use JMS\Serializer\JsonSerializationVisitor;
10+
use JMS\Serializer\XmlSerializationVisitor;
1011

1112
class ExceptionWrapperSerializeHandler implements SubscribingHandlerInterface
1213
{
@@ -21,29 +22,75 @@ public static function getSubscribingMethods()
2122
'format' => 'json',
2223
'type' => 'FOS\\RestBundle\\Util\\ExceptionWrapper',
2324
'method' => 'serializeToJson'
25+
),
26+
array(
27+
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
28+
'format' => 'xml',
29+
'type' => 'FOS\\RestBundle\\Util\\ExceptionWrapper',
30+
'method' => 'serializeToXml'
2431
)
2532
);
2633
}
2734

2835
/**
29-
* @param VisitorInterface $visitor
30-
* @param ExceptionWrapper $value
36+
* @param JsonSerializationVisitor $visitor
37+
* @param ExceptionWrapper $wrapper
3138
* @param array $type
3239
* @param Context $context
33-
* @return string
40+
* @return array
3441
*/
3542
public function serializeToJson(
36-
VisitorInterface $visitor,
37-
ExceptionWrapper $value,
43+
JsonSerializationVisitor $visitor,
44+
ExceptionWrapper $wrapper,
3845
array $type,
3946
Context $context
4047
) {
41-
$type['name'] = 'array';
42-
$data = array(
43-
'code' => $value->getCode(),
44-
'message' => $value->getMessage(),
45-
'errors' => $value->getErrors(),
46-
);
48+
$data = $this->convertToArray($wrapper);
4749
return $visitor->visitArray($data, $type, $context);
4850
}
51+
52+
/**
53+
* @param XmlSerializationVisitor $visitor
54+
* @param ExceptionWrapper $wrapper
55+
* @param array $type
56+
* @param Context $context
57+
*/
58+
public function serializeToXml(
59+
XmlSerializationVisitor $visitor,
60+
ExceptionWrapper $wrapper,
61+
array $type,
62+
Context $context
63+
) {
64+
$data = $this->convertToArray($wrapper);
65+
66+
if (null === $visitor->document) {
67+
$visitor->document = $visitor->createDocument(null, null, true);
68+
}
69+
70+
foreach ($data as $key => $value) {
71+
$entryNode = $visitor->document->createElement($key);
72+
$visitor->getCurrentNode()->appendChild($entryNode);
73+
$visitor->setCurrentNode($entryNode);
74+
75+
$node = $context->getNavigator()->accept($value, null, $context);
76+
if (null !== $node) {
77+
$visitor->getCurrentNode()->appendChild($node);
78+
}
79+
80+
$visitor->revertCurrentNode();
81+
}
82+
}
83+
84+
/**
85+
* @param ExceptionWrapper $exceptionWrapper
86+
* @return array
87+
*/
88+
protected function convertToArray(ExceptionWrapper $exceptionWrapper)
89+
{
90+
return array(
91+
'code' => $exceptionWrapper->getCode(),
92+
'message' => $exceptionWrapper->getMessage(),
93+
'errors' => $exceptionWrapper->getErrors(),
94+
);
95+
}
4996
}

Tests/View/ViewHandlerTest.php

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use FOS\RestBundle\View\View;
1818
use FOS\RestBundle\View\ViewHandler;
1919
use JMS\Serializer\EventDispatcher\EventDispatcher;
20+
use JMS\Serializer\Exclusion\GroupsExclusionStrategy;
2021
use JMS\Serializer\Handler\FormErrorHandler;
2122
use JMS\Serializer\Handler\HandlerRegistry;
2223
use JMS\Serializer\SerializerBuilder;
@@ -557,14 +558,19 @@ public function testConfigurableViewHandlerInterface()
557558
$this->assertTrue($context->shouldSerializeNull());
558559
}
559560

560-
public function testCreateResponseWithFormErrorsAndSerializationGroups()
561+
/**
562+
* @dataProvider exceptionWrapperSerializeResponseContentProvider
563+
* @param string $format
564+
*/
565+
public function testCreateResponseWithFormErrorsAndSerializationGroups($format)
561566
{
562-
$builder = Forms::createFormFactory()->createBuilder();
563-
$form = $builder
567+
$form = Forms::createFormFactory()->createBuilder()
564568
->add('name', 'text')
565569
->add('description', 'text')
566570
->getForm();
567571

572+
$form->get('name')->addError(new FormError('Invalid name'));
573+
568574
$exceptionWrapper = new ExceptionWrapper(
569575
array(
570576
'status_code' => 400,
@@ -576,43 +582,22 @@ public function testCreateResponseWithFormErrorsAndSerializationGroups()
576582
$view = new View($exceptionWrapper);
577583
$view->getSerializationContext()->setGroups(array('Custom'));
578584

579-
$handler = new ExceptionWrapperSerializeHandler();
580-
$formErrorHandlerMock = $this->getMock(
581-
'JMS\Serializer\Handler\FormErrorHandler',
582-
array('serializeFormToJson'),
583-
array(),
584-
'',
585-
false
585+
$wrapperHandler = new ExceptionWrapperSerializeHandler();
586+
$translatorMock = $this->getMock(
587+
'Symfony\\Component\\Translation\\TranslatorInterface',
588+
array('trans', 'transChoice', 'setLocale', 'getLocale')
586589
);
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-
);
590+
$translatorMock
591+
->expects($this->any())
592+
->method('trans')
593+
->will($this->returnArgument(0));
594+
595+
$formErrorHandler = new FormErrorHandler($translatorMock);
611596

612597
$serializer = SerializerBuilder::create()
613-
->configureHandlers(function (HandlerRegistry $handlerRegistry) use ($handler, $formErrorHandlerMock) {
614-
$handlerRegistry->registerSubscribingHandler($handler);
615-
$handlerRegistry->registerSubscribingHandler($formErrorHandlerMock);
598+
->configureHandlers(function (HandlerRegistry $handlerRegistry) use ($wrapperHandler, $formErrorHandler) {
599+
$handlerRegistry->registerSubscribingHandler($wrapperHandler);
600+
$handlerRegistry->registerSubscribingHandler($formErrorHandler);
616601
})
617602
->build();
618603

@@ -626,22 +611,38 @@ public function testCreateResponseWithFormErrorsAndSerializationGroups()
626611
$viewHandler = new ViewHandler(array());
627612
$viewHandler->setContainer($container);
628613

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-
);
614+
$response = $viewHandler->createResponse($view, new Request(), $format);
644615

645-
$this->assertEquals($expected, json_decode($response->getContent(), true));
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+
);
646647
}
647648
}

0 commit comments

Comments
 (0)