|
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();
|
@@ -551,4 +557,89 @@ protected function getObject()
|
551 | 557 |
|
552 | 558 | return $obj;
|
553 | 559 | }
|
| 560 | + |
| 561 | + public function testEncodeXmlWithBoolValue() |
| 562 | + { |
| 563 | + $expectedXml = <<<'XML' |
| 564 | +<?xml version="1.0"?> |
| 565 | +<response><foo>1</foo><bar>0</bar></response> |
| 566 | + |
| 567 | +XML; |
| 568 | + |
| 569 | + $actualXml = $this->encoder->encode(array('foo' => true, 'bar' => false), 'xml'); |
| 570 | + |
| 571 | + $this->assertEquals($expectedXml, $actualXml); |
| 572 | + } |
| 573 | + |
| 574 | + public function testEncodeXmlWithDateTimeObjectValue() |
| 575 | + { |
| 576 | + $xmlEncoder = $this->createXmlEncoderWithDateTimeNormalizer(); |
| 577 | + |
| 578 | + $actualXml = $xmlEncoder->encode(array('dateTime' => new \DateTime($this->exampleDateTimeString)), 'xml'); |
| 579 | + |
| 580 | + $this->assertEquals($this->createXmlWithDateTime(), $actualXml); |
| 581 | + } |
| 582 | + |
| 583 | + public function testEncodeXmlWithDateTimeObjectField() |
| 584 | + { |
| 585 | + $xmlEncoder = $this->createXmlEncoderWithDateTimeNormalizer(); |
| 586 | + |
| 587 | + $actualXml = $xmlEncoder->encode(array('foo' => array('@dateTime' => new \DateTime($this->exampleDateTimeString))), 'xml'); |
| 588 | + |
| 589 | + $this->assertEquals($this->createXmlWithDateTimeField(), $actualXml); |
| 590 | + } |
| 591 | + |
| 592 | + /** |
| 593 | + * @return XmlEncoder |
| 594 | + */ |
| 595 | + private function createXmlEncoderWithDateTimeNormalizer() |
| 596 | + { |
| 597 | + $encoder = new XmlEncoder(); |
| 598 | + $serializer = new Serializer(array($this->createMockDateTimeNormalizer()), array('xml' => new XmlEncoder())); |
| 599 | + $encoder->setSerializer($serializer); |
| 600 | + |
| 601 | + return $encoder; |
| 602 | + } |
| 603 | + |
| 604 | + /** |
| 605 | + * @return \PHPUnit_Framework_MockObject_MockObject|NormalizerInterface |
| 606 | + */ |
| 607 | + private function createMockDateTimeNormalizer() |
| 608 | + { |
| 609 | + $mock = $this->getMockBuilder('\Symfony\Component\Serializer\Normalizer\CustomNormalizer')->getMock(); |
| 610 | + |
| 611 | + $mock |
| 612 | + ->expects($this->once()) |
| 613 | + ->method('normalize') |
| 614 | + ->with(new \DateTime($this->exampleDateTimeString), 'xml', array()) |
| 615 | + ->willReturn($this->exampleDateTimeString); |
| 616 | + |
| 617 | + $mock |
| 618 | + ->expects($this->once()) |
| 619 | + ->method('supportsNormalization') |
| 620 | + ->with(new \DateTime($this->exampleDateTimeString), 'xml') |
| 621 | + ->willReturn(true); |
| 622 | + |
| 623 | + return $mock; |
| 624 | + } |
| 625 | + |
| 626 | + /** |
| 627 | + * @return string |
| 628 | + */ |
| 629 | + private function createXmlWithDateTime() |
| 630 | + { |
| 631 | + return sprintf('<?xml version="1.0"?> |
| 632 | +<response><dateTime>%s</dateTime></response> |
| 633 | +', $this->exampleDateTimeString); |
| 634 | + } |
| 635 | + |
| 636 | + /** |
| 637 | + * @return string |
| 638 | + */ |
| 639 | + private function createXmlWithDateTimeField() |
| 640 | + { |
| 641 | + return sprintf('<?xml version="1.0"?> |
| 642 | +<response><foo dateTime="%s"/></response> |
| 643 | +', $this->exampleDateTimeString); |
| 644 | + } |
554 | 645 | }
|
0 commit comments