1<?php
2
3namespace Sabre\Xml\Deserializer;
4
5use
6    Sabre\Xml\Reader;
7
8class KeyValueTest extends \PHPUnit_Framework_TestCase {
9
10    function testKeyValue() {
11
12        $input = <<<BLA
13<?xml version="1.0"?>
14<root xmlns="http://sabredav.org/ns">
15  <struct>
16    <elem1 />
17    <elem2>hi</elem2>
18    <elem3 xmlns="http://sabredav.org/another-ns">
19       <elem4>foo</elem4>
20       <elem5>foo &amp; bar</elem5>
21    </elem3>
22  </struct>
23</root>
24BLA;
25
26        $reader = new Reader();
27        $reader->elementMap = [
28            '{http://sabredav.org/ns}struct' => function(Reader $reader) {
29                return keyValue($reader, 'http://sabredav.org/ns');
30            }
31        ];
32        $reader->xml($input);
33        $output = $reader->parse();
34
35        $this->assertEquals([
36            'name'  => '{http://sabredav.org/ns}root',
37            'value' => [
38                [
39                    'name'  => '{http://sabredav.org/ns}struct',
40                    'value' => [
41                        'elem1'                                 => null,
42                        'elem2'                                 => 'hi',
43                        '{http://sabredav.org/another-ns}elem3' => [
44                            [
45                                'name'       => '{http://sabredav.org/another-ns}elem4',
46                                'value'      => 'foo',
47                                'attributes' => [],
48                            ],
49                            [
50                                'name'       => '{http://sabredav.org/another-ns}elem5',
51                                'value'      => 'foo & bar',
52                                'attributes' => [],
53                            ],
54                        ]
55                    ],
56                    'attributes' => [],
57                ]
58            ],
59            'attributes' => [],
60        ], $output);
61    }
62
63    /**
64     * @expectedException \Sabre\Xml\LibXMLException
65     */
66    function testKeyValueLoop() {
67
68        /**
69         * This bug is a weird one, because it triggers an infinite loop, but
70         * only if the XML document is a certain size (in bytes). Removing one
71         * or two characters from the xml body here cause the infinite loop to
72         * *not* get triggered, so to properly test this bug (Issue #94), don't
73         * change the XML body.
74         */
75        $invalid_xml = '
76        <foo ft="PRNTING" Ppt="YES" AutoClose="YES" SkipUnverified="NO" Test="NO">
77            <Package ID="1">
78                <MailClass>NONE</MailClass>
79                <PackageType>ENVELOPE</PackageType>
80                <WeightOz>1</WeightOz>
81                <FleetType>DC</FleetType>
82            <Package ID="2">
83                <MailClass>NONE</MailClass>
84                <PackageType>ENVELOPE</PackageType>
85                <WeightOz>1</WeightOz>
86                <FleetType>DC/FleetType>
87            </Package>
88        </foo>';
89        $reader = new Reader();
90
91        $reader->xml($invalid_xml);
92        $reader->elementMap = [
93
94            '{}Package' => function($reader) {
95                $recipient = [];
96                // Borrowing a parser from the KeyValue class.
97                $keyValue = keyValue($reader);
98
99                if (isset($keyValue['{}WeightOz'])){
100                    $recipient['referenceId'] = $keyValue['{}WeightOz'];
101                }
102
103                return $recipient;
104            },
105        ];
106
107        $reader->parse();
108
109
110    }
111
112}
113