1<?php
2
3namespace Sabre\XML\Deserializer;
4
5use
6    Sabre\Xml\Reader;
7
8class ValueObjectTest extends \PHPUnit_Framework_TestCase {
9
10    function testDeserializeValueObject() {
11
12        $input = <<<XML
13<?xml version="1.0"?>
14<foo xmlns="urn:foo">
15   <firstName>Harry</firstName>
16   <lastName>Turtle</lastName>
17</foo>
18XML;
19
20        $reader = new Reader();
21        $reader->xml($input);
22        $reader->elementMap = [
23            '{urn:foo}foo' => function(Reader $reader) {
24                return valueObject($reader, 'Sabre\\Xml\\Deserializer\\TestVo', 'urn:foo');
25            }
26        ];
27
28        $output = $reader->parse();
29
30        $vo = new TestVo();
31        $vo->firstName = 'Harry';
32        $vo->lastName = 'Turtle';
33
34        $expected = [
35            'name'       => '{urn:foo}foo',
36            'value'      => $vo,
37            'attributes' => []
38        ];
39
40        $this->assertEquals(
41            $expected,
42            $output
43        );
44
45    }
46
47    function testDeserializeValueObjectIgnoredElement() {
48
49        $input = <<<XML
50<?xml version="1.0"?>
51<foo xmlns="urn:foo">
52   <firstName>Harry</firstName>
53   <lastName>Turtle</lastName>
54   <email>harry@example.org</email>
55</foo>
56XML;
57
58        $reader = new Reader();
59        $reader->xml($input);
60        $reader->elementMap = [
61            '{urn:foo}foo' => function(Reader $reader) {
62                return valueObject($reader, 'Sabre\\Xml\\Deserializer\\TestVo', 'urn:foo');
63            }
64        ];
65
66        $output = $reader->parse();
67
68        $vo = new TestVo();
69        $vo->firstName = 'Harry';
70        $vo->lastName = 'Turtle';
71
72        $expected = [
73            'name'       => '{urn:foo}foo',
74            'value'      => $vo,
75            'attributes' => []
76        ];
77
78        $this->assertEquals(
79            $expected,
80            $output
81        );
82
83    }
84
85    function testDeserializeValueObjectAutoArray() {
86
87        $input = <<<XML
88<?xml version="1.0"?>
89<foo xmlns="urn:foo">
90   <firstName>Harry</firstName>
91   <lastName>Turtle</lastName>
92   <link>http://example.org/</link>
93   <link>http://example.net/</link>
94</foo>
95XML;
96
97        $reader = new Reader();
98        $reader->xml($input);
99        $reader->elementMap = [
100            '{urn:foo}foo' => function(Reader $reader) {
101                return valueObject($reader, 'Sabre\\Xml\\Deserializer\\TestVo', 'urn:foo');
102            }
103        ];
104
105        $output = $reader->parse();
106
107        $vo = new TestVo();
108        $vo->firstName = 'Harry';
109        $vo->lastName = 'Turtle';
110        $vo->link = [
111            'http://example.org/',
112            'http://example.net/',
113        ];
114
115
116        $expected = [
117            'name'       => '{urn:foo}foo',
118            'value'      => $vo,
119            'attributes' => []
120        ];
121
122        $this->assertEquals(
123            $expected,
124            $output
125        );
126
127    }
128    function testDeserializeValueObjectEmpty() {
129
130        $input = <<<XML
131<?xml version="1.0"?>
132<foo xmlns="urn:foo" />
133XML;
134
135        $reader = new Reader();
136        $reader->xml($input);
137        $reader->elementMap = [
138            '{urn:foo}foo' => function(Reader $reader) {
139                return valueObject($reader, 'Sabre\\Xml\\Deserializer\\TestVo', 'urn:foo');
140            }
141        ];
142
143        $output = $reader->parse();
144
145        $vo = new TestVo();
146
147        $expected = [
148            'name'       => '{urn:foo}foo',
149            'value'      => $vo,
150            'attributes' => []
151        ];
152
153        $this->assertEquals(
154            $expected,
155            $output
156        );
157
158    }
159
160}
161
162class TestVo {
163
164    public $firstName;
165    public $lastName;
166
167    public $link = [];
168
169}
170