|
19 | 19 | use Symfony\Component\Serializer\Serializer;
|
20 | 20 | use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
21 | 21 | use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
| 22 | +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
22 | 23 |
|
23 | 24 | class XmlEncoderTest extends TestCase
|
24 | 25 | {
|
| 26 | + /** |
| 27 | + * @var XmlEncoder |
| 28 | + */ |
25 | 29 | private $encoder;
|
26 | 30 |
|
| 31 | + private $exampleDateTimeString = '2017-02-19T15:16:08+0300'; |
| 32 | + |
27 | 33 | protected function setUp()
|
28 | 34 | {
|
29 | 35 | $this->encoder = new XmlEncoder();
|
@@ -524,4 +530,89 @@ protected function getObject()
|
524 | 530 |
|
525 | 531 | return $obj;
|
526 | 532 | }
|
| 533 | + |
| 534 | + public function testEncodeXmlWithBoolValue() |
| 535 | + { |
| 536 | + $expectedXml = <<<'XML' |
| 537 | +<?xml version="1.0"?> |
| 538 | +<response><foo>1</foo><bar>0</bar></response> |
| 539 | + |
| 540 | +XML; |
| 541 | + |
| 542 | + $actualXml = $this->encoder->encode(array('foo' => true, 'bar' => false), 'xml'); |
| 543 | + |
| 544 | + $this->assertEquals($expectedXml, $actualXml); |
| 545 | + } |
| 546 | + |
| 547 | + public function testEncodeXmlWithDateTimeObjectValue() |
| 548 | + { |
| 549 | + $xmlEncoder = $this->createXmlEncoderWithDateTimeNormalizer(); |
| 550 | + |
| 551 | + $actualXml = $xmlEncoder->encode(array('dateTime' => new \DateTime($this->exampleDateTimeString)), 'xml'); |
| 552 | + |
| 553 | + $this->assertEquals($this->createXmlWithDateTime(), $actualXml); |
| 554 | + } |
| 555 | + |
| 556 | + public function testEncodeXmlWithDateTimeObjectField() |
| 557 | + { |
| 558 | + $xmlEncoder = $this->createXmlEncoderWithDateTimeNormalizer(); |
| 559 | + |
| 560 | + $actualXml = $xmlEncoder->encode(array('foo' => array('@dateTime' => new \DateTime($this->exampleDateTimeString))), 'xml'); |
| 561 | + |
| 562 | + $this->assertEquals($this->createXmlWithDateTimeField(), $actualXml); |
| 563 | + } |
| 564 | + |
| 565 | + /** |
| 566 | + * @return XmlEncoder |
| 567 | + */ |
| 568 | + private function createXmlEncoderWithDateTimeNormalizer() |
| 569 | + { |
| 570 | + $encoder = new XmlEncoder(); |
| 571 | + $serializer = new Serializer(array($this->createMockDateTimeNormalizer()), array('xml' => new XmlEncoder())); |
| 572 | + $encoder->setSerializer($serializer); |
| 573 | + |
| 574 | + return $encoder; |
| 575 | + } |
| 576 | + |
| 577 | + /** |
| 578 | + * @return \PHPUnit_Framework_MockObject_MockObject|NormalizerInterface |
| 579 | + */ |
| 580 | + private function createMockDateTimeNormalizer() |
| 581 | + { |
| 582 | + $mock = $this->getMockBuilder('\Symfony\Component\Serializer\Normalizer\CustomNormalizer')->getMock(); |
| 583 | + |
| 584 | + $mock |
| 585 | + ->expects($this->once()) |
| 586 | + ->method('normalize') |
| 587 | + ->with(new \DateTime($this->exampleDateTimeString), 'xml', array()) |
| 588 | + ->willReturn($this->exampleDateTimeString); |
| 589 | + |
| 590 | + $mock |
| 591 | + ->expects($this->once()) |
| 592 | + ->method('supportsNormalization') |
| 593 | + ->with(new \DateTime($this->exampleDateTimeString), 'xml') |
| 594 | + ->willReturn(true); |
| 595 | + |
| 596 | + return $mock; |
| 597 | + } |
| 598 | + |
| 599 | + /** |
| 600 | + * @return string |
| 601 | + */ |
| 602 | + private function createXmlWithDateTime() |
| 603 | + { |
| 604 | + return sprintf('<?xml version="1.0"?> |
| 605 | +<response><dateTime>%s</dateTime></response> |
| 606 | +', $this->exampleDateTimeString); |
| 607 | + } |
| 608 | + |
| 609 | + /** |
| 610 | + * @return string |
| 611 | + */ |
| 612 | + private function createXmlWithDateTimeField() |
| 613 | + { |
| 614 | + return sprintf('<?xml version="1.0"?> |
| 615 | +<response><foo dateTime="%s"/></response> |
| 616 | +', $this->exampleDateTimeString); |
| 617 | + } |
527 | 618 | }
|
0 commit comments