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