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