AnimalCreateModelTest   F
last analyzed

Complexity

Total Complexity 107

Size/Duplication

Total Lines 741
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 406
c 0
b 0
f 0
dl 0
loc 741
rs 2
wmc 107

55 Methods

Rating   Name   Duplication   Size   Complexity  
A testRemoveGenderAttribute() 0 10 1
A testReplaceMotherReferenceWithNewAnimal() 0 6 1
A testAddRelationsDefinitionRefsByHelper() 0 26 4
A testRelationshipDefinitionRefElementsByTextContent() 0 14 2
A testUpdateRelationshipDefinitionRefsByIdByAttributeName() 0 19 4
A testClearRelationshipDefinitionRefElementsByClearRelationshipDefinitions() 0 7 1
A testClearRelationshipDefinitionsRefs() 0 6 1
A testUpdateRelationshipDefinitionsByIdByHelper() 0 19 4
A testUpdateRelationshipDefinitionRefElementsByRemoveElements() 0 15 4
A testChangeIdAttributeOfFatherReference() 0 6 1
A testClearRelationshipDefinitionRefElements() 0 8 1
A setUp() 0 60 1
A testRemoveAgeAttribute() 0 4 1
A testRemoveBestFriendRef() 0 8 1
A testSetAgeAttributeByAttributeName() 0 4 1
A testUpdateRelationshipDefinitionRefsByIdByHelper() 0 19 4
A testSetFatherAttributeByHelper() 0 4 1
A testUpdateRelationshipDefinitionRefsByRemoveIdAttribute() 0 17 4
A testReplaceFatherReferenceWithNewAnimal() 0 6 1
A testSetGenderAttributeByHelper() 0 4 1
A testSetIsEndangeredAttributeByAttributeName() 0 4 1
A testAddBestFriend() 0 16 4
A testRemoveMotherAttribute() 0 6 1
A testClearBestFriendRef() 0 7 1
A testSetNameAttributeByHelper() 0 4 1
A testUpdateRelationshipDefinitionRefElementsByTextContentWithNamespace() 0 24 4
A testSetIsEndangeredAttributeByHelper() 0 4 1
A testRemoveIsEndangeredAttribute() 0 5 1
A testUpdateRelationshipDefinitionRefElementsByTextContent() 0 26 4
A testUpdateRelationshipDefinitionsByReplaceElements() 0 18 4
A validateModel() 0 3 1
A testClearAndAddBestFriendRef() 0 7 1
A testSetGenderAttributeByAttributeName() 0 4 1
A testSetFatherAttributeByAttributeName() 0 4 1
A testAddRelationshipDefinitionRefElementsByHelper() 0 28 4
A testRemoveFatherAttribute() 0 6 1
A testClearRelationshipDefinitionRefElementsByClearRelationshipDefinitionRefs() 0 7 1
A testUpdateRelationshipDefinitionRefsByRemoveElements() 0 17 4
A testClearRelationshipDefinitionRefsByClearRelationshipDefinitions() 0 7 1
A testSetIdAttributeByAttributeName() 0 4 1
A testRemoveIdAttribute() 0 4 1
A testUpdateRelationshipDefinitionsByIdByAttributeName() 0 19 4
A testSetFatherAttributeByAttributeNameWithNamespace() 0 4 1
A testSetMotherAttributeByAttributeName() 0 4 1
A testUpdateRelationshipDefinitionRefsByReplaceElements() 0 19 4
A testGetBestFriends() 0 15 4
A testClearRelationshipDefinitions() 0 4 1
A testChangeIdAttributeOfMotherReference() 0 6 1
A testSetIdAttributeByHelper() 0 5 1
B testAddRelationshipDefinitionsByHelper() 0 31 7
A testSetMotherAttributeByHelper() 0 4 1
A testSetAgeAttributeByHelper() 0 4 1
A testGetElementById() 0 8 1
A testRemoveNameAttribute() 0 4 1
A testSetNameAttributeByAttributeName() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like AnimalCreateModelTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AnimalCreateModelTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Tests\TestModel\Instance;
4
5
use Xml\ModelInstanceInterface;
6
use Xml\Impl\Parser\AbstractModelParser;
7
use Tests\TestModel\{
8
    Gender,
9
    TestModelConstants,
10
    TestModelParser
11
};
12
use Tests\TestModel\TestModelTest;
13
use Tests\TestModel\Instance\{
14
    Animal,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tests\TestModel\Instance\Animal. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
    Animals,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tests\TestModel\Instance\Animals. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
    Bird,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tests\TestModel\Instance\Bird. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
    ChildRelationshipDefinition,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tests\TestModel\Instance...dRelationshipDefinition. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
    FriendRelationshipDefinition,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tests\TestModel\Instance...dRelationshipDefinition. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
19
    RelationshipDefinition,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tests\TestModel\Instance\RelationshipDefinition. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
20
    RelationshipDefinitionRef
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tests\TestModel\Instance\RelationshipDefinitionRef. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
21
};
22
23
class AnimalCreateModelTest extends TestModelTest
24
{
25
    protected $tweety;
26
    protected $hedwig;
27
    protected $birdo;
28
    protected $plucky;
29
    protected $fiffy;
30
    protected $timmy;
31
    protected $daisy;
32
    protected $hedwigRelationship;
33
    protected $birdoRelationship;
34
    protected $pluckyRelationship;
35
    protected $fiffyRelationship;
36
    protected $timmyRelationship;
37
    protected $daisyRelationship;
38
39
    protected function setUp(): void
40
    {
41
        $this->modelParser = new TestModelParser();
42
        $this->modelInstance = $this->modelParser->getEmptyModel();
43
44
        $animals = $this->modelInstance->newInstance(Animals::class);
45
        $this->modelInstance->setDocumentElement($animals);
46
47
        $animals->getDomElement()->registerNamespace("tns", TestModelConstants::MODEL_NAMESPACE);
48
49
        $this->tweety = $this->createBird($this->modelInstance, "tweety", Gender::FEMALE);
50
        $this->hedwig = $this->createBird($this->modelInstance, "hedwig", Gender::MALE);
51
        $this->birdo = $this->createBird($this->modelInstance, "birdo", Gender::FEMALE);
52
        $this->plucky = $this->createBird($this->modelInstance, "plucky", Gender::UNKNOWN);
53
        $this->fiffy = $this->createBird($this->modelInstance, "fiffy", Gender::FEMALE);
54
        $this->timmy = $this->createBird($this->modelInstance, "timmy", Gender::MALE);
55
        $this->daisy = $this->createBird($this->modelInstance, "daisy", Gender::FEMALE);
56
57
        $this->hedwigRelationship = $this->createRelationshipDefinition(
58
            $this->modelInstance,
59
            $this->hedwig,
60
            ChildRelationshipDefinition::class
61
        );
62
        $this->addRelationshipDefinition($this->tweety, $this->hedwigRelationship);
63
        $this->birdoRelationship = $this->createRelationshipDefinition(
64
            $this->modelInstance,
65
            $this->birdo,
66
            ChildRelationshipDefinition::class
67
        );
68
        $this->addRelationshipDefinition($this->tweety, $this->birdoRelationship);
69
        $this->pluckyRelationship = $this->createRelationshipDefinition(
70
            $this->modelInstance,
71
            $this->plucky,
72
            FriendRelationshipDefinition::class
73
        );
74
        $this->addRelationshipDefinition($this->tweety, $this->pluckyRelationship);
75
        $this->fiffyRelationship = $this->createRelationshipDefinition(
76
            $this->modelInstance,
77
            $this->fiffy,
78
            FriendRelationshipDefinition::class
79
        );
80
        $this->addRelationshipDefinition($this->tweety, $this->fiffyRelationship);
81
82
        $this->tweety->addRelationshipDefinition($this->hedwigRelationship);
83
        $this->tweety->addRelationshipDefinition($this->birdoRelationship);
84
        $this->tweety->addRelationshipDefinition($this->pluckyRelationship);
85
        $this->tweety->addRelationshipDefinition($this->fiffyRelationship);
86
87
        $this->tweety->addFriend($this->birdo);
88
        $this->tweety->addFriend($this->plucky);
89
90
        $this->timmyRelationship = $this->createRelationshipDefinition(
91
            $this->modelInstance,
92
            $this->timmy,
93
            FriendRelationshipDefinition::class
94
        );
95
        $this->daisyRelationship = $this->createRelationshipDefinition(
96
            $this->modelInstance,
97
            $this->daisy,
98
            ChildRelationshipDefinition::class
99
        );
100
    }
101
102
    public function testGetElementById(): void
103
    {
104
        $this->assertFalse(
105
            $this->tweety->getModelInstance()->getModelElementById($this->tweety->getId()) === null
106
        );
107
        $this->tweety->setId("new-" . $this->tweety->getId());
108
        $this->assertFalse(
109
            $this->tweety->getModelInstance()->getModelElementById($this->tweety->getId()) === null
110
        );
111
    }
112
113
    public function testSetIdAttributeByHelper(): void
114
    {
115
        $newId = "new-" . $this->tweety->getId();
116
        $this->tweety->setId($newId);
117
        $this->assertEquals($newId, $this->tweety->getId());
118
    }
119
120
    public function testSetIdAttributeByAttributeName(): void
121
    {
122
        $this->tweety->setAttributeValue("id", "duffy", true);
123
        $this->assertEquals("duffy", $this->tweety->getId());
124
    }
125
126
    public function testRemoveIdAttribute(): void
127
    {
128
        $this->tweety->removeAttribute("id");
129
        $this->assertNull($this->tweety->getId());
130
    }
131
132
    public function testSetNameAttributeByHelper(): void
133
    {
134
        $this->tweety->setName("tweety");
135
        $this->assertEquals("tweety", $this->tweety->getName());
136
    }
137
138
    public function testSetNameAttributeByAttributeName(): void
139
    {
140
        $this->tweety->setAttributeValue("name", "daisy");
141
        $this->assertEquals("daisy", $this->tweety->getName());
142
    }
143
144
    public function testRemoveNameAttribute(): void
145
    {
146
        $this->tweety->removeAttribute("name");
147
        $this->assertNull($this->tweety->getName());
148
    }
149
150
    public function testSetFatherAttributeByHelper(): void
151
    {
152
        $this->tweety->setFather($this->timmy);
153
        $this->assertTrue($this->tweety->getFather()->equals($this->timmy));
154
    }
155
156
    public function testSetFatherAttributeByAttributeName(): void
157
    {
158
        $this->tweety->setAttributeValue("father", $this->timmy->getId());
159
        $this->assertTrue($this->tweety->getFather()->equals($this->timmy));
160
    }
161
162
    public function testSetFatherAttributeByAttributeNameWithNamespace(): void
163
    {
164
        $this->tweety->setAttributeValue("father", "tns:hedwig");
165
        $this->assertTrue($this->tweety->getFather()->equals($this->hedwig));
166
    }
167
168
    public function testRemoveFatherAttribute(): void
169
    {
170
        $this->tweety->setFather($this->timmy);
171
        $this->assertTrue($this->tweety->getFather()->equals($this->timmy));
172
        $this->tweety->removeAttribute("father");
173
        $this->assertNull($this->tweety->getFather());
174
    }
175
176
    public function testChangeIdAttributeOfFatherReference(): void
177
    {
178
        $this->tweety->setFather($this->timmy);
179
        $this->assertTrue($this->tweety->getFather()->equals($this->timmy));
180
        $this->tweety->setId("new-" . $this->tweety->getId());
181
        $this->assertTrue($this->tweety->getFather()->equals($this->timmy));
182
    }
183
184
    public function testReplaceFatherReferenceWithNewAnimal(): void
185
    {
186
        $this->tweety->setFather($this->timmy);
187
        $this->assertTrue($this->tweety->getFather()->equals($this->timmy));
188
        $this->timmy->replaceWithElement($this->plucky);
189
        $this->assertTrue($this->tweety->getFather()->equals($this->plucky));
190
    }
191
192
    public function testSetMotherAttributeByHelper(): void
193
    {
194
        $this->tweety->setMother($this->daisy);
195
        $this->assertTrue($this->tweety->getMother()->equals($this->daisy));
196
    }
197
198
    public function testSetMotherAttributeByAttributeName(): void
199
    {
200
        $this->tweety->setAttributeValue("mother", $this->fiffy->getId());
201
        $this->assertTrue($this->tweety->getMother()->equals($this->fiffy));
202
    }
203
204
    public function testRemoveMotherAttribute(): void
205
    {
206
        $this->tweety->setMother($this->daisy);
207
        $this->assertTrue($this->tweety->getMother()->equals($this->daisy));
208
        $this->tweety->removeAttribute("mother");
209
        $this->assertNull($this->tweety->getMother());
210
    }
211
212
    public function testReplaceMotherReferenceWithNewAnimal(): void
213
    {
214
        $this->tweety->setMother($this->daisy);
215
        $this->assertTrue($this->tweety->getMother()->equals($this->daisy));
216
        $this->daisy->replaceWithElement($this->birdo);
217
        $this->assertTrue($this->tweety->getMother()->equals($this->birdo));
218
    }
219
220
    public function testChangeIdAttributeOfMotherReference(): void
221
    {
222
        $this->tweety->setMother($this->daisy);
223
        $this->assertTrue($this->tweety->getMother()->equals($this->daisy));
224
        $this->daisy->setId("new-" . $this->daisy->getId());
225
        $this->assertTrue($this->tweety->getMother()->equals($this->daisy));
226
    }
227
228
    public function testSetIsEndangeredAttributeByHelper(): void
229
    {
230
        $this->tweety->setIsEndangered(true);
231
        $this->assertTrue($this->tweety->isEndangered());
232
    }
233
234
    public function testSetIsEndangeredAttributeByAttributeName(): void
235
    {
236
        $this->tweety->setAttributeValue("isEndangered", "false");
237
        $this->assertFalse($this->tweety->isEndangered());
238
    }
239
240
    public function testRemoveIsEndangeredAttribute(): void
241
    {
242
        $this->tweety->removeAttribute("isEndangered");
243
        // default value of isEndangered: false
244
        $this->assertFalse($this->tweety->isEndangered());
245
    }
246
247
    public function testSetGenderAttributeByHelper(): void
248
    {
249
        $this->tweety->setGender(Gender::MALE);
250
        $this->assertEquals(Gender::MALE, $this->tweety->getGender());
251
    }
252
253
    public function testSetGenderAttributeByAttributeName(): void
254
    {
255
        $this->tweety->setAttributeValue("gender", Gender::UNKNOWN);
256
        $this->assertEquals(Gender::UNKNOWN, $this->tweety->getGender());
257
    }
258
259
    public function testRemoveGenderAttribute(): void
260
    {
261
        $this->tweety->removeAttribute("gender");
262
        $this->assertNull($this->tweety->getGender());
263
264
        try {
265
            $this->expectException(\Xml\Exception\ModelValidationException::class);
266
            $this->validateModel();
267
        } finally {
268
            $this->tweety->setGender(Gender::FEMALE);
269
        }
270
    }
271
272
    public function testSetAgeAttributeByHelper(): void
273
    {
274
        $this->tweety->setAge(13);
275
        $this->assertEquals(13, $this->tweety->getAge());
276
    }
277
278
    public function testSetAgeAttributeByAttributeName(): void
279
    {
280
        $this->tweety->setAttributeValue("age", "23");
281
        $this->assertEquals(23, $this->tweety->getAge());
282
    }
283
284
    public function testRemoveAgeAttribute(): void
285
    {
286
        $this->tweety->removeAttribute("age");
287
        $this->assertNull($this->tweety->getAge());
288
    }
289
290
    private function validateModel(): void
291
    {
292
        $this->modelParser->validateModel($this->modelInstance->getDocument());
293
    }
294
295
    public function testAddRelationshipDefinitionsByHelper(): void
296
    {
297
        $this->assertCount(4, $this->tweety->getRelationshipDefinitions());
298
        $rels = [
299
            $this->hedwigRelationship,
300
            $this->birdoRelationship,
301
            $this->pluckyRelationship,
302
            $this->fiffyRelationship
303
        ];
304
        foreach ($rels as $rel) {
305
            $exists = false;
306
            foreach ($this->tweety->getRelationshipDefinitions() as $rel2) {
307
                if ($rel2->equals($rel)) {
308
                    $exists = true;
309
                }
310
            }
311
            $this->assertTrue($exists);
312
        }
313
        $this->tweety->addRelationship($this->timmyRelationship);
314
        $this->tweety->addRelationship($this->daisyRelationship);
315
        $rels[] = $this->timmyRelationship;
316
        $rels[] = $this->daisyRelationship;
317
        $this->assertCount(6, $this->tweety->getRelationshipDefinitions());
318
        foreach ($rels as $rel) {
319
            $exists = false;
320
            foreach ($this->tweety->getRelationshipDefinitions() as $rel2) {
321
                if ($rel2->equals($rel)) {
322
                    $exists = true;
323
                }
324
            }
325
            $this->assertTrue($exists);
326
        }
327
    }
328
329
    public function testUpdateRelationshipDefinitionsByIdByHelper(): void
330
    {
331
        $this->hedwigRelationship->setId("new-" . $this->hedwigRelationship->getId());
332
        $this->pluckyRelationship->setId("new-" . $this->pluckyRelationship->getId());
333
        $this->assertCount(4, $this->tweety->getRelationshipDefinitions());
334
        $rels = [
335
            $this->hedwigRelationship,
336
            $this->birdoRelationship,
337
            $this->pluckyRelationship,
338
            $this->fiffyRelationship
339
        ];
340
        foreach ($rels as $rel) {
341
            $exists = false;
342
            foreach ($this->tweety->getRelationshipDefinitions() as $rel2) {
343
                if ($rel2->equals($rel)) {
344
                    $exists = true;
345
                }
346
            }
347
            $this->assertTrue($exists);
348
        }
349
    }
350
351
    public function testUpdateRelationshipDefinitionsByIdByAttributeName(): void
352
    {
353
        $this->birdoRelationship->setAttributeValue("id", "new-" . $this->birdoRelationship->getId(), true);
354
        $this->fiffyRelationship->setAttributeValue("id", "new-" . $this->fiffyRelationship->getId(), true);
355
        $this->assertCount(4, $this->tweety->getRelationshipDefinitions());
356
        $rels = [
357
            $this->hedwigRelationship,
358
            $this->birdoRelationship,
359
            $this->pluckyRelationship,
360
            $this->fiffyRelationship
361
        ];
362
        foreach ($rels as $rel) {
363
            $exists = false;
364
            foreach ($this->tweety->getRelationshipDefinitions() as $rel2) {
365
                if ($rel2->equals($rel)) {
366
                    $exists = true;
367
                }
368
            }
369
            $this->assertTrue($exists);
370
        }
371
    }
372
373
    public function testUpdateRelationshipDefinitionsByReplaceElements(): void
374
    {
375
        $this->hedwigRelationship->replaceWithElement($this->timmyRelationship);
376
        $this->pluckyRelationship->replaceWithElement($this->daisyRelationship);
377
        $rels = [
378
            $this->timmyRelationship,
379
            $this->birdoRelationship,
380
            $this->daisyRelationship,
381
            $this->fiffyRelationship
382
        ];
383
        foreach ($rels as $rel) {
384
            $exists = false;
385
            foreach ($this->tweety->getRelationshipDefinitions() as $rel2) {
386
                if ($rel2->equals($rel)) {
387
                    $exists = true;
388
                }
389
            }
390
            $this->assertTrue($exists);
391
        }
392
    }
393
394
    public function testClearRelationshipDefinitions(): void
395
    {
396
        $this->tweety->clearRelationships();
397
        $this->assertEmpty($this->tweety->getRelationshipDefinitions());
398
    }
399
400
    public function testAddRelationsDefinitionRefsByHelper(): void
401
    {
402
        $this->addRelationshipDefinition($this->tweety, $this->timmyRelationship);
403
        $this->addRelationshipDefinition($this->tweety, $this->daisyRelationship);
404
405
        $this->tweety->addRelationshipDefinition($this->timmyRelationship);
406
        $this->tweety->addRelationshipDefinition($this->daisyRelationship);
407
408
        $this->assertCount(6, $this->tweety->getRelationshipDefinitions());
409
410
        $rels = [
411
            $this->hedwigRelationship,
412
            $this->pluckyRelationship,
413
            $this->timmyRelationship,
414
            $this->birdoRelationship,
415
            $this->daisyRelationship,
416
            $this->fiffyRelationship
417
        ];
418
        foreach ($rels as $rel) {
419
            $exists = false;
420
            foreach ($this->tweety->getRelationshipDefinitions() as $rel2) {
421
                if ($rel2->equals($rel)) {
422
                    $exists = true;
423
                }
424
            }
425
            $this->assertTrue($exists);
426
        }
427
    }
428
429
    public function testUpdateRelationshipDefinitionRefsByIdByHelper(): void
430
    {
431
        $this->hedwigRelationship->setId("child-relationship");
432
        $this->pluckyRelationship->setId("friend-relationship");
433
        $this->assertCount(4, $this->tweety->getRelationshipDefinitions());
434
        $rels = [
435
            $this->hedwigRelationship,
436
            $this->pluckyRelationship,
437
            $this->birdoRelationship,
438
            $this->fiffyRelationship
439
        ];
440
        foreach ($rels as $rel) {
441
            $exists = false;
442
            foreach ($this->tweety->getRelationshipDefinitions() as $rel2) {
443
                if ($rel2->equals($rel)) {
444
                    $exists = true;
445
                }
446
            }
447
            $this->assertTrue($exists);
448
        }
449
    }
450
451
    public function testUpdateRelationshipDefinitionRefsByIdByAttributeName(): void
452
    {
453
        $this->birdoRelationship->setAttributeValue("id", "birdo-relationship", true);
454
        $this->fiffyRelationship->setAttributeValue("id", "fiffy-relationship", true);
455
        $this->assertCount(4, $this->tweety->getRelationshipDefinitionRefs());
456
        $rels = [
457
            $this->hedwigRelationship,
458
            $this->pluckyRelationship,
459
            $this->birdoRelationship,
460
            $this->fiffyRelationship
461
        ];
462
        foreach ($rels as $rel) {
463
            $exists = false;
464
            foreach ($this->tweety->getRelationshipDefinitionRefs() as $rel2) {
465
                if ($rel2->equals($rel)) {
466
                    $exists = true;
467
                }
468
            }
469
            $this->assertTrue($exists);
470
        }
471
    }
472
473
    public function testUpdateRelationshipDefinitionRefsByReplaceElements(): void
474
    {
475
        $this->hedwigRelationship->replaceWithElement($this->timmyRelationship);
476
        $this->pluckyRelationship->replaceWithElement($this->daisyRelationship);
477
        $this->assertCount(4, $this->tweety->getRelationshipDefinitionRefs());
478
        $rels = [
479
            $this->timmyRelationship,
480
            $this->daisyRelationship,
481
            $this->birdoRelationship,
482
            $this->fiffyRelationship
483
        ];
484
        foreach ($rels as $rel) {
485
            $exists = false;
486
            foreach ($this->tweety->getRelationshipDefinitionRefs() as $rel2) {
487
                if ($rel2->equals($rel)) {
488
                    $exists = true;
489
                }
490
            }
491
            $this->assertTrue($exists);
492
        }
493
    }
494
495
    public function testUpdateRelationshipDefinitionRefsByRemoveElements(): void
496
    {
497
        $this->tweety->removeRelationship($this->birdoRelationship);
498
        $this->tweety->removeRelationship($this->fiffyRelationship);
499
        $this->assertCount(2, $this->tweety->getRelationshipDefinitionRefs());
500
        $rels = [
501
            $this->hedwigRelationship,
502
            $this->pluckyRelationship
503
        ];
504
        foreach ($rels as $rel) {
505
            $exists = false;
506
            foreach ($this->tweety->getRelationshipDefinitionRefs() as $rel2) {
507
                if ($rel2->equals($rel)) {
508
                    $exists = true;
509
                }
510
            }
511
            $this->assertTrue($exists);
512
        }
513
    }
514
515
    public function testUpdateRelationshipDefinitionRefsByRemoveIdAttribute(): void
516
    {
517
        $this->birdoRelationship->removeAttribute("id");
518
        $this->pluckyRelationship->removeAttribute("id");
519
        $this->assertCount(2, $this->tweety->getRelationshipDefinitionRefs());
520
        $rels = [
521
            $this->hedwigRelationship,
522
            $this->fiffyRelationship
523
        ];
524
        foreach ($rels as $rel) {
525
            $exists = false;
526
            foreach ($this->tweety->getRelationshipDefinitionRefs() as $rel2) {
527
                if ($rel2->equals($rel)) {
528
                    $exists = true;
529
                }
530
            }
531
            $this->assertTrue($exists);
532
        }
533
    }
534
535
    public function testClearRelationshipDefinitionsRefs(): void
536
    {
537
        $this->tweety->clearRelationshipDefinitions();
538
        $this->assertEmpty($this->tweety->getRelationshipDefinitionRefs());
539
        // should not affect animal relationship definitions
540
        $this->assertCount(4, $this->tweety->getRelationshipDefinitions());
541
    }
542
543
    public function testClearRelationshipDefinitionRefsByClearRelationshipDefinitions(): void
544
    {
545
        $this->assertTrue(count($this->tweety->getRelationshipDefinitionRefs()) > 0);
546
        $this->tweety->clearRelationships();
547
        $this->assertEmpty($this->tweety->getRelationshipDefinitions());
548
        // should affect animal relationship definition refs
549
        $this->assertEmpty($this->tweety->getRelationshipDefinitionRefs());
550
    }
551
552
    public function testAddRelationshipDefinitionRefElementsByHelper(): void
553
    {
554
        $this->assertCount(4, $this->tweety->getRelationshipDefinitionRefElements());
555
556
        $this->addRelationshipDefinition($this->tweety, $this->timmyRelationship);
557
        $timmyRelationshipDefinitionRef = $this->modelInstance->newInstance(RelationshipDefinitionRef::class);
558
        $timmyRelationshipDefinitionRef->setTextContent($this->timmyRelationship->getId());
559
        $this->tweety->addRelationshipDefinitionRefElement($timmyRelationshipDefinitionRef);
560
561
        $this->addRelationshipDefinition($this->tweety, $this->daisyRelationship);
562
        $daisyRelationshipDefinitionRef = $this->modelInstance->newInstance(RelationshipDefinitionRef::class);
563
        $daisyRelationshipDefinitionRef->setTextContent($this->daisyRelationship->getId());
564
        $this->tweety->addRelationshipDefinitionRefElement($daisyRelationshipDefinitionRef);
565
566
        $this->assertCount(6, $this->tweety->getRelationshipDefinitionRefElements());
567
568
        $rels = [
569
            $timmyRelationshipDefinitionRef,
570
            $daisyRelationshipDefinitionRef
571
        ];
572
        foreach ($rels as $rel) {
573
            $exists = false;
574
            foreach ($this->tweety->getRelationshipDefinitionRefElements() as $rel2) {
575
                if ($rel2->equals($rel)) {
576
                    $exists = true;
577
                }
578
            }
579
            $this->assertTrue($exists);
580
        }
581
    }
582
583
    public function testRelationshipDefinitionRefElementsByTextContent(): void
584
    {
585
        $relationshipDefinitionRefElements = $this->tweety->getRelationshipDefinitionRefElements();
586
        $textContents = [];
587
        foreach ($relationshipDefinitionRefElements as $relationshipDefinitionRef) {
588
            $textContent = $relationshipDefinitionRef->getTextContent();
589
            $this->assertTrue(!empty($textContent));
590
            $textContents[] = $textContent;
591
        }
592
        $this->assertCount(4, $textContents);
593
        $this->assertContains($this->hedwigRelationship->getId(), $textContents);
594
        $this->assertContains($this->birdoRelationship->getId(), $textContents);
595
        $this->assertContains($this->pluckyRelationship->getId(), $textContents);
596
        $this->assertContains($this->fiffyRelationship->getId(), $textContents);
597
    }
598
599
    public function testUpdateRelationshipDefinitionRefElementsByTextContent(): void
600
    {
601
        $relationshipDefinitionRefs = $this->tweety->getRelationshipDefinitionRefElements();
602
603
        $this->addRelationshipDefinition($this->tweety, $this->timmyRelationship);
604
        $relationshipDefinitionRefs[0]->setTextContent($this->timmyRelationship->getId());
605
606
        $this->addRelationshipDefinition($this->daisy, $this->daisyRelationship);
607
        $relationshipDefinitionRefs[2]->setTextContent($this->daisyRelationship->getId());
608
609
        $this->assertCount(4, $this->tweety->getRelationshipDefinitionRefs());
610
611
        $rels = [
612
            $this->birdoRelationship,
613
            $this->fiffyRelationship,
614
            $this->timmyRelationship,
615
            $this->daisyRelationship
616
        ];
617
        foreach ($rels as $rel) {
618
            $exists = false;
619
            foreach ($this->tweety->getRelationshipDefinitionRefs() as $rel2) {
620
                if ($rel2->equals($rel)) {
621
                    $exists = true;
622
                }
623
            }
624
            $this->assertTrue($exists);
625
        }
626
    }
627
628
    public function testUpdateRelationshipDefinitionRefElementsByTextContentWithNamespace(): void
629
    {
630
        $relationshipDefinitionRefs = $this->tweety->getRelationshipDefinitionRefElements();
631
632
        $this->addRelationshipDefinition($this->tweety, $this->timmyRelationship);
633
        $relationshipDefinitionRefs[0]->setTextContent("tns:" . $this->timmyRelationship->getId());
634
635
        $this->addRelationshipDefinition($this->daisy, $this->daisyRelationship);
636
        $relationshipDefinitionRefs[2]->setTextContent("tns:" . $this->daisyRelationship->getId());
637
638
        $rels = [
639
            $this->birdoRelationship,
640
            $this->fiffyRelationship,
641
            $this->timmyRelationship,
642
            $this->daisyRelationship
643
        ];
644
        foreach ($rels as $rel) {
645
            $exists = false;
646
            foreach ($this->tweety->getRelationshipDefinitionRefs() as $rel2) {
647
                if ($rel2->equals($rel)) {
648
                    $exists = true;
649
                }
650
            }
651
            $this->assertTrue($exists);
652
        }
653
    }
654
655
    public function testUpdateRelationshipDefinitionRefElementsByRemoveElements(): void
656
    {
657
        $relationshipDefinitionRefs = $this->tweety->getRelationshipDefinitionRefElements();
658
        $this->tweety->removeRelationshipDefinitionRefElement($relationshipDefinitionRefs[1]);
659
        $this->tweety->removeRelationshipDefinitionRefElement($relationshipDefinitionRefs[3]);
660
661
        $rels = [$this->hedwigRelationship, $this->pluckyRelationship];
662
        foreach ($rels as $rel) {
663
            $exists = false;
664
            foreach ($this->tweety->getRelationshipDefinitionRefs() as $rel2) {
665
                if ($rel2->equals($rel)) {
666
                    $exists = true;
667
                }
668
            }
669
            $this->assertTrue($exists);
670
        }
671
    }
672
673
    public function testClearRelationshipDefinitionRefElements(): void
674
    {
675
        $this->tweety->clearRelationshipDefinitionRefElements();
676
        $this->assertEmpty($this->tweety->getRelationshipDefinitionRefElements());
677
        $this->assertEmpty($this->tweety->getRelationshipDefinitionRefs());
678
679
        // should not affect animal relationship definitions
680
        $this->assertCount(4, $this->tweety->getRelationshipDefinitions());
681
    }
682
683
    public function testClearRelationshipDefinitionRefElementsByClearRelationshipDefinitionRefs(): void
684
    {
685
        $this->tweety->clearRelationshipDefinitions();
686
        $this->assertEmpty($this->tweety->getRelationshipDefinitionRefs());
687
        $this->assertEmpty($this->tweety->getRelationshipDefinitionRefElements());
688
        // should not affect animal relationship definitions
689
        $this->assertCount(4, $this->tweety->getRelationshipDefinitions());
690
    }
691
692
    public function testClearRelationshipDefinitionRefElementsByClearRelationshipDefinitions(): void
693
    {
694
        $this->tweety->clearRelationships();
695
        $this->assertEmpty($this->tweety->getRelationshipDefinitionRefs());
696
        $this->assertEmpty($this->tweety->getRelationshipDefinitionRefElements());
697
        // should affect animal relationship definitions
698
        $this->assertEmpty($this->tweety->getRelationshipDefinitions());
699
    }
700
701
    public function testGetBestFriends(): void
702
    {
703
        $bestFriends = $this->tweety->getBestFriends();
704
        $this->assertCount(2, $bestFriends);
705
706
        $birds = [$this->birdo, $this->plucky];
707
708
        foreach ($birds as $bird) {
709
            $exists = false;
710
            foreach ($bestFriends as $friend) {
711
                if ($friend->equals($bird)) {
712
                    $exists = true;
713
                }
714
            }
715
            $this->assertTrue($exists);
716
        }
717
    }
718
719
    public function testAddBestFriend(): void
720
    {
721
        $this->tweety->addFriend($this->daisy);
722
723
        $bestFriends = $this->tweety->getBestFriends();
724
725
        $birds = [$this->birdo, $this->plucky, $this->daisy];
726
727
        foreach ($birds as $bird) {
728
            $exists = false;
729
            foreach ($bestFriends as $friend) {
730
                if ($friend->equals($bird)) {
731
                    $exists = true;
732
                }
733
            }
734
            $this->assertTrue($exists);
735
        }
736
    }
737
738
    public function testRemoveBestFriendRef(): void
739
    {
740
        $this->tweety->removeFriend($this->plucky);
741
742
        $bestFriends = $this->tweety->getBestFriends();
743
744
        $this->assertCount(1, $bestFriends);
745
        $this->birdo->equals($bestFriends[0]);
746
    }
747
748
    public function testClearBestFriendRef(): void
749
    {
750
        $this->tweety->clearFriends();
751
752
        $bestFriends = $this->tweety->getBestFriends();
753
754
        $this->assertEmpty($bestFriends);
755
    }
756
757
    public function testClearAndAddBestFriendRef(): void
758
    {
759
        $this->tweety->clearFriends();
760
761
        $this->tweety->addFriend($this->daisy);
762
763
        $this->assertCount(1, $this->tweety->getBestFriends());
764
    }
765
}
766