[Serializer] Fix that it will never reach DOMNode

This commit is contained in:
Alexander Janssen 2020-07-29 17:05:52 +02:00 committed by Fabien Potencier
parent cc827466bb
commit 38787ac785
2 changed files with 17 additions and 3 deletions

View File

@ -488,6 +488,9 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
$node->appendChild($child);
} elseif ($val instanceof \Traversable) {
$this->buildXml($node, $val);
} elseif ($val instanceof \DOMNode) {
$child = $this->dom->importNode($val, true);
$node->appendChild($child);
} elseif (\is_object($val)) {
if (null === $this->serializer) {
throw new BadMethodCallException(sprintf('The serializer needs to be set to allow "%s()" to be used with object data.', __METHOD__));
@ -502,9 +505,6 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
return $this->appendText($node, $val);
} elseif (\is_bool($val)) {
return $this->appendText($node, (int) $val);
} elseif ($val instanceof \DOMNode) {
$child = $this->dom->importNode($val, true);
$node->appendChild($child);
}
return true;

View File

@ -662,6 +662,20 @@ XML;
$this->assertEquals($expectedXml, $actualXml);
}
public function testEncodeXmlWithDomNodeValue()
{
$expectedXml = <<<'XML'
<?xml version="1.0"?>
<response><foo>bar</foo><bar>foo &amp; bar</bar></response>
XML;
$document = new \DOMDocument();
$actualXml = $this->encoder->encode(['foo' => $document->createTextNode('bar'), 'bar' => $document->createTextNode('foo & bar')], 'xml');
$this->assertEquals($expectedXml, $actualXml);
}
public function testEncodeXmlWithDateTimeObjectValue()
{
$xmlEncoder = $this->createXmlEncoderWithDateTimeNormalizer();