|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dgame\Soap\Test; |
|
4
|
|
|
|
|
5
|
|
|
use Dgame\Soap\Hydrator\Translator; |
|
6
|
|
|
use Dgame\Soap\XmlElement; |
|
7
|
|
|
use Dgame\Soap\XmlNode; |
|
8
|
|
|
use DOMDocument; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class TranslatorTest |
|
13
|
|
|
* @package Dgame\Soap\Test |
|
14
|
|
|
*/ |
|
15
|
|
|
final class TranslatorTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
public function testXmlElementTranslation() |
|
18
|
|
|
{ |
|
19
|
|
|
$doc = new DOMDocument('1.0'); |
|
20
|
|
|
$translator = new Translator(); |
|
21
|
|
|
|
|
22
|
|
|
$node = $translator->translateNode($doc->createElement('test', 'foobar')); |
|
23
|
|
|
|
|
24
|
|
|
$this->assertNotNull($node); |
|
25
|
|
|
$this->assertInstanceOf(XmlElement::class, $node); |
|
26
|
|
|
$this->assertEquals('test', $node->getName()); |
|
27
|
|
|
$this->assertTrue($node->hasValue()); |
|
28
|
|
|
$this->assertFalse($node->hasAttributes()); |
|
29
|
|
|
$this->assertEquals('test', $node->getName()); |
|
30
|
|
|
$this->assertEquals('foobar', $node->getValue()); |
|
31
|
|
|
|
|
32
|
|
|
$node = $translator->translateNode($doc->createElement('test', '42')); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertNotNull($node); |
|
35
|
|
|
$this->assertInstanceOf(XmlElement::class, $node); |
|
36
|
|
|
$this->assertEquals('test', $node->getName()); |
|
37
|
|
|
$this->assertTrue($node->hasValue()); |
|
38
|
|
|
$this->assertFalse($node->hasAttributes()); |
|
39
|
|
|
$this->assertEquals('test', $node->getName()); |
|
40
|
|
|
$this->assertEquals(42, $node->getValue()); |
|
41
|
|
|
|
|
42
|
|
|
$element = $doc->createElementNs('http://www.example.com/bar', 'bar:test', 'foobar'); |
|
43
|
|
|
$element->setAttribute('id', '0'); |
|
44
|
|
|
|
|
45
|
|
|
$node = $translator->translateNode($element); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertNotNull($node); |
|
48
|
|
|
$this->assertInstanceOf(XmlElement::class, $node); |
|
49
|
|
|
$this->assertEquals('test', $node->getName()); |
|
50
|
|
|
$this->assertTrue($node->hasValue()); |
|
51
|
|
|
$this->assertTrue($node->hasAttributes()); |
|
52
|
|
|
$this->assertTrue($node->hasPrefix()); |
|
53
|
|
|
$this->assertCount(1, $node->getAttributes()); |
|
54
|
|
|
$this->assertEquals('bar', $node->getPrefix()); |
|
55
|
|
|
$this->assertEquals('test', $node->getName()); |
|
56
|
|
|
$this->assertEquals('foobar', $node->getValue()); |
|
57
|
|
|
$this->assertEquals('id', $node->getAttributes()[0]->getName()); |
|
58
|
|
|
$this->assertEquals(0, $node->getAttributes()[0]->getValue()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testXmlNodeTranslation() |
|
62
|
|
|
{ |
|
63
|
|
|
$doc = new DOMDocument('1.0'); |
|
64
|
|
|
$translator = new Translator(); |
|
65
|
|
|
|
|
66
|
|
|
$element = $doc->createElementNs('http://www.example.com/abc', 'abc:test'); |
|
67
|
|
|
$element->setAttribute('id', '0'); |
|
68
|
|
|
$element->appendChild($doc->createElement('name', 'Franz')); |
|
69
|
|
|
$element->appendChild($doc->createElement('age', '42')); |
|
70
|
|
|
|
|
71
|
|
|
$node = $translator->translateNode($element); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertNotNull($node); |
|
74
|
|
|
$this->assertInstanceOf(XmlNode::class, $node); |
|
75
|
|
|
$this->assertEquals('test', $node->getName()); |
|
76
|
|
|
$this->assertFalse($node->hasValue()); |
|
77
|
|
|
$this->assertTrue($node->hasAttributes()); |
|
78
|
|
|
$this->assertTrue($node->hasElements()); |
|
79
|
|
|
$this->assertTrue($node->hasPrefix()); |
|
80
|
|
|
$this->assertEquals('abc', $node->getPrefix()); |
|
81
|
|
|
$this->assertEquals('test', $node->getName()); |
|
82
|
|
|
$this->assertFalse($node->hasValue()); |
|
83
|
|
|
$this->assertCount(1, $node->getAttributes()); |
|
84
|
|
|
$this->assertEquals('id', $node->getAttributes()[0]->getName()); |
|
85
|
|
|
$this->assertEquals(0, $node->getAttributes()[0]->getValue()); |
|
86
|
|
|
$this->assertInstanceOf(XmlElement::class, $node->getElements()[0]); |
|
87
|
|
|
$this->assertInstanceOf(XmlElement::class, $node->getElements()[1]); |
|
88
|
|
|
$this->assertCount(2, $node->getElements()); |
|
89
|
|
|
$this->assertEquals('name', $node->getElements()[0]->getName()); |
|
90
|
|
|
$this->assertEquals('Franz', $node->getElements()[0]->getValue()); |
|
91
|
|
|
$this->assertEquals('age', $node->getElements()[1]->getName()); |
|
92
|
|
|
$this->assertEquals(42, $node->getElements()[1]->getValue()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testDocumentTranslation() |
|
96
|
|
|
{ |
|
97
|
|
|
$doc = new DOMDocument('1.0'); |
|
98
|
|
|
$doc->load(__DIR__ . '/xml/test1.xml'); |
|
99
|
|
|
|
|
100
|
|
|
$translator = new Translator(); |
|
101
|
|
|
$envelope = $translator->translateDocument($doc); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertEquals('soap-env', $envelope->getName()); |
|
104
|
|
|
$this->assertCount(2, $envelope->getElements()); |
|
105
|
|
|
|
|
106
|
|
|
$child1 = $envelope->getElements()[0]; |
|
107
|
|
|
$child2 = $envelope->getElements()[1]; |
|
108
|
|
|
|
|
109
|
|
|
$this->assertInstanceOf(XmlNode::class, $child1); |
|
110
|
|
|
$this->assertInstanceOf(XmlNode::class, $child2); |
|
111
|
|
|
|
|
112
|
|
|
$this->assertEquals('person', $child1->getName()); |
|
113
|
|
|
$this->assertTrue($child1->hasElements()); |
|
114
|
|
|
$this->assertCount(4, $child1->getElements()); |
|
115
|
|
|
$this->assertTrue($child1->hasAttributes()); |
|
116
|
|
|
$this->assertCount(1, $child1->getAttributes()); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertEquals('name', $child1->getAttributes()[0]->getName()); |
|
119
|
|
|
$this->assertEquals('Max Musterman', $child1->getAttributes()[0]->getValue()); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertInstanceOf(XmlElement::class, $child1->getElements()[0]); |
|
122
|
|
|
$this->assertEquals('car', $child1->getElements()[0]->getName()); |
|
123
|
|
|
$this->assertFalse($child1->getElements()[0]->hasValue()); |
|
124
|
|
|
$this->assertCount(2, $child1->getElements()[0]->getAttributes()); |
|
125
|
|
|
$this->assertEquals('marke', $child1->getElements()[0]->getAttributes()[0]->getName()); |
|
126
|
|
|
$this->assertEquals('BMW', $child1->getElements()[0]->getAttributes()[0]->getValue()); |
|
127
|
|
|
|
|
128
|
|
|
$this->assertInstanceOf(XmlElement::class, $child1->getElements()[1]); |
|
129
|
|
|
$this->assertEquals('phone', $child1->getElements()[1]->getName()); |
|
130
|
|
|
$this->assertEquals(9, $child1->getElements()[1]->getValue()); |
|
131
|
|
|
$this->assertCount(1, $child1->getElements()[1]->getAttributes()); |
|
132
|
|
|
$this->assertEquals('name', $child1->getElements()[1]->getAttributes()[0]->getName()); |
|
133
|
|
|
$this->assertEquals('iPhone', $child1->getElements()[1]->getAttributes()[0]->getValue()); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertInstanceOf(XmlElement::class, $child1->getElements()[2]); |
|
136
|
|
|
$this->assertEquals('birth-place', $child1->getElements()[2]->getName()); |
|
137
|
|
|
$this->assertEquals('Hamburg', $child1->getElements()[2]->getValue()); |
|
138
|
|
|
$this->assertFalse($child1->getElements()[2]->hasAttributes()); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertInstanceOf(XmlNode::class, $child1->getElements()[3]); |
|
141
|
|
|
$this->assertEquals('address', $child1->getElements()[3]->getName()); |
|
142
|
|
|
$this->assertFalse($child1->getElements()[3]->hasValue()); |
|
143
|
|
|
$this->assertFalse($child1->getElements()[3]->hasAttributes()); |
|
144
|
|
|
$this->assertCount(2, $child1->getElements()[3]->getElements()); |
|
145
|
|
|
|
|
146
|
|
|
$i = 0; |
|
147
|
|
|
foreach (['street' => 'Hauptstraße 1', 'plz' => '245698'] as $name => $value) { |
|
148
|
|
|
$this->assertEquals($name, $child1->getElements()[3]->getElements()[$i]->getName()); |
|
149
|
|
|
$this->assertEquals($value, $child1->getElements()[3]->getElements()[$i]->getValue()); |
|
150
|
|
|
|
|
151
|
|
|
$i++; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$this->assertEquals('person', $child2->getName()); |
|
155
|
|
|
$this->assertTrue($child2->hasAttributes()); |
|
156
|
|
|
$this->assertCount(1, $child2->getAttributes()); |
|
157
|
|
|
$this->assertTrue($child2->hasElements()); |
|
158
|
|
|
$this->assertCount(4, $child2->getElements()); |
|
159
|
|
|
|
|
160
|
|
|
$this->assertEquals('name', $child2->getAttributes()[0]->getName()); |
|
161
|
|
|
$this->assertEquals('Dr. Dolittle', $child2->getAttributes()[0]->getValue()); |
|
162
|
|
|
|
|
163
|
|
|
$this->assertInstanceOf(XmlElement::class, $child2->getElements()[0]); |
|
164
|
|
|
$this->assertEquals('car', $child2->getElements()[0]->getName()); |
|
165
|
|
|
$this->assertFalse($child2->getElements()[0]->hasValue()); |
|
166
|
|
|
$this->assertCount(2, $child2->getElements()[0]->getAttributes()); |
|
167
|
|
|
$this->assertEquals('marke', $child2->getElements()[0]->getAttributes()[0]->getName()); |
|
168
|
|
|
$this->assertEquals('Audi', $child2->getElements()[0]->getAttributes()[0]->getValue()); |
|
169
|
|
|
|
|
170
|
|
|
$this->assertInstanceOf(XmlElement::class, $child2->getElements()[1]); |
|
171
|
|
|
$this->assertEquals('phone', $child2->getElements()[1]->getName()); |
|
172
|
|
|
$this->assertEquals('Xperia Z3', $child2->getElements()[1]->getValue()); |
|
173
|
|
|
$this->assertCount(1, $child2->getElements()[1]->getAttributes()); |
|
174
|
|
|
$this->assertEquals('name', $child2->getElements()[1]->getAttributes()[0]->getName()); |
|
175
|
|
|
$this->assertEquals('Sony', $child2->getElements()[1]->getAttributes()[0]->getValue()); |
|
176
|
|
|
|
|
177
|
|
|
$this->assertInstanceOf(XmlElement::class, $child2->getElements()[2]); |
|
178
|
|
|
$this->assertEquals('birth-place', $child2->getElements()[2]->getName()); |
|
179
|
|
|
$this->assertEquals('München', $child2->getElements()[2]->getValue()); |
|
180
|
|
|
$this->assertFalse($child2->getElements()[2]->hasAttributes()); |
|
181
|
|
|
|
|
182
|
|
|
$this->assertInstanceOf(XmlNode::class, $child2->getElements()[3]); |
|
183
|
|
|
$this->assertEquals('address', $child2->getElements()[3]->getName()); |
|
184
|
|
|
$this->assertFalse($child2->getElements()[3]->hasValue()); |
|
185
|
|
|
$this->assertFalse($child2->getElements()[3]->hasAttributes()); |
|
186
|
|
|
$this->assertCount(2, $child2->getElements()[3]->getElements()); |
|
187
|
|
|
|
|
188
|
|
|
$i = 0; |
|
189
|
|
|
foreach (['street' => 'Partkstraße', 'plz' => '365494'] as $name => $value) { |
|
190
|
|
|
$this->assertEquals($name, $child2->getElements()[3]->getElements()[$i]->getName()); |
|
191
|
|
|
$this->assertEquals($value, $child2->getElements()[3]->getElements()[$i]->getValue()); |
|
192
|
|
|
|
|
193
|
|
|
$i++; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
} |