|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\TestModel\Instance; |
|
4
|
|
|
|
|
5
|
|
|
use Xml\Impl\Util\StringUtil; |
|
6
|
|
|
use Xml\ModelInstanceInterface; |
|
7
|
|
|
use Xml\Impl\Parser\AbstractModelParser; |
|
8
|
|
|
use Tests\TestModel\{ |
|
9
|
|
|
Gender, |
|
10
|
|
|
TestModelConstants, |
|
11
|
|
|
TestModelParser |
|
12
|
|
|
}; |
|
13
|
|
|
use Tests\TestModel\TestModelTest; |
|
14
|
|
|
|
|
15
|
|
|
class FlyingAnimalCreateTest extends TestModelTest |
|
16
|
|
|
{ |
|
17
|
|
|
protected $tweety; |
|
18
|
|
|
protected $hedwig; |
|
19
|
|
|
protected $birdo; |
|
20
|
|
|
protected $plucky; |
|
21
|
|
|
protected $fiffy; |
|
22
|
|
|
protected $timmy; |
|
23
|
|
|
protected $daisy; |
|
24
|
|
|
|
|
25
|
|
|
protected function setUp(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$this->modelParser = new TestModelParser(); |
|
28
|
|
|
$this->modelInstance = $this->modelParser->getEmptyModel(); |
|
29
|
|
|
|
|
30
|
|
|
$animals = $this->modelInstance->newInstance(Animals::class); |
|
31
|
|
|
$this->modelInstance->setDocumentElement($animals); |
|
32
|
|
|
|
|
33
|
|
|
// add a tns namespace prefix for QName testing |
|
34
|
|
|
$animals->getDomElement()->registerNamespace("tns", TestModelConstants::MODEL_NAMESPACE); |
|
35
|
|
|
|
|
36
|
|
|
$this->tweety = $this->createBird($this->modelInstance, "tweety", Gender::FEMALE); |
|
37
|
|
|
$this->hedwig = $this->createBird($this->modelInstance, "hedwig", Gender::MALE); |
|
38
|
|
|
$this->birdo = $this->createBird($this->modelInstance, "birdo", Gender::FEMALE); |
|
39
|
|
|
$this->plucky = $this->createBird($this->modelInstance, "plucky", Gender::UNKNOWN); |
|
40
|
|
|
$this->fiffy = $this->createBird($this->modelInstance, "fiffy", Gender::FEMALE); |
|
41
|
|
|
$this->timmy = $this->createBird($this->modelInstance, "timmy", Gender::MALE); |
|
42
|
|
|
$this->daisy = $this->createBird($this->modelInstance, "daisy", Gender::FEMALE); |
|
43
|
|
|
|
|
44
|
|
|
$this->tweety->setFlightInstructor($this->hedwig); |
|
45
|
|
|
|
|
46
|
|
|
$this->tweety->addFlightPartnerRef($this->hedwig); |
|
47
|
|
|
$this->tweety->addFlightPartnerRef($this->birdo); |
|
48
|
|
|
$this->tweety->addFlightPartnerRef($this->plucky); |
|
49
|
|
|
$this->tweety->addFlightPartnerRef($this->fiffy); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testSetWingspanAttributeByHelper(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$wingspan = 2.123; |
|
55
|
|
|
$this->tweety->setWingspan($wingspan); |
|
56
|
|
|
$this->assertEquals($wingspan, $this->tweety->getWingspan()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testSetWingspanAttributeByAttributeName(): void |
|
60
|
|
|
{ |
|
61
|
|
|
$wingspan = 2.123; |
|
62
|
|
|
$this->tweety->setAttributeValue("wingspan", $wingspan, false); |
|
63
|
|
|
$this->assertEquals($wingspan, $this->tweety->getWingspan()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testRemoveWingspanAttribute(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$wingspan = 2.123; |
|
69
|
|
|
$this->tweety->setWingspan($wingspan); |
|
70
|
|
|
$this->assertEquals($wingspan, $this->tweety->getWingspan()); |
|
71
|
|
|
|
|
72
|
|
|
$this->tweety->removeAttribute("wingspan"); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertNull($this->tweety->getWingspan()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testSetFlightInstructorByHelper(): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->tweety->setFlightInstructor($this->timmy); |
|
80
|
|
|
$this->assertTrue($this->tweety->getFlightInstructor()->equals($this->timmy)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testUpdateFlightInstructorByIdHelper(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->hedwig->setId("new-" . $this->hedwig->getId()); |
|
86
|
|
|
$this->assertTrue($this->tweety->getFlightInstructor()->equals($this->hedwig)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testUpdateFlightInstructorByIdAttributeName(): void |
|
90
|
|
|
{ |
|
91
|
|
|
$this->hedwig->setAttributeValue("id", "new-" . $this->hedwig->getId(), true); |
|
92
|
|
|
$this->assertTrue($this->tweety->getFlightInstructor()->equals($this->hedwig)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testUpdateFlightInstructorByReplaceElement(): void |
|
96
|
|
|
{ |
|
97
|
|
|
$this->hedwig->replaceWithElement($this->timmy); |
|
98
|
|
|
$this->assertTrue($this->tweety->getFlightInstructor()->equals($this->timmy)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testUpdateFlightInstructorByRemoveElement(): void |
|
102
|
|
|
{ |
|
103
|
|
|
$animals = $this->modelInstance->getDocumentElement(); |
|
104
|
|
|
$animals->removeAnimal($this->hedwig); |
|
105
|
|
|
$this->assertNull($this->tweety->getFlightInstructor()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function testClearFlightInstructor(): void |
|
109
|
|
|
{ |
|
110
|
|
|
$this->tweety->removeFlightInstructor(); |
|
111
|
|
|
$this->assertNull($this->tweety->getFlightInstructor()); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function testAddFlightPartnerRefsByHelper(): void |
|
115
|
|
|
{ |
|
116
|
|
|
$this->assertCount(4, $this->tweety->getFlightPartnerRefs()); |
|
117
|
|
|
$birds = [$this->hedwig, $this->birdo, $this->plucky, $this->fiffy]; |
|
118
|
|
|
foreach ($birds as $bird) { |
|
119
|
|
|
$exists = false; |
|
120
|
|
|
foreach ($this->tweety->getFlightPartnerRefs() as $ref) { |
|
121
|
|
|
if ($ref->equals($bird)) { |
|
122
|
|
|
$exists = true; |
|
123
|
|
|
break; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
$this->assertTrue($exists); |
|
127
|
|
|
} |
|
128
|
|
|
$this->tweety->addFlightPartnerRef($this->timmy); |
|
129
|
|
|
$this->tweety->addFlightPartnerRef($this->daisy); |
|
130
|
|
|
|
|
131
|
|
|
$birds[] = $this->timmy; |
|
132
|
|
|
$birds[] = $this->daisy; |
|
133
|
|
|
|
|
134
|
|
|
foreach ($birds as $bird) { |
|
135
|
|
|
$exists = false; |
|
136
|
|
|
foreach ($this->tweety->getFlightPartnerRefs() as $ref) { |
|
137
|
|
|
if ($ref->equals($bird)) { |
|
138
|
|
|
$exists = true; |
|
139
|
|
|
break; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
$this->assertTrue($exists); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function testUpdateFlightPartnerRefsByIdByHelper(): void |
|
147
|
|
|
{ |
|
148
|
|
|
$this->hedwig->setId("new-" . $this->hedwig->getId()); |
|
149
|
|
|
$this->plucky->setId("new-" . $this->plucky->getId()); |
|
150
|
|
|
$this->assertCount(4, $this->tweety->getFlightPartnerRefs()); |
|
151
|
|
|
$birds = [$this->hedwig, $this->birdo, $this->plucky, $this->fiffy]; |
|
152
|
|
|
foreach ($birds as $bird) { |
|
153
|
|
|
$exists = false; |
|
154
|
|
|
foreach ($this->tweety->getFlightPartnerRefs() as $ref) { |
|
155
|
|
|
if ($ref->equals($bird)) { |
|
156
|
|
|
$exists = true; |
|
157
|
|
|
break; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
$this->assertTrue($exists); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function testUpdateFlightPartnerRefsByIdByAttributeName(): void |
|
165
|
|
|
{ |
|
166
|
|
|
$this->birdo->setAttributeValue("id", "new-" . $this->birdo->getId(), true); |
|
167
|
|
|
$this->fiffy->setAttributeValue("id", "new-" . $this->fiffy->getId(), true); |
|
168
|
|
|
$birds = [$this->hedwig, $this->birdo, $this->plucky, $this->fiffy]; |
|
169
|
|
|
foreach ($birds as $bird) { |
|
170
|
|
|
$exists = false; |
|
171
|
|
|
foreach ($this->tweety->getFlightPartnerRefs() as $ref) { |
|
172
|
|
|
if ($ref->equals($bird)) { |
|
173
|
|
|
$exists = true; |
|
174
|
|
|
break; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
$this->assertTrue($exists); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function testUpdateFlightPartnerRefsByReplaceElements(): void |
|
182
|
|
|
{ |
|
183
|
|
|
$this->hedwig->replaceWithElement($this->timmy); |
|
184
|
|
|
$this->plucky->replaceWithElement($this->daisy); |
|
185
|
|
|
$birds = [$this->timmy, $this->birdo, $this->daisy, $this->fiffy]; |
|
186
|
|
|
foreach ($birds as $bird) { |
|
187
|
|
|
$exists = false; |
|
188
|
|
|
foreach ($this->tweety->getFlightPartnerRefs() as $ref) { |
|
189
|
|
|
if ($ref->equals($bird)) { |
|
190
|
|
|
$exists = true; |
|
191
|
|
|
break; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
$this->assertTrue($exists); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function testUpdateFlightPartnerRefsByRemoveElements(): void |
|
199
|
|
|
{ |
|
200
|
|
|
$this->tweety->removeFlightPartnerRef($this->birdo); |
|
201
|
|
|
$this->tweety->removeFlightPartnerRef($this->fiffy); |
|
202
|
|
|
$this->assertCount(2, $this->tweety->getFlightPartnerRefs()); |
|
203
|
|
|
$birds = [$this->hedwig, $this->plucky]; |
|
204
|
|
|
foreach ($birds as $bird) { |
|
205
|
|
|
$exists = false; |
|
206
|
|
|
foreach ($this->tweety->getFlightPartnerRefs() as $ref) { |
|
207
|
|
|
if ($ref->equals($bird)) { |
|
208
|
|
|
$exists = true; |
|
209
|
|
|
break; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
$this->assertTrue($exists); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function testClearFlightPartnerRefs(): void |
|
217
|
|
|
{ |
|
218
|
|
|
$this->tweety->clearFlightPartnerRefs(); |
|
219
|
|
|
$this->assertEmpty($this->tweety->getFlightPartnerRefs()); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function testAddFlightPartnerRefElementsByHelper(): void |
|
223
|
|
|
{ |
|
224
|
|
|
$this->assertCount(4, $this->tweety->getFlightPartnerRefElements()); |
|
225
|
|
|
|
|
226
|
|
|
$timmyFlightPartnerRef = $this->modelInstance->newInstance(FlightPartnerRef::class); |
|
227
|
|
|
$timmyFlightPartnerRef->setTextContent($this->timmy->getId()); |
|
228
|
|
|
$this->tweety->addFlightPartnerRefElement($timmyFlightPartnerRef); |
|
229
|
|
|
|
|
230
|
|
|
$daisyFlightPartnerRef = $this->modelInstance->newInstance(FlightPartnerRef::class); |
|
231
|
|
|
$daisyFlightPartnerRef->setTextContent($this->daisy->getId()); |
|
232
|
|
|
$this->tweety->addFlightPartnerRefElement($daisyFlightPartnerRef); |
|
233
|
|
|
|
|
234
|
|
|
$this->assertCount(6, $this->tweety->getFlightPartnerRefElements()); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public function testFlightPartnerRefElementsByTextContent(): void |
|
238
|
|
|
{ |
|
239
|
|
|
$flightPartnerRefElements = $this->tweety->getFlightPartnerRefElements(); |
|
240
|
|
|
$textContents = []; |
|
241
|
|
|
foreach ($flightPartnerRefElements as $flightPartnerRefElement) { |
|
242
|
|
|
$textContent = $flightPartnerRefElement->getTextContent(); |
|
243
|
|
|
$this->assertFalse(empty($textContent)); |
|
244
|
|
|
$textContents[] = $textContent; |
|
245
|
|
|
} |
|
246
|
|
|
$this->assertCount(4, $textContents); |
|
247
|
|
|
$this->assertContains($this->hedwig->getId(), $textContents); |
|
248
|
|
|
$this->assertContains($this->birdo->getId(), $textContents); |
|
249
|
|
|
$this->assertContains($this->plucky->getId(), $textContents); |
|
250
|
|
|
$this->assertContains($this->fiffy->getId(), $textContents); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
public function testUpdateFlightPartnerRefElementsByTextContent(): void |
|
254
|
|
|
{ |
|
255
|
|
|
$flightPartnerRefs = $this->tweety->getFlightPartnerRefElements(); |
|
256
|
|
|
|
|
257
|
|
|
$flightPartnerRefs[0]->setTextContent($this->timmy->getId()); |
|
258
|
|
|
$flightPartnerRefs[2]->setTextContent($this->daisy->getId()); |
|
259
|
|
|
|
|
260
|
|
|
$this->assertCount(4, $this->tweety->getFlightPartnerRefs()); |
|
261
|
|
|
|
|
262
|
|
|
$birds = [$this->birdo, $this->fiffy, $this->timmy, $this->daisy]; |
|
263
|
|
|
foreach ($birds as $bird) { |
|
264
|
|
|
$exists = false; |
|
265
|
|
|
foreach ($this->tweety->getFlightPartnerRefs() as $ref) { |
|
266
|
|
|
if ($ref->equals($bird)) { |
|
267
|
|
|
$exists = true; |
|
268
|
|
|
break; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
$this->assertTrue($exists); |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
public function testUpdateFlightPartnerRefElementsByRemoveElements(): void |
|
276
|
|
|
{ |
|
277
|
|
|
$flightPartnerRefs = $this->tweety->getFlightPartnerRefElements(); |
|
278
|
|
|
$this->tweety->removeFlightPartnerRefElement($flightPartnerRefs[1]); |
|
279
|
|
|
$this->tweety->removeFlightPartnerRefElement($flightPartnerRefs[3]); |
|
280
|
|
|
|
|
281
|
|
|
$this->assertCount(2, $this->tweety->getFlightPartnerRefs()); |
|
282
|
|
|
$birds = [$this->hedwig, $this->plucky]; |
|
283
|
|
|
foreach ($birds as $bird) { |
|
284
|
|
|
$exists = false; |
|
285
|
|
|
foreach ($this->tweety->getFlightPartnerRefs() as $ref) { |
|
286
|
|
|
if ($ref->equals($bird)) { |
|
287
|
|
|
$exists = true; |
|
288
|
|
|
break; |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
$this->assertTrue($exists); |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
public function testClearFlightPartnerRefElements(): void |
|
296
|
|
|
{ |
|
297
|
|
|
$this->tweety->clearFlightPartnerRefElements(); |
|
298
|
|
|
$this->assertEmpty($this->tweety->getFlightPartnerRefElements()); |
|
299
|
|
|
|
|
300
|
|
|
// should not affect animals collection |
|
301
|
|
|
$animals = $this->modelInstance->getDocumentElement(); |
|
302
|
|
|
$this->assertCount(7, $animals->getAnimals()); |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
|