Completed
Push — master ( e80ea0...ba2e48 )
by Randy
02:42
created

HydrationTest::testWithTagName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
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\TestHobby;
13
use Dgame\Soap\Test\Object\TestOrt;
14
use Dgame\Soap\Test\Object\TestOrtsTeil;
15
use Dgame\Soap\Test\Object\TestPerson;
16
use Dgame\Soap\Test\Object\TestPhone;
17
use Dgame\Soap\Test\Object\TestResult;
18
use Dgame\Soap\Test\Object\TestRoot;
19
use Dgame\Soap\Test\Object\TestStammdaten;
20
use Dgame\Soap\Test\Object\TestStrassen;
21
use DOMDocument;
22
use PHPUnit\Framework\TestCase;
23
24
/**
25
 * Class HydrationTest
26
 */
27
final class HydrationTest extends TestCase
28
{
29
    public function testObjects()
30
    {
31
        $doc = new DOMDocument();
32
        $doc->load(__DIR__ . '/xml/test1.xml');
33
34
        $mapper = new ClassMapper(
35
            [
36
                'Root'    => TestRoot::class,
37
                'Person'  => TestPerson::class,
38
                'Car'     => TestCar::class,
39
                'Phone'   => TestPhone::class,
40
                'Address' => TestAddress::class
41
            ]
42
        );
43
        $mapper->appendPattern('/^(?:soap\-?)?env(?:elope)?/iS', 'Root');
44
45
        $hydrator = new Hydrator($mapper);
46
        /** @var TestRoot $root */
47
        $root = $hydrator->hydrateDocument($doc);
48
49
        $this->assertNotNull($root);
50
        $this->assertInstanceOf(TestRoot::class, $root);
51
52
        /** @var TestPerson[] $persons */
53
        $persons = $root->getPersons();
54
        $this->assertCount(2, $persons);
55
56
        $this->assertInstanceOf(TestPerson::class, $persons[0]);
57
        $this->assertEquals('Max Musterman', $persons[0]->getName());
58
        $this->assertInstanceOf(TestCar::class, $persons[0]->getCar());
59
        $this->assertEquals('BMW', $persons[0]->getCar()->getMarke());
60
        $this->assertNotEmpty($persons[0]->getCar()->kennung);
61
        $this->assertEquals('i8', $persons[0]->getCar()->kennung);
62
        $this->assertInstanceOf(TestPhone::class, $persons[0]->getPhone());
63
        $this->assertEquals('iPhone', $persons[0]->getPhone()->getName());
64
        $this->assertEquals(9, $persons[0]->getPhone()->getValue());
65
        $this->assertEquals('Hamburg', $persons[0]->getBirthplace());
66
        $this->assertInstanceOf(TestAddress::class, $persons[0]->getAddress());
67
        $this->assertEquals('Hauptstraße 1', $persons[0]->getAddress()->getStreet());
68
        $this->assertEquals(245698, $persons[0]->getAddress()->getPlz());
69
70
        $this->assertInstanceOf(TestPerson::class, $persons[1]);
71
        $this->assertEquals('Dr. Dolittle', $persons[1]->getName());
72
        $this->assertInstanceOf(TestCar::class, $persons[1]->getCar());
73
        $this->assertEquals('Audi', $persons[1]->getCar()->getMarke());
74
        $this->assertNotEmpty($persons[0]->getCar()->kennung);
75
        $this->assertEquals('A3', $persons[1]->getCar()->kennung);
76
        $this->assertInstanceOf(TestPhone::class, $persons[1]->getPhone());
77
        $this->assertEquals('Sony', $persons[1]->getPhone()->getName());
78
        $this->assertEquals('Xperia Z3', $persons[1]->getPhone()->getValue());
79
        $this->assertEquals('München', $persons[1]->getBirthplace());
80
        $this->assertInstanceOf(TestAddress::class, $persons[1]->getAddress());
81
        $this->assertEquals('Partkstraße', $persons[1]->getAddress()->getStreet());
82
        $this->assertEquals(365494, $persons[1]->getAddress()->getPlz());
83
    }
84
85
    public function testWithoutFirstObject()
86
    {
87
        $doc = new DOMDocument();
88
        $doc->loadXml('<root><Car marke="Mercedes" /></root>');
89
90
        $mapper = new ClassMapper(
91
            [
92
                'Car' => TestCar::class
93
            ]
94
        );
95
96
        $hydrator = new Hydrator($mapper);
97
        /** @var TestCar $car */
98
        $car = $hydrator->hydrateDocument($doc);
99
100
        $this->assertInstanceOf(TestCar::class, $car);
101
        $this->assertEquals('Mercedes', $car->getMarke());
102
    }
103
104
    public function testWithLowerCaseClassMap()
105
    {
106
        //        $this->markTestSkipped();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
107
        $doc = new DOMDocument();
108
        $doc->loadXml('<root><Car marke="Mercedes" /></root>');
109
110
        $mapper = new ClassMapper(
111
            [
112
                'car' => TestCar::class
113
            ]
114
        );
115
116
        $hydrator = new Hydrator($mapper);
117
        /** @var TestCar $car */
118
        $car = $hydrator->hydrateDocument($doc);
119
120
        $this->assertInstanceOf(TestCar::class, $car);
121
        $this->assertEquals('Mercedes', $car->getMarke());
122
    }
123
124
    public function testWithFacadeMethod()
125
    {
126
        $doc = new DOMDocument();
127
        $doc->loadXml('<root><Person><birthday>14.08.1991</birthday></Person></root>');
128
129
        $mapper = new ClassMapper(
130
            [
131
                'person' => TestPerson::class
132
            ]
133
        );
134
135
        $hydrator = new Hydrator($mapper);
136
        /** @var TestPerson $person */
137
        $person = $hydrator->hydrateDocument($doc);
138
139
        $this->assertInstanceOf(TestPerson::class, $person);
140
        $this->assertEquals('14.08.1991', $person->getBirthday()->format('d.m.Y'));
141
    }
142
143
    public function testWithTagName()
144
    {
145
        $doc = new DOMDocument();
146
        $doc->loadXml('<root><Person><hobby>Radeln</hobby></Person></root>');
147
148
        $mapper = new ClassMapper(
149
            [
150
                'person' => TestPerson::class,
151
                'hobby'  => TestHobby::class
152
            ]
153
        );
154
155
        $hydrator = new Hydrator($mapper);
156
        /** @var TestPerson $person */
157
        $person = $hydrator->hydrateDocument($doc);
158
159
        $this->assertInstanceOf(TestPerson::class, $person);
160
        $this->assertInstanceOf(TestHobby::class, $person->hobby);
161
        $this->assertEquals('Radeln', $person->hobby->value);
162
    }
163
164
    public function testPropertyAssignment()
165
    {
166
        $doc = new DOMDocument();
167
        $doc->load(__DIR__ . '/xml/test2.xml');
168
169
        $mapper = new ClassMapper(
170
            [
171
                'Stammdaten' => TestStammdaten::class
172
            ]
173
        );
174
175
        $hydrator = new Hydrator($mapper);
176
        /** @var TestStammdaten $stammdaten */
177
        $stammdaten = $hydrator->hydrateDocument($doc);
178
179
        $this->assertInstanceOf(TestStammdaten::class, $stammdaten);
180
        $this->assertEquals('Muster', $stammdaten->Name);
181
        $this->assertEquals('Max', $stammdaten->Vorname);
182
    }
183
184
    public function testFault()
185
    {
186
        $doc = new DOMDocument();
187
        $doc->load(__DIR__ . '/xml/fault.xml');
188
189
        $mapper = new ClassMapper(
190
            [
191
                'Envelope' => TestEnvelope::class,
192
                'Body'     => TestBody::class,
193
                'Fault'    => TestFault::class
194
            ]
195
        );
196
197
        $hydrator = new Hydrator($mapper);
198
        /** @var TestEnvelope $envelope */
199
        $envelope = $hydrator->hydrateDocument($doc);
200
201
        $this->assertInstanceOf(TestEnvelope::class, $envelope);
202
        $this->assertTrue($envelope->getBody()->hasFault());
203
        $this->assertEquals('Fehler!', $envelope->getBody()->getFault()->getFaultcode());
204
        $this->assertEquals('Es ist ein Fehler aufgetreten', $envelope->getBody()->getFault()->getFaultstring());
205
    }
206
207
    public function testList()
208
    {
209
        $doc = new DOMDocument();
210
        $doc->load(__DIR__ . '/xml/list.xml');
211
212
        $mapper = new ClassMapper(
213
            [
214
                'Envelope' => TestEnvelope::class,
215
                'Body'     => TestBody::class,
216
                'Fault'    => TestFault::class,
217
                'Result'   => TestResult::class,
218
                'Ort'      => TestOrt::class,
219
                'Oteil'    => TestOrtsTeil::class,
220
                'Strassen' => TestStrassen::class
221
            ]
222
        );
223
224
        $hydrator = new Hydrator($mapper);
225
        /** @var TestEnvelope $envelope */
226
        $envelope = $hydrator->hydrateDocument($doc);
227
228
        $this->assertInstanceOf(TestEnvelope::class, $envelope);
229
        $this->assertFalse($envelope->getBody()->hasFault());
230
        $this->assertInstanceOf(TestResult::class, $envelope->getBody()->getResult());
231
        $this->assertCount(1, $envelope->getBody()->getResult()->getOrte());
232
        $this->assertEquals('Hamburg', $envelope->getBody()->getResult()->getOrte()[0]->getName());
233
        $this->assertCount(4, $envelope->getBody()->getResult()->getOrte()[0]->getOrtsteile());
234
        $this->assertEquals('Hamburg-Altstadt', $envelope->getBody()->getResult()->getOrte()[0]->getOrtsteile()[1]->getName());
235
236
        for ($i = 0; $i < 4; $i++) {
237
            $this->assertNotEmpty($envelope->getBody()->getResult()->getOrte()[0]->getOrtsteile()[$i]->getStrassen());
238
        }
239
    }
240
}