writer = new Writer(); $this->writer->namespaceMap = [ 'http://sabredav.org/ns' => 's', ]; $this->writer->openMemory(); $this->writer->setIndent(true); $this->writer->startDocument(); } function compare($input, $output) { $this->writer->write($input); $this->assertEquals($output, $this->writer->outputMemory()); } function testSimple() { $this->compare([ '{http://sabredav.org/ns}root' => 'text', ], << text HI ); } /** * @depends testSimple */ function testSimpleQuotes() { $this->compare([ '{http://sabredav.org/ns}root' => '"text"', ], << "text" HI ); } function testSimpleAttributes() { $this->compare([ '{http://sabredav.org/ns}root' => [ 'value' => 'text', 'attributes' => [ 'attr1' => 'attribute value', ], ], ], << text HI ); } function testMixedSyntax() { $this->compare([ '{http://sabredav.org/ns}root' => [ '{http://sabredav.org/ns}single' => 'value', '{http://sabredav.org/ns}multiple' => [ [ 'name' => '{http://sabredav.org/ns}foo', 'value' => 'bar', ], [ 'name' => '{http://sabredav.org/ns}foo', 'value' => 'foobar', ], ], [ 'name' => '{http://sabredav.org/ns}attributes', 'value' => null, 'attributes' => [ 'foo' => 'bar', ], ], [ 'name' => '{http://sabredav.org/ns}verbose', 'value' => 'syntax', 'attributes' => [ 'foo' => 'bar', ], ], ], ], << value bar foobar syntax HI ); } function testNull() { $this->compare([ '{http://sabredav.org/ns}root' => null, ], << HI ); } function testArrayFormat2() { $this->compare([ '{http://sabredav.org/ns}root' => [ [ 'name' => '{http://sabredav.org/ns}elem1', 'value' => 'text', 'attributes' => [ 'attr1' => 'attribute value', ], ], ], ], << text HI ); } function testArrayOfValues() { $this->compare([ '{http://sabredav.org/ns}root' => [ [ 'name' => '{http://sabredav.org/ns}elem1', 'value' => [ 'foo', 'bar', 'baz', ], ], ], ], << foobarbaz HI ); } /** * @depends testArrayFormat2 */ function testArrayFormat2NoValue() { $this->compare([ '{http://sabredav.org/ns}root' => [ [ 'name' => '{http://sabredav.org/ns}elem1', 'attributes' => [ 'attr1' => 'attribute value', ], ], ], ], << HI ); } function testCustomNamespace() { $this->compare([ '{http://sabredav.org/ns}root' => [ '{urn:foo}elem1' => 'bar', ], ], << bar HI ); } function testEmptyNamespace() { // Empty namespaces are allowed, so we should support this. $this->compare([ '{http://sabredav.org/ns}root' => [ '{}elem1' => 'bar', ], ], << bar HI ); } function testAttributes() { $this->compare([ '{http://sabredav.org/ns}root' => [ [ 'name' => '{http://sabredav.org/ns}elem1', 'value' => 'text', 'attributes' => [ 'attr1' => 'val1', '{http://sabredav.org/ns}attr2' => 'val2', '{urn:foo}attr3' => 'val3', ], ], ], ], << text HI ); } function testBaseElement() { $this->compare([ '{http://sabredav.org/ns}root' => new Element\Base('hello') ], << hello HI ); } function testElementObj() { $this->compare([ '{http://sabredav.org/ns}root' => new Element\Mock() ], << hiiii! HI ); } function testEmptyNamespacePrefix() { $this->writer->namespaceMap['http://sabredav.org/ns'] = null; $this->compare([ '{http://sabredav.org/ns}root' => new Element\Mock() ], << hiiii! HI ); } function testEmptyNamespacePrefixEmptyString() { $this->writer->namespaceMap['http://sabredav.org/ns'] = ''; $this->compare([ '{http://sabredav.org/ns}root' => new Element\Mock() ], << hiiii! HI ); } function testWriteElement() { $this->writer->writeElement("{http://sabredav.org/ns}foo", 'content'); $output = << content HI; $this->assertEquals($output, $this->writer->outputMemory()); } function testWriteElementComplex() { $this->writer->writeElement("{http://sabredav.org/ns}foo", new Element\KeyValue(['{http://sabredav.org/ns}bar' => 'test'])); $output = << test HI; $this->assertEquals($output, $this->writer->outputMemory()); } /** * @expectedException \InvalidArgumentException */ function testWriteBadObject() { $this->writer->write(new \StdClass()); } function testStartElementSimple() { $this->writer->startElement("foo"); $this->writer->endElement(); $output = << HI; $this->assertEquals($output, $this->writer->outputMemory()); } function testCallback() { $this->compare([ '{http://sabredav.org/ns}root' => function(Writer $writer) { $writer->text('deferred writer'); }, ], << deferred writer HI ); } /** * @expectedException \InvalidArgumentException */ function testResource() { $this->compare([ '{http://sabredav.org/ns}root' => fopen('php://memory', 'r'), ], << deferred writer HI ); } function testClassMap() { $obj = (object)[ 'key1' => 'value1', 'key2' => 'value2', ]; $this->writer->classMap['stdClass'] = function(Writer $writer, $value) { foreach (get_object_vars($value) as $key => $val) { $writer->writeElement('{http://sabredav.org/ns}' . $key, $val); } }; $this->compare([ '{http://sabredav.org/ns}root' => $obj ], << value1 value2 HI ); } }