-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Xml encoder throws exception for valid data #21671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,18 +12,24 @@ | |
namespace Symfony\Component\Serializer\Tests\Encoder; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Serializer\Tests\Fixtures\Dummy; | ||
use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy; | ||
use Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy; | ||
use Symfony\Component\Serializer\Encoder\XmlEncoder; | ||
use Symfony\Component\Serializer\Serializer; | ||
use Symfony\Component\Serializer\Exception\UnexpectedValueException; | ||
use Symfony\Component\Serializer\Normalizer\CustomNormalizer; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use Symfony\Component\Serializer\Serializer; | ||
use Symfony\Component\Serializer\Tests\Fixtures\Dummy; | ||
use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy; | ||
use Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy; | ||
|
||
class XmlEncoderTest extends TestCase | ||
{ | ||
/** | ||
* @var XmlEncoder | ||
*/ | ||
private $encoder; | ||
|
||
private $exampleDateTimeString = '2017-02-19T15:16:08+0300'; | ||
|
||
protected function setUp() | ||
{ | ||
$this->encoder = new XmlEncoder(); | ||
|
@@ -524,4 +530,89 @@ protected function getObject() | |
|
||
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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't get why this is expected? IMO it should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an actual behavior of XmlEncode, to cast bool to int. I am not sure that i should fix this non explicit behaviour, so i am just cover that with test. |
||
|
||
$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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please revert any change related to ordering: our policy is to add new lines in alpha order, but keep old ones as is - to help for merges between branches.