'Test!', ]; $util = new Service(); $util->elementMap = $elems; $reader = $util->getReader(); $this->assertInstanceOf('Sabre\\Xml\\Reader', $reader); $this->assertEquals($elems, $reader->elementMap); } function testGetWriter() { $ns = [ 'http://sabre.io/ns' => 's', ]; $util = new Service(); $util->namespaceMap = $ns; $writer = $util->getWriter(); $this->assertInstanceOf('Sabre\\Xml\\Writer', $writer); $this->assertEquals($ns, $writer->namespaceMap); } /** * @depends testGetReader */ function testParse() { $xml = << value XML; $util = new Service(); $result = $util->parse($xml, null, $rootElement); $this->assertEquals('{http://sabre.io/ns}root', $rootElement); $expected = [ [ 'name' => '{http://sabre.io/ns}child', 'value' => 'value', 'attributes' => [], ] ]; $this->assertEquals( $expected, $result ); } /** * @depends testGetReader */ function testParseStream() { $xml = << value XML; $stream = fopen('php://memory', 'r+'); fwrite($stream, $xml); rewind($stream); $util = new Service(); $result = $util->parse($stream, null, $rootElement); $this->assertEquals('{http://sabre.io/ns}root', $rootElement); $expected = [ [ 'name' => '{http://sabre.io/ns}child', 'value' => 'value', 'attributes' => [], ] ]; $this->assertEquals( $expected, $result ); } /** * @depends testGetReader */ function testExpect() { $xml = << value XML; $util = new Service(); $result = $util->expect('{http://sabre.io/ns}root', $xml); $expected = [ [ 'name' => '{http://sabre.io/ns}child', 'value' => 'value', 'attributes' => [], ] ]; $this->assertEquals( $expected, $result ); } /** * @expectedException \Sabre\Xml\LibXMLException */ function testInvalidNameSpace() { $xml = ''; $util = new Service(); $util->elementMap = [ '{DAV:}propfind' => PropFindTestAsset::class, ]; $util->namespaceMap = [ 'http://sabre.io/ns' => 's', ]; $result = $util->expect('{DAV:}propfind', $xml); } /** * @dataProvider providesEmptyPropfinds */ function testEmptyPropfind($xml) { $util = new Service(); $util->elementMap = [ '{DAV:}propfind' => PropFindTestAsset::class, ]; $util->namespaceMap = [ 'http://sabre.io/ns' => 's', ]; $result = $util->expect('{DAV:}propfind', $xml); $this->assertEquals(false, $result->allProp); $this->assertEquals([], $result->properties); } /** * @depends testGetReader */ function testExpectStream() { $xml = << value XML; $stream = fopen('php://memory', 'r+'); fwrite($stream, $xml); rewind($stream); $util = new Service(); $result = $util->expect('{http://sabre.io/ns}root', $stream); $expected = [ [ 'name' => '{http://sabre.io/ns}child', 'value' => 'value', 'attributes' => [], ] ]; $this->assertEquals( $expected, $result ); } /** * @depends testGetReader * @expectedException \Sabre\Xml\ParseException */ function testExpectWrong() { $xml = << value XML; $util = new Service(); $util->expect('{http://sabre.io/ns}error', $xml); } /** * @depends testGetWriter */ function testWrite() { $util = new Service(); $util->namespaceMap = [ 'http://sabre.io/ns' => 's', ]; $result = $util->write('{http://sabre.io/ns}root', [ '{http://sabre.io/ns}child' => 'value', ]); $expected = << value XML; $this->assertEquals( $expected, $result ); } function testMapValueObject() { $input = << 1234 99.99 black friday deal 5 XML; $ns = 'http://sabredav.org/ns'; $orderService = new \Sabre\Xml\Service(); $orderService->mapValueObject('{' . $ns . '}order', 'Sabre\Xml\Order'); $orderService->mapValueObject('{' . $ns . '}status', 'Sabre\Xml\OrderStatus'); $orderService->namespaceMap[$ns] = null; $order = $orderService->parse($input); $expected = new Order(); $expected->id = 1234; $expected->amount = 99.99; $expected->description = 'black friday deal'; $expected->status = new OrderStatus(); $expected->status->id = 5; $expected->status->label = 'processed'; $this->assertEquals($expected, $order); $writtenXml = $orderService->writeValueObject($order); $this->assertEquals($input, $writtenXml); } function testMapValueObjectArrayProperty() { $input = << 1234 99.99 black friday deal 5 http://example.org/ http://example.com/ XML; $ns = 'http://sabredav.org/ns'; $orderService = new \Sabre\Xml\Service(); $orderService->mapValueObject('{' . $ns . '}order', 'Sabre\Xml\Order'); $orderService->mapValueObject('{' . $ns . '}status', 'Sabre\Xml\OrderStatus'); $orderService->namespaceMap[$ns] = null; $order = $orderService->parse($input); $expected = new Order(); $expected->id = 1234; $expected->amount = 99.99; $expected->description = 'black friday deal'; $expected->status = new OrderStatus(); $expected->status->id = 5; $expected->status->label = 'processed'; $expected->link = ['http://example.org/', 'http://example.com/']; $this->assertEquals($expected, $order); $writtenXml = $orderService->writeValueObject($order); $this->assertEquals($input, $writtenXml); } /** * @expectedException \InvalidArgumentException */ function testWriteVoNotFound() { $service = new Service(); $service->writeValueObject(new \StdClass()); } function testParseClarkNotation() { $this->assertEquals([ 'http://sabredav.org/ns', 'elem', ], Service::parseClarkNotation('{http://sabredav.org/ns}elem')); } /** * @expectedException \InvalidArgumentException */ function testParseClarkNotationFail() { Service::parseClarkNotation('http://sabredav.org/ns}elem'); } function providesEmptyPropfinds() { return [ [''], [''], [''], [''], [' '], ]; } } /** * asset for testMapValueObject() * @internal */ class Order { public $id; public $amount; public $description; public $status; public $empty; public $link = []; } /** * asset for testMapValueObject() * @internal */ class OrderStatus { public $id; public $label; } /** * asset for testInvalidNameSpace. * * @internal */ class PropFindTestAsset implements XmlDeserializable { public $allProp = false; public $properties; static function xmlDeserialize(Reader $reader) { $self = new self(); $reader->pushContext(); $reader->elementMap['{DAV:}prop'] = 'Sabre\Xml\Element\Elements'; foreach (KeyValue::xmlDeserialize($reader) as $k => $v) { switch ($k) { case '{DAV:}prop': $self->properties = $v; break; case '{DAV:}allprop': $self->allProp = true; } } $reader->popContext(); return $self; } }