* * 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 testSetRootNodeName() { $obj = new ScalarDummy; $obj->xmlFoo = "foo"; $this->encoder->setRootNodeName('test'); $expected = ''."\n". ''."\n"; $this->assertEquals($expected, $this->encoder->encode($obj, 'xml')); } public function testAttributes() { $obj = new ScalarDummy; $obj->xmlFoo = array( 'foo-bar' => array( '@id' => 1, '@name' => 'Bar' ), 'Foo' => array( 'Bar' => "Test", '@Type' => 'test' ), 'föo_bär' => 'a', "Bar" => array(1,2,3), 'a' => 'b', ); $expected = ''."\n". ''. ''. ''. ''. '1'. '2'. '3'. ''. ''."\n"; $this->assertEquals($expected, $this->encoder->encode($obj, 'xml')); } public function testElementNameValid() { $obj = new ScalarDummy; $obj->xmlFoo = array( 'foo-bar' => 'a', 'foo_bar' => 'a', 'föo_bär' => 'a', ); $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', "Barry" => array('FooBar' => array("@id"=>1,"Baz"=>"Ed"))); $obj->qux = "1"; return $obj; } }