[Serializer] Xml encoder throws exception for valid data
This commit is contained in:
parent
00c61da3c6
commit
5c2d4c671e
@ -369,7 +369,10 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
|
|||||||
if (is_array($data) || ($data instanceof \Traversable && !$this->serializer->supportsNormalization($data, $this->format))) {
|
if (is_array($data) || ($data instanceof \Traversable && !$this->serializer->supportsNormalization($data, $this->format))) {
|
||||||
foreach ($data as $key => $data) {
|
foreach ($data as $key => $data) {
|
||||||
//Ah this is the magic @ attribute types.
|
//Ah this is the magic @ attribute types.
|
||||||
if (0 === strpos($key, '@') && is_scalar($data) && $this->isElementNameValid($attributeName = substr($key, 1))) {
|
if (0 === strpos($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
|
||||||
|
if (!is_scalar($data)) {
|
||||||
|
$data = $this->serializer->normalize($data, $this->format, $this->context);
|
||||||
|
}
|
||||||
$parentNode->setAttribute($attributeName, $data);
|
$parentNode->setAttribute($attributeName, $data);
|
||||||
} elseif ($key === '#') {
|
} elseif ($key === '#') {
|
||||||
$append = $this->selectNodeType($parentNode, $data);
|
$append = $this->selectNodeType($parentNode, $data);
|
||||||
@ -474,7 +477,7 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
|
|||||||
} elseif ($val instanceof \Traversable) {
|
} elseif ($val instanceof \Traversable) {
|
||||||
$this->buildXml($node, $val);
|
$this->buildXml($node, $val);
|
||||||
} elseif (is_object($val)) {
|
} elseif (is_object($val)) {
|
||||||
return $this->buildXml($node, $this->serializer->normalize($val, $this->format, $this->context));
|
return $this->selectNodeType($node, $this->serializer->normalize($val, $this->format, $this->context));
|
||||||
} elseif (is_numeric($val)) {
|
} elseif (is_numeric($val)) {
|
||||||
return $this->appendText($node, (string) $val);
|
return $this->appendText($node, (string) $val);
|
||||||
} elseif (is_string($val) && $this->needsCdataWrapping($val)) {
|
} elseif (is_string($val) && $this->needsCdataWrapping($val)) {
|
||||||
|
@ -19,11 +19,17 @@ use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
|||||||
use Symfony\Component\Serializer\Serializer;
|
use Symfony\Component\Serializer\Serializer;
|
||||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||||
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||||
|
|
||||||
class XmlEncoderTest extends TestCase
|
class XmlEncoderTest extends TestCase
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var XmlEncoder
|
||||||
|
*/
|
||||||
private $encoder;
|
private $encoder;
|
||||||
|
|
||||||
|
private $exampleDateTimeString = '2017-02-19T15:16:08+0300';
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
$this->encoder = new XmlEncoder();
|
$this->encoder = new XmlEncoder();
|
||||||
@ -524,4 +530,89 @@ XML;
|
|||||||
|
|
||||||
return $obj;
|
return $obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testEncodeXmlWithBoolValue()
|
||||||
|
{
|
||||||
|
$expectedXml = <<<'XML'
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<response><foo>1</foo><bar>0</bar></response>
|
||||||
|
|
||||||
|
XML;
|
||||||
|
|
||||||
|
$actualXml = $this->encoder->encode(array('foo' => true, 'bar' => false), 'xml');
|
||||||
|
|
||||||
|
$this->assertEquals($expectedXml, $actualXml);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEncodeXmlWithDateTimeObjectValue()
|
||||||
|
{
|
||||||
|
$xmlEncoder = $this->createXmlEncoderWithDateTimeNormalizer();
|
||||||
|
|
||||||
|
$actualXml = $xmlEncoder->encode(array('dateTime' => new \DateTime($this->exampleDateTimeString)), 'xml');
|
||||||
|
|
||||||
|
$this->assertEquals($this->createXmlWithDateTime(), $actualXml);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEncodeXmlWithDateTimeObjectField()
|
||||||
|
{
|
||||||
|
$xmlEncoder = $this->createXmlEncoderWithDateTimeNormalizer();
|
||||||
|
|
||||||
|
$actualXml = $xmlEncoder->encode(array('foo' => array('@dateTime' => new \DateTime($this->exampleDateTimeString))), 'xml');
|
||||||
|
|
||||||
|
$this->assertEquals($this->createXmlWithDateTimeField(), $actualXml);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return XmlEncoder
|
||||||
|
*/
|
||||||
|
private function createXmlEncoderWithDateTimeNormalizer()
|
||||||
|
{
|
||||||
|
$encoder = new XmlEncoder();
|
||||||
|
$serializer = new Serializer(array($this->createMockDateTimeNormalizer()), array('xml' => new XmlEncoder()));
|
||||||
|
$encoder->setSerializer($serializer);
|
||||||
|
|
||||||
|
return $encoder;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \PHPUnit_Framework_MockObject_MockObject|NormalizerInterface
|
||||||
|
*/
|
||||||
|
private function createMockDateTimeNormalizer()
|
||||||
|
{
|
||||||
|
$mock = $this->getMockBuilder('\Symfony\Component\Serializer\Normalizer\CustomNormalizer')->getMock();
|
||||||
|
|
||||||
|
$mock
|
||||||
|
->expects($this->once())
|
||||||
|
->method('normalize')
|
||||||
|
->with(new \DateTime($this->exampleDateTimeString), 'xml', array())
|
||||||
|
->willReturn($this->exampleDateTimeString);
|
||||||
|
|
||||||
|
$mock
|
||||||
|
->expects($this->once())
|
||||||
|
->method('supportsNormalization')
|
||||||
|
->with(new \DateTime($this->exampleDateTimeString), 'xml')
|
||||||
|
->willReturn(true);
|
||||||
|
|
||||||
|
return $mock;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function createXmlWithDateTime()
|
||||||
|
{
|
||||||
|
return sprintf('<?xml version="1.0"?>
|
||||||
|
<response><dateTime>%s</dateTime></response>
|
||||||
|
', $this->exampleDateTimeString);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function createXmlWithDateTimeField()
|
||||||
|
{
|
||||||
|
return sprintf('<?xml version="1.0"?>
|
||||||
|
<response><foo dateTime="%s"/></response>
|
||||||
|
', $this->exampleDateTimeString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user