|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dgame\Soap\Test; |
|
4
|
|
|
|
|
5
|
|
|
use Dgame\Soap\Hydrator\ClassMapper; |
|
6
|
|
|
use Dgame\Soap\Hydrator\Dom\Hydrator; |
|
7
|
|
|
use Dgame\Soap\Test\Object\TestAddress; |
|
8
|
|
|
use Dgame\Soap\Test\Object\TestBody; |
|
9
|
|
|
use Dgame\Soap\Test\Object\TestCar; |
|
10
|
|
|
use Dgame\Soap\Test\Object\TestEnvelope; |
|
11
|
|
|
use Dgame\Soap\Test\Object\TestFault; |
|
12
|
|
|
use Dgame\Soap\Test\Object\TestOrt; |
|
13
|
|
|
use Dgame\Soap\Test\Object\TestOrtsTeil; |
|
14
|
|
|
use Dgame\Soap\Test\Object\TestPerson; |
|
15
|
|
|
use Dgame\Soap\Test\Object\TestPhone; |
|
16
|
|
|
use Dgame\Soap\Test\Object\TestResult; |
|
17
|
|
|
use Dgame\Soap\Test\Object\TestRoot; |
|
18
|
|
|
use Dgame\Soap\Test\Object\TestStammdaten; |
|
19
|
|
|
use Dgame\Soap\Test\Object\TestStrassen; |
|
20
|
|
|
use DOMDocument; |
|
21
|
|
|
use PHPUnit\Framework\TestCase; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class HydrationTest |
|
25
|
|
|
*/ |
|
26
|
|
|
final class HydrationTest extends TestCase |
|
27
|
|
|
{ |
|
28
|
|
|
public function testObjects() |
|
29
|
|
|
{ |
|
30
|
|
|
$doc = new DOMDocument(); |
|
31
|
|
|
$doc->load(__DIR__ . '/xml/test1.xml'); |
|
32
|
|
|
|
|
33
|
|
|
$mapper = new ClassMapper( |
|
34
|
|
|
[ |
|
35
|
|
|
'Root' => TestRoot::class, |
|
36
|
|
|
'Person' => TestPerson::class, |
|
37
|
|
|
'Car' => TestCar::class, |
|
38
|
|
|
'Phone' => TestPhone::class, |
|
39
|
|
|
'Address' => TestAddress::class |
|
40
|
|
|
] |
|
41
|
|
|
); |
|
42
|
|
|
$mapper->appendPattern('/^(?:soap\-?)?env(?:elope)?/iS', 'Root'); |
|
43
|
|
|
|
|
44
|
|
|
$hydrator = new Hydrator($mapper); |
|
45
|
|
|
/** @var TestRoot $root */ |
|
46
|
|
|
$root = $hydrator->hydrateDocument($doc); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertNotNull($root); |
|
49
|
|
|
$this->assertInstanceOf(TestRoot::class, $root); |
|
50
|
|
|
|
|
51
|
|
|
/** @var TestPerson[] $persons */ |
|
52
|
|
|
$persons = $root->getPersons(); |
|
53
|
|
|
$this->assertCount(2, $persons); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertInstanceOf(TestPerson::class, $persons[0]); |
|
56
|
|
|
$this->assertEquals('Max Musterman', $persons[0]->getName()); |
|
57
|
|
|
$this->assertInstanceOf(TestCar::class, $persons[0]->getCar()); |
|
58
|
|
|
$this->assertEquals('BMW', $persons[0]->getCar()->getMarke()); |
|
59
|
|
|
$this->assertNotEmpty($persons[0]->getCar()->kennung); |
|
60
|
|
|
$this->assertEquals('i8', $persons[0]->getCar()->kennung); |
|
61
|
|
|
$this->assertInstanceOf(TestPhone::class, $persons[0]->getPhone()); |
|
62
|
|
|
$this->assertEquals('iPhone', $persons[0]->getPhone()->getName()); |
|
63
|
|
|
$this->assertEquals(9, $persons[0]->getPhone()->getValue()); |
|
64
|
|
|
$this->assertEquals('Hamburg', $persons[0]->getBirthplace()); |
|
65
|
|
|
$this->assertInstanceOf(TestAddress::class, $persons[0]->getAddress()); |
|
66
|
|
|
$this->assertEquals('Hauptstraße 1', $persons[0]->getAddress()->getStreet()); |
|
67
|
|
|
$this->assertEquals(245698, $persons[0]->getAddress()->getPlz()); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertInstanceOf(TestPerson::class, $persons[1]); |
|
70
|
|
|
$this->assertEquals('Dr. Dolittle', $persons[1]->getName()); |
|
71
|
|
|
$this->assertInstanceOf(TestCar::class, $persons[1]->getCar()); |
|
72
|
|
|
$this->assertEquals('Audi', $persons[1]->getCar()->getMarke()); |
|
73
|
|
|
$this->assertNotEmpty($persons[0]->getCar()->kennung); |
|
74
|
|
|
$this->assertEquals('A3', $persons[1]->getCar()->kennung); |
|
75
|
|
|
$this->assertInstanceOf(TestPhone::class, $persons[1]->getPhone()); |
|
76
|
|
|
$this->assertEquals('Sony', $persons[1]->getPhone()->getName()); |
|
77
|
|
|
$this->assertEquals('Xperia Z3', $persons[1]->getPhone()->getValue()); |
|
78
|
|
|
$this->assertEquals('München', $persons[1]->getBirthplace()); |
|
79
|
|
|
$this->assertInstanceOf(TestAddress::class, $persons[1]->getAddress()); |
|
80
|
|
|
$this->assertEquals('Partkstraße', $persons[1]->getAddress()->getStreet()); |
|
81
|
|
|
$this->assertEquals(365494, $persons[1]->getAddress()->getPlz()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testWithoutFirstObject() |
|
85
|
|
|
{ |
|
86
|
|
|
$doc = new DOMDocument(); |
|
87
|
|
|
$doc->loadXml('<root><Car marke="Mercedes" /></root>'); |
|
88
|
|
|
|
|
89
|
|
|
$mapper = new ClassMapper( |
|
90
|
|
|
[ |
|
91
|
|
|
'Car' => TestCar::class |
|
92
|
|
|
] |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
$hydrator = new Hydrator($mapper); |
|
96
|
|
|
/** @var TestCar $car */ |
|
97
|
|
|
$car = $hydrator->hydrateDocument($doc); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertInstanceOf(TestCar::class, $car); |
|
100
|
|
|
$this->assertEquals('Mercedes', $car->getMarke()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function testPropertyAssignment() |
|
104
|
|
|
{ |
|
105
|
|
|
$doc = new DOMDocument(); |
|
106
|
|
|
$doc->load(__DIR__ . '/xml/test2.xml'); |
|
107
|
|
|
|
|
108
|
|
|
$mapper = new ClassMapper( |
|
109
|
|
|
[ |
|
110
|
|
|
'Stammdaten' => TestStammdaten::class |
|
111
|
|
|
] |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
$hydrator = new Hydrator($mapper); |
|
115
|
|
|
/** @var TestStammdaten $stammdaten */ |
|
116
|
|
|
$stammdaten = $hydrator->hydrateDocument($doc); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertInstanceOf(TestStammdaten::class, $stammdaten); |
|
119
|
|
|
$this->assertEquals('Muster', $stammdaten->Name); |
|
120
|
|
|
$this->assertEquals('Max', $stammdaten->Vorname); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testFault() |
|
124
|
|
|
{ |
|
125
|
|
|
$doc = new DOMDocument(); |
|
126
|
|
|
$doc->load(__DIR__ . '/xml/fault.xml'); |
|
127
|
|
|
|
|
128
|
|
|
$mapper = new ClassMapper( |
|
129
|
|
|
[ |
|
130
|
|
|
'Envelope' => TestEnvelope::class, |
|
131
|
|
|
'Body' => TestBody::class, |
|
132
|
|
|
'Fault' => TestFault::class |
|
133
|
|
|
] |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
|
|
$hydrator = new Hydrator($mapper); |
|
137
|
|
|
/** @var TestEnvelope $envelope */ |
|
138
|
|
|
$envelope = $hydrator->hydrateDocument($doc); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertInstanceOf(TestEnvelope::class, $envelope); |
|
141
|
|
|
$this->assertTrue($envelope->getBody()->hasFault()); |
|
142
|
|
|
$this->assertEquals('Fehler!', $envelope->getBody()->getFault()->getFaultcode()); |
|
143
|
|
|
$this->assertEquals('Es ist ein Fehler aufgetreten', $envelope->getBody()->getFault()->getFaultstring()); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function testList() |
|
147
|
|
|
{ |
|
148
|
|
|
$doc = new DOMDocument(); |
|
149
|
|
|
$doc->load(__DIR__ . '/xml/list.xml'); |
|
150
|
|
|
|
|
151
|
|
|
$mapper = new ClassMapper( |
|
152
|
|
|
[ |
|
153
|
|
|
'Envelope' => TestEnvelope::class, |
|
154
|
|
|
'Body' => TestBody::class, |
|
155
|
|
|
'Fault' => TestFault::class, |
|
156
|
|
|
'Result' => TestResult::class, |
|
157
|
|
|
'Ort' => TestOrt::class, |
|
158
|
|
|
'Oteil' => TestOrtsTeil::class, |
|
159
|
|
|
'Strassen' => TestStrassen::class |
|
160
|
|
|
] |
|
161
|
|
|
); |
|
162
|
|
|
|
|
163
|
|
|
$hydrator = new Hydrator($mapper); |
|
164
|
|
|
/** @var TestEnvelope $envelope */ |
|
165
|
|
|
$envelope = $hydrator->hydrateDocument($doc); |
|
166
|
|
|
|
|
167
|
|
|
$this->assertInstanceOf(TestEnvelope::class, $envelope); |
|
168
|
|
|
$this->assertFalse($envelope->getBody()->hasFault()); |
|
169
|
|
|
$this->assertInstanceOf(TestResult::class, $envelope->getBody()->getResult()); |
|
170
|
|
|
$this->assertCount(1, $envelope->getBody()->getResult()->getOrte()); |
|
171
|
|
|
$this->assertEquals('Hamburg', $envelope->getBody()->getResult()->getOrte()[0]->getName()); |
|
172
|
|
|
$this->assertCount(4, $envelope->getBody()->getResult()->getOrte()[0]->getOrtsteile()); |
|
173
|
|
|
$this->assertEquals('Hamburg-Altstadt', $envelope->getBody()->getResult()->getOrte()[0]->getOrtsteile()[1]->getName()); |
|
174
|
|
|
|
|
175
|
|
|
for ($i = 0; $i < 4; $i++) { |
|
176
|
|
|
$this->assertNotEmpty($envelope->getBody()->getResult()->getOrte()[0]->getOrtsteile()[$i]->getStrassen()); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |