1<?php 2 3namespace Sabre\Xml; 4 5class WriterTest extends \PHPUnit_Framework_TestCase { 6 7 protected $writer; 8 9 function setUp() { 10 11 $this->writer = new Writer(); 12 $this->writer->namespaceMap = [ 13 'http://sabredav.org/ns' => 's', 14 ]; 15 $this->writer->openMemory(); 16 $this->writer->setIndent(true); 17 $this->writer->startDocument(); 18 19 } 20 21 function compare($input, $output) { 22 23 $this->writer->write($input); 24 $this->assertEquals($output, $this->writer->outputMemory()); 25 26 } 27 28 29 function testSimple() { 30 31 $this->compare([ 32 '{http://sabredav.org/ns}root' => 'text', 33 ], <<<HI 34<?xml version="1.0"?> 35<s:root xmlns:s="http://sabredav.org/ns">text</s:root> 36 37HI 38 ); 39 40 } 41 42 /** 43 * @depends testSimple 44 */ 45 function testSimpleQuotes() { 46 47 $this->compare([ 48 '{http://sabredav.org/ns}root' => '"text"', 49 ], <<<HI 50<?xml version="1.0"?> 51<s:root xmlns:s="http://sabredav.org/ns">"text"</s:root> 52 53HI 54 ); 55 56 } 57 58 function testSimpleAttributes() { 59 60 $this->compare([ 61 '{http://sabredav.org/ns}root' => [ 62 'value' => 'text', 63 'attributes' => [ 64 'attr1' => 'attribute value', 65 ], 66 ], 67 ], <<<HI 68<?xml version="1.0"?> 69<s:root xmlns:s="http://sabredav.org/ns" attr1="attribute value">text</s:root> 70 71HI 72 ); 73 74 } 75 76 function testMixedSyntax() { 77 $this->compare([ 78 '{http://sabredav.org/ns}root' => [ 79 'single' => 'value', 80 'multiple' => [ 81 [ 82 'name' => 'foo', 83 'value' => 'bar', 84 ], 85 [ 86 'name' => 'foo', 87 'value' => 'foobar', 88 ], 89 ], 90 'attributes' => [ 91 'value' => null, 92 'attributes' => [ 93 'foo' => 'bar', 94 ], 95 ], 96 [ 97 'name' => 'verbose', 98 'value' => 'syntax', 99 'attributes' => [ 100 'foo' => 'bar', 101 ], 102 ], 103 ], 104 ], <<<HI 105<?xml version="1.0"?> 106<s:root xmlns:s="http://sabredav.org/ns"> 107 <single>value</single> 108 <multiple> 109 <foo>bar</foo> 110 <foo>foobar</foo> 111 </multiple> 112 <attributes foo="bar"/> 113 <verbose foo="bar">syntax</verbose> 114</s:root> 115 116HI 117 ); 118 } 119 120 function testNull() { 121 122 $this->compare([ 123 '{http://sabredav.org/ns}root' => null, 124 ], <<<HI 125<?xml version="1.0"?> 126<s:root xmlns:s="http://sabredav.org/ns"/> 127 128HI 129 ); 130 131 } 132 133 function testArrayFormat2() { 134 135 $this->compare([ 136 '{http://sabredav.org/ns}root' => [ 137 [ 138 'name' => '{http://sabredav.org/ns}elem1', 139 'value' => 'text', 140 'attributes' => [ 141 'attr1' => 'attribute value', 142 ], 143 ], 144 ], 145 ], <<<HI 146<?xml version="1.0"?> 147<s:root xmlns:s="http://sabredav.org/ns"> 148 <s:elem1 attr1="attribute value">text</s:elem1> 149</s:root> 150 151HI 152 ); 153 154 } 155 156 function testCustomNamespace() { 157 158 $this->compare([ 159 '{http://sabredav.org/ns}root' => [ 160 '{urn:foo}elem1' => 'bar', 161 ], 162 ], <<<HI 163<?xml version="1.0"?> 164<s:root xmlns:s="http://sabredav.org/ns"> 165 <x1:elem1 xmlns:x1="urn:foo">bar</x1:elem1> 166</s:root> 167 168HI 169 ); 170 171 } 172 173 function testEmptyNamespace() { 174 175 // Empty namespaces are allowed, so we should support this. 176 $this->compare([ 177 '{http://sabredav.org/ns}root' => [ 178 '{}elem1' => 'bar', 179 ], 180 ], <<<HI 181<?xml version="1.0"?> 182<s:root xmlns:s="http://sabredav.org/ns"> 183 <elem1 xmlns="">bar</elem1> 184</s:root> 185 186HI 187 ); 188 189 } 190 191 function testAttributes() { 192 193 $this->compare([ 194 '{http://sabredav.org/ns}root' => [ 195 [ 196 'name' => '{http://sabredav.org/ns}elem1', 197 'value' => 'text', 198 'attributes' => [ 199 'attr1' => 'val1', 200 '{http://sabredav.org/ns}attr2' => 'val2', 201 '{urn:foo}attr3' => 'val3', 202 ], 203 ], 204 ], 205 ], <<<HI 206<?xml version="1.0"?> 207<s:root xmlns:s="http://sabredav.org/ns"> 208 <s:elem1 attr1="val1" s:attr2="val2" x1:attr3="val3" xmlns:x1="urn:foo">text</s:elem1> 209</s:root> 210 211HI 212 ); 213 214 } 215 216 /** 217 * @expectedException \InvalidArgumentException 218 */ 219 function testInvalidFormat() { 220 221 $this->compare([ 222 '{http://sabredav.org/ns}root' => [ 223 ['incorrect' => '0', 'keynames' => 1] 224 ], 225 ], ""); 226 227 } 228 229 function testBaseElement() { 230 231 $this->compare([ 232 '{http://sabredav.org/ns}root' => new Element\Base('hello') 233 ], <<<HI 234<?xml version="1.0"?> 235<s:root xmlns:s="http://sabredav.org/ns">hello</s:root> 236 237HI 238 ); 239 240 } 241 242 function testElementObj() { 243 244 $this->compare([ 245 '{http://sabredav.org/ns}root' => new Element\Mock() 246 ], <<<HI 247<?xml version="1.0"?> 248<s:root xmlns:s="http://sabredav.org/ns"> 249 <s:elem1>hiiii!</s:elem1> 250</s:root> 251 252HI 253 ); 254 255 } 256 257 function testEmptyNamespacePrefix() { 258 259 $this->writer->namespaceMap['http://sabredav.org/ns'] = null; 260 $this->compare([ 261 '{http://sabredav.org/ns}root' => new Element\Mock() 262 ], <<<HI 263<?xml version="1.0"?> 264<root xmlns="http://sabredav.org/ns"> 265 <elem1>hiiii!</elem1> 266</root> 267 268HI 269 ); 270 271 } 272 273 function testWriteElement() { 274 275 $this->writer->writeElement("{http://sabredav.org/ns}foo", 'content'); 276 277 $output = <<<HI 278<?xml version="1.0"?> 279<s:foo xmlns:s="http://sabredav.org/ns">content</s:foo> 280 281HI; 282 283 $this->assertEquals($output, $this->writer->outputMemory()); 284 285 286 } 287 288 function testWriteElementComplex() { 289 290 $this->writer->writeElement("{http://sabredav.org/ns}foo", new Element\KeyValue(['{http://sabredav.org/ns}bar' => 'test'])); 291 292 $output = <<<HI 293<?xml version="1.0"?> 294<s:foo xmlns:s="http://sabredav.org/ns"> 295 <s:bar>test</s:bar> 296</s:foo> 297 298HI; 299 300 $this->assertEquals($output, $this->writer->outputMemory()); 301 302 } 303 304 /** 305 * @expectedException \InvalidArgumentException 306 */ 307 function testWriteBadObject() { 308 309 $this->writer->write(new \StdClass()); 310 311 } 312 313 function testStartElementSimple() { 314 315 $this->writer->startElement("foo"); 316 $this->writer->endElement(); 317 318 $output = <<<HI 319<?xml version="1.0"?> 320<foo xmlns:s="http://sabredav.org/ns"/> 321 322HI; 323 324 $this->assertEquals($output, $this->writer->outputMemory()); 325 326 327 } 328} 329