1 | <?php |
||
31 | final class HydrationTest extends TestCase |
||
32 | { |
||
33 | public function testObjects() |
||
34 | { |
||
35 | $doc = new DOMDocument(); |
||
36 | $doc->load(__DIR__ . '/xml/test1.xml'); |
||
37 | |||
38 | $mapper = new ClassMapper( |
||
39 | [ |
||
40 | 'Root' => TestRoot::class, |
||
41 | 'Person' => TestPerson::class, |
||
42 | 'Car' => TestCar::class, |
||
43 | 'Phone' => TestPhone::class, |
||
44 | 'Address' => TestAddress::class |
||
45 | ] |
||
46 | ); |
||
47 | $mapper->appendPattern('/^(?:soap\-?)?env(?:elope)?/iS', 'Root'); |
||
48 | |||
49 | $hydrator = new Hydrator($mapper); |
||
50 | /** @var TestRoot $root */ |
||
51 | $root = $hydrator->hydrate($doc); |
||
52 | |||
53 | $this->assertNotNull($root); |
||
54 | $this->assertInstanceOf(TestRoot::class, $root); |
||
55 | |||
56 | /** @var TestPerson[] $persons */ |
||
57 | $persons = $root->getPersons(); |
||
58 | $this->assertCount(2, $persons); |
||
59 | |||
60 | $this->assertInstanceOf(TestPerson::class, $persons[0]); |
||
61 | $this->assertEquals('Max Musterman', $persons[0]->getName()); |
||
62 | $this->assertInstanceOf(TestCar::class, $persons[0]->getCar()); |
||
63 | $this->assertEquals('BMW', $persons[0]->getCar()->getMarke()); |
||
64 | $this->assertNotEmpty($persons[0]->getCar()->kennung); |
||
65 | $this->assertEquals('i8', $persons[0]->getCar()->kennung); |
||
66 | $this->assertInstanceOf(TestPhone::class, $persons[0]->getPhone()); |
||
67 | $this->assertEquals('iPhone', $persons[0]->getPhone()->getName()); |
||
68 | $this->assertEquals(9, $persons[0]->getPhone()->getValue()); |
||
69 | $this->assertEquals('Hamburg', $persons[0]->getBirthplace()); |
||
70 | $this->assertInstanceOf(TestAddress::class, $persons[0]->getAddress()); |
||
71 | $this->assertEquals('Hauptstraße 1', $persons[0]->getAddress()->getStreet()); |
||
72 | $this->assertEquals(245698, $persons[0]->getAddress()->getPlz()); |
||
73 | |||
74 | $this->assertInstanceOf(TestPerson::class, $persons[1]); |
||
75 | $this->assertEquals('Dr. Dolittle', $persons[1]->getName()); |
||
76 | $this->assertInstanceOf(TestCar::class, $persons[1]->getCar()); |
||
77 | $this->assertEquals('Audi', $persons[1]->getCar()->getMarke()); |
||
78 | $this->assertNotEmpty($persons[0]->getCar()->kennung); |
||
79 | $this->assertEquals('A3', $persons[1]->getCar()->kennung); |
||
80 | $this->assertInstanceOf(TestPhone::class, $persons[1]->getPhone()); |
||
81 | $this->assertEquals('Sony', $persons[1]->getPhone()->getName()); |
||
82 | $this->assertEquals('Xperia Z3', $persons[1]->getPhone()->getValue()); |
||
83 | $this->assertEquals('München', $persons[1]->getBirthplace()); |
||
84 | $this->assertInstanceOf(TestAddress::class, $persons[1]->getAddress()); |
||
85 | $this->assertEquals('Partkstraße', $persons[1]->getAddress()->getStreet()); |
||
86 | $this->assertEquals(365494, $persons[1]->getAddress()->getPlz()); |
||
87 | } |
||
88 | |||
89 | public function testWithoutFirstObject() |
||
90 | { |
||
91 | $doc = new DOMDocument(); |
||
92 | $doc->loadXml('<root><Car marke="Mercedes" /></root>'); |
||
93 | |||
94 | $mapper = new ClassMapper( |
||
95 | [ |
||
96 | 'Car' => TestCar::class |
||
97 | ] |
||
98 | ); |
||
99 | |||
100 | $hydrator = new Hydrator($mapper); |
||
101 | /** @var TestCar $car */ |
||
102 | $car = $hydrator->hydrate($doc); |
||
103 | |||
104 | $this->assertInstanceOf(TestCar::class, $car); |
||
105 | $this->assertEquals('Mercedes', $car->getMarke()); |
||
106 | } |
||
107 | |||
108 | public function testWithLowerCaseClassMap() |
||
109 | { |
||
110 | // $this->markTestSkipped(); |
||
111 | $doc = new DOMDocument(); |
||
112 | $doc->loadXml('<root><Car marke="Mercedes" /></root>'); |
||
113 | |||
114 | $mapper = new ClassMapper( |
||
115 | [ |
||
116 | 'car' => TestCar::class |
||
117 | ] |
||
118 | ); |
||
119 | |||
120 | $hydrator = new Hydrator($mapper); |
||
121 | /** @var TestCar $car */ |
||
122 | $car = $hydrator->hydrate($doc); |
||
123 | |||
124 | $this->assertInstanceOf(TestCar::class, $car); |
||
125 | $this->assertEquals('Mercedes', $car->getMarke()); |
||
126 | } |
||
127 | |||
128 | public function testWithFacadeMethod() |
||
129 | { |
||
130 | $doc = new DOMDocument(); |
||
131 | $doc->loadXml('<root><Person><birthday>14.08.1991</birthday></Person></root>'); |
||
132 | |||
133 | $mapper = new ClassMapper( |
||
134 | [ |
||
135 | 'person' => TestPerson::class |
||
136 | ] |
||
137 | ); |
||
138 | |||
139 | $hydrator = new Hydrator($mapper); |
||
140 | /** @var TestPerson $person */ |
||
141 | $person = $hydrator->hydrate($doc); |
||
142 | |||
143 | $this->assertInstanceOf(TestPerson::class, $person); |
||
144 | $this->assertEquals('14.08.1991', $person->getBirthday()->format('d.m.Y')); |
||
145 | } |
||
146 | |||
147 | public function testWithTagName() |
||
148 | { |
||
149 | $doc = new DOMDocument(); |
||
150 | $doc->loadXml('<root><Person><hobby>Radeln</hobby></Person></root>'); |
||
151 | |||
152 | $mapper = new ClassMapper( |
||
153 | [ |
||
154 | 'person' => TestPerson::class, |
||
155 | 'hobby' => TestHobby::class |
||
156 | ] |
||
157 | ); |
||
158 | |||
159 | $hydrator = new Hydrator($mapper); |
||
160 | /** @var TestPerson $person */ |
||
161 | $person = $hydrator->hydrate($doc); |
||
162 | |||
163 | $this->assertInstanceOf(TestPerson::class, $person); |
||
164 | $this->assertInstanceOf(TestHobby::class, $person->hobby); |
||
165 | $this->assertEquals('Radeln', $person->hobby->value); |
||
166 | } |
||
167 | |||
168 | public function testPropertyAssignment() |
||
169 | { |
||
170 | $doc = new DOMDocument(); |
||
171 | $doc->load(__DIR__ . '/xml/test2.xml'); |
||
172 | |||
173 | $mapper = new ClassMapper( |
||
174 | [ |
||
175 | 'Stammdaten' => TestStammdaten::class |
||
176 | ] |
||
177 | ); |
||
178 | |||
179 | $hydrator = new Hydrator($mapper); |
||
180 | /** @var TestStammdaten $stammdaten */ |
||
181 | $stammdaten = $hydrator->hydrate($doc); |
||
182 | |||
183 | $this->assertInstanceOf(TestStammdaten::class, $stammdaten); |
||
184 | $this->assertEquals('Muster', $stammdaten->Name); |
||
185 | $this->assertEquals('Max', $stammdaten->Vorname); |
||
186 | } |
||
187 | |||
188 | public function testFault() |
||
189 | { |
||
190 | $doc = new DOMDocument(); |
||
191 | $doc->load(__DIR__ . '/xml/fault.xml'); |
||
192 | |||
193 | $mapper = new ClassMapper( |
||
194 | [ |
||
195 | 'Envelope' => TestEnvelope::class, |
||
196 | 'Body' => TestBody::class, |
||
197 | 'Fault' => TestFault::class |
||
198 | ] |
||
199 | ); |
||
200 | |||
201 | $hydrator = new Hydrator($mapper); |
||
202 | /** @var TestEnvelope $envelope */ |
||
203 | $envelope = $hydrator->hydrate($doc); |
||
204 | |||
205 | $this->assertInstanceOf(TestEnvelope::class, $envelope); |
||
206 | $this->assertTrue($envelope->getBody()->hasFault()); |
||
207 | $this->assertEquals('Fehler!', $envelope->getBody()->getFault()->getFaultcode()); |
||
208 | $this->assertEquals('Es ist ein Fehler aufgetreten', $envelope->getBody()->getFault()->getFaultstring()); |
||
209 | } |
||
210 | |||
211 | public function testList() |
||
212 | { |
||
213 | $doc = new DOMDocument(); |
||
214 | $doc->load(__DIR__ . '/xml/list.xml'); |
||
215 | |||
216 | $mapper = new ClassMapper( |
||
217 | [ |
||
218 | 'Envelope' => TestEnvelope::class, |
||
219 | 'Body' => TestBody::class, |
||
220 | 'Fault' => TestFault::class, |
||
221 | 'Result' => TestResult::class, |
||
222 | 'Ort' => TestOrt::class, |
||
223 | 'Oteil' => TestOrtsTeil::class, |
||
224 | 'Strassen' => TestStrassen::class |
||
225 | ] |
||
226 | ); |
||
227 | |||
228 | $hydrator = new Hydrator($mapper); |
||
229 | /** @var TestEnvelope $envelope */ |
||
230 | $envelope = $hydrator->hydrate($doc); |
||
231 | |||
232 | $this->assertInstanceOf(TestEnvelope::class, $envelope); |
||
233 | $this->assertFalse($envelope->getBody()->hasFault()); |
||
234 | $this->assertInstanceOf(TestResult::class, $envelope->getBody()->getResult()); |
||
235 | $this->assertCount(1, $envelope->getBody()->getResult()->getOrte()); |
||
236 | $this->assertEquals('Hamburg', $envelope->getBody()->getResult()->getOrte()[0]->getName()); |
||
237 | $this->assertCount(4, $envelope->getBody()->getResult()->getOrte()[0]->getOrtsteile()); |
||
238 | $this->assertEquals('Hamburg-Altstadt', $envelope->getBody()->getResult()->getOrte()[0]->getOrtsteile()[1]->getName()); |
||
239 | |||
240 | for ($i = 0; $i < 4; $i++) { |
||
241 | $this->assertNotEmpty($envelope->getBody()->getResult()->getOrte()[0]->getOrtsteile()[$i]->getStrassen()); |
||
242 | } |
||
243 | } |
||
244 | |||
245 | public function testFailedPropertyAssignment() |
||
246 | { |
||
247 | $doc = new DOMDocument(); |
||
248 | $doc->load(__DIR__ . '/xml/test2.xml'); |
||
249 | |||
250 | $handler = new TestHandler(); |
||
251 | $log = new Logger(Hydrator::class); |
||
252 | $log->pushHandler($handler); |
||
253 | $log->pushProcessor(new PsrLogMessageProcessor()); |
||
254 | |||
255 | Registry::removeLogger(Hydrator::class); |
||
256 | Registry::addLogger($log); |
||
257 | |||
258 | $mapper = new ClassMapper(['mandant' => TestPerson::class]); |
||
259 | $hydrator = new Hydrator($mapper); |
||
260 | $hydrator->hydrate($doc); |
||
261 | |||
262 | $this->assertTrue($handler->hasWarningRecords()); |
||
263 | $this->assertTrue($handler->hasWarning('Could neither hydrate or assign Stammdaten')); |
||
264 | $this->assertTrue($handler->hasWarning('Could neither hydrate or assign Name')); |
||
265 | $this->assertTrue($handler->hasWarning('Could neither hydrate or assign Vorname')); |
||
266 | |||
267 | $handler->clear(); |
||
268 | |||
269 | $mapper = new ClassMapper(); |
||
270 | $hydrator = new Hydrator($mapper); |
||
271 | $hydrator->hydrate($doc); |
||
272 | |||
273 | $this->assertTrue($handler->hasWarningRecords()); |
||
274 | $this->assertTrue($handler->hasWarning('Could neither hydrate or assign Mandant')); |
||
275 | $this->assertTrue($handler->hasWarning('Could neither hydrate or assign Stammdaten')); |
||
276 | $this->assertTrue($handler->hasWarning('Could neither hydrate or assign Name')); |
||
277 | $this->assertTrue($handler->hasWarning('Could neither hydrate or assign Vorname')); |
||
278 | } |
||
279 | } |