1<?php 2 3namespace Sabre\Xml\Element; 4 5use Sabre\Xml\Reader; 6use Sabre\Xml\Writer; 7 8class XmlFragmentTest extends \PHPUnit_Framework_TestCase { 9 10 /** 11 * @dataProvider xmlProvider 12 */ 13 function testDeserialize($input, $expected) { 14 15 $input = <<<BLA 16<?xml version="1.0"?> 17<root xmlns="http://sabredav.org/ns"> 18 <fragment>$input</fragment> 19</root> 20BLA; 21 22 $reader = new Reader(); 23 $reader->elementMap = [ 24 '{http://sabredav.org/ns}fragment' => 'Sabre\\Xml\\Element\\XmlFragment', 25 ]; 26 $reader->xml($input); 27 28 $output = $reader->parse(); 29 30 $this->assertEquals([ 31 'name' => '{http://sabredav.org/ns}root', 32 'value' => [ 33 [ 34 'name' => '{http://sabredav.org/ns}fragment', 35 'value' => new XmlFragment($expected), 36 'attributes' => [], 37 ], 38 ], 39 'attributes' => [], 40 ], $output); 41 42 } 43 44 /** 45 * Data provider for serialize and deserialize tests. 46 * 47 * Returns three items per test: 48 * 49 * 1. Input data for the reader. 50 * 2. Expected output for XmlFragment deserializer 51 * 3. Expected output after serializing that value again. 52 * 53 * If 3 is not set, use 1 for 3. 54 * 55 * @return void 56 */ 57 function xmlProvider() { 58 59 return [ 60 [ 61 'hello', 62 'hello', 63 ], 64 [ 65 '<element>hello</element>', 66 '<element xmlns="http://sabredav.org/ns">hello</element>' 67 ], 68 [ 69 '<element foo="bar">hello</element>', 70 '<element xmlns="http://sabredav.org/ns" foo="bar">hello</element>' 71 ], 72 [ 73 '<element x1:foo="bar" xmlns:x1="http://example.org/ns">hello</element>', 74 '<element xmlns:x1="http://example.org/ns" xmlns="http://sabredav.org/ns" x1:foo="bar">hello</element>' 75 ], 76 [ 77 '<element xmlns="http://example.org/ns">hello</element>', 78 '<element xmlns="http://example.org/ns">hello</element>', 79 '<x1:element xmlns:x1="http://example.org/ns">hello</x1:element>', 80 ], 81 [ 82 '<element xmlns:foo="http://example.org/ns">hello</element>', 83 '<element xmlns:foo="http://example.org/ns" xmlns="http://sabredav.org/ns">hello</element>', 84 '<element>hello</element>', 85 ], 86 [ 87 '<foo:element xmlns:foo="http://example.org/ns">hello</foo:element>', 88 '<foo:element xmlns:foo="http://example.org/ns">hello</foo:element>', 89 '<x1:element xmlns:x1="http://example.org/ns">hello</x1:element>', 90 ], 91 [ 92 '<foo:element xmlns:foo="http://example.org/ns"><child>hello</child></foo:element>', 93 '<foo:element xmlns:foo="http://example.org/ns" xmlns="http://sabredav.org/ns"><child>hello</child></foo:element>', 94 '<x1:element xmlns:x1="http://example.org/ns"><child>hello</child></x1:element>', 95 ], 96 [ 97 '<foo:element xmlns:foo="http://example.org/ns"><child/></foo:element>', 98 '<foo:element xmlns:foo="http://example.org/ns" xmlns="http://sabredav.org/ns"><child/></foo:element>', 99 '<x1:element xmlns:x1="http://example.org/ns"><child/></x1:element>', 100 ], 101 [ 102 '<foo:element xmlns:foo="http://example.org/ns"><child a="b"/></foo:element>', 103 '<foo:element xmlns:foo="http://example.org/ns" xmlns="http://sabredav.org/ns"><child a="b"/></foo:element>', 104 '<x1:element xmlns:x1="http://example.org/ns"><child a="b"/></x1:element>', 105 ], 106 ]; 107 108 } 109 110 /** 111 * @dataProvider xmlProvider 112 */ 113 function testSerialize($expectedFallback, $input, $expected = null) { 114 115 if (is_null($expected)) { 116 $expected = $expectedFallback; 117 } 118 119 $writer = new Writer(); 120 $writer->namespaceMap = [ 121 'http://sabredav.org/ns' => null 122 ]; 123 $writer->openMemory(); 124 $writer->startDocument('1.0'); 125 //$writer->setIndent(true); 126 $writer->write([ 127 '{http://sabredav.org/ns}root' => [ 128 '{http://sabredav.org/ns}fragment' => new XmlFragment($input), 129 ], 130 ]); 131 132 $output = $writer->outputMemory(); 133 134 $expected = <<<XML 135<?xml version="1.0"?> 136<root xmlns="http://sabredav.org/ns"><fragment>$expected</fragment></root> 137XML; 138 139 $this->assertEquals($expected, $output); 140 141 } 142 143} 144