* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ class XmlEncoderTest extends \PHPUnit_Framework_TestCase { public function setUp() { $serializer = new Serializer; $this->encoder = new XmlEncoder; $serializer->setEncoder('xml', $this->encoder); $serializer->addNormalizer(new CustomNormalizer); } public function testEncodeScalar() { $obj = new ScalarDummy; $obj->xmlFoo = "foo"; $expected = ''."\n". ''."\n"; $this->assertEquals($expected, $this->encoder->encode($obj, 'xml')); } public function testEncodeSimpleXML() { $xml = simplexml_load_string('Peter'); $array = array('person' => $xml); $expected = ''."\n". 'Peter'."\n"; $this->assertEquals($expected, $this->encoder->encode($array, 'xml')); } public function testDecodeScalar() { $source = ''."\n". 'foo'."\n"; $this->assertEquals('foo', $this->encoder->decode($source, 'xml')); } public function testEncode() { $source = $this->getXmlSource(); $obj = $this->getObject(); $this->assertEquals($source, $this->encoder->encode($obj, 'xml')); } public function testDecode() { $source = $this->getXmlSource(); $obj = $this->getObject(); $this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml')); } protected function getXmlSource() { return ''."\n". ''. ''. ''. ''. '1'. ''."\n"; } protected function getObject() { $obj = new Dummy; $obj->foo = 'foo'; $obj->bar = array('a', 'b'); $obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar'); $obj->qux = "1"; return $obj; } }