UnknownAnimalTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 72
c 0
b 0
f 0
dl 0
loc 124
rs 10
wmc 11

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testAddChildToUnknownAnimal() 0 5 1
A testAddUnknownAnimal() 0 16 1
A testAddRelationshipDefinitionToUnknownAnimal() 0 20 2
A testGetUnknownAttribute() 0 7 1
A testReplaceChildOfUnknownAnimal() 0 13 1
A testGetUnknownAnimalByType() 0 15 1
A copyModelInstance() 0 4 1
A setUp() 0 4 1
A testRemoveChildOfUnknownAnimal() 0 6 1
A testGetUnknownAnimalById() 0 11 1
1
<?php
2
3
namespace Tests\TestModel\Instance;
4
5
use Xml\Exception\ModelException;
6
use Xml\Impl\Util\StringUtil;
7
use Xml\ModelInstanceInterface;
8
use Xml\Impl\Parser\AbstractModelParser;
9
use Tests\TestModel\{
10
    Gender,
11
    TestModelConstants,
12
    TestModelParser
13
};
14
use Tests\TestModel\TestModelTest;
15
16
class UnknownAnimalTest extends TestModelTest
17
{
18
    private $wanda;
19
    private $flipper;
20
21
    protected function setUp(): void
22
    {
23
        parent::parseModel("UnknownAnimalTest");
24
        $this->copyModelInstance();
25
    }
26
27
    private function copyModelInstance(): void
28
    {
29
        $this->wanda = $this->modelInstance->getModelElementById("wanda");
30
        $this->flipper = $this->modelInstance->getModelElementById("flipper");
31
    }
32
33
    public function testGetUnknownAnimalById(): void
34
    {
35
        $this->assertFalse($this->wanda === null);
36
        $this->assertEquals("wanda", $this->wanda->getAttributeValue("id"));
37
        $this->assertEquals("Female", $this->wanda->getAttributeValue("gender"));
38
        $this->assertEquals("fish", $this->wanda->getAttributeValue("species"));
39
40
        $this->assertFalse($this->flipper === null);
41
        $this->assertEquals("flipper", $this->flipper->getAttributeValue("id"));
42
        $this->assertEquals("Male", $this->flipper->getAttributeValue("gender"));
43
        $this->assertEquals("dolphin", $this->flipper->getAttributeValue("species"));
44
    }
45
46
    public function testGetUnknownAnimalByType(): void
47
    {
48
        $unknownAnimalType = $this->modelInstance->registerGenericType(TestModelConstants::MODEL_NAMESPACE, "unknownAnimal");
49
        $unknownAnimals = $this->modelInstance->getModelElementsByType($unknownAnimalType);
50
        $this->assertCount(2, $unknownAnimals);
51
52
        $wanda = $unknownAnimals[0];
53
        $this->assertEquals("wanda", $wanda->getAttributeValue("id"));
54
        $this->assertEquals("Female", $wanda->getAttributeValue("gender"));
55
        $this->assertEquals("fish", $wanda->getAttributeValue("species"));
56
57
        $flipper = $unknownAnimals[1];
58
        $this->assertEquals("flipper", $flipper->getAttributeValue("id"));
59
        $this->assertEquals("Male", $flipper->getAttributeValue("gender"));
60
        $this->assertEquals("dolphin", $flipper->getAttributeValue("species"));
61
    }
62
63
    public function testAddUnknownAnimal(): void
64
    {
65
        $unknownAnimalType = $this->modelInstance->registerGenericType(TestModelConstants::MODEL_NAMESPACE, "unknownAnimal");
66
        $animalsType = $this->modelInstance->getModel()->getType(Animals::class);
67
        $animalType = $this->modelInstance->getModel()->getType(Animal::class);
68
69
        $unknownAnimal = $this->modelInstance->newInstance($unknownAnimalType);
70
        $this->assertFalse($unknownAnimal === null);
71
        $unknownAnimal->setAttributeValue("id", "new-animal", true);
72
        $unknownAnimal->setAttributeValue("gender", "Unknown");
73
        $unknownAnimal->setAttributeValue("species", "unknown");
74
75
        $animals = $this->modelInstance->getModelElementsByType($animalsType)[0];
76
        $childElementsByType = $animals->getChildElementsByType($animalType);
77
        $animals->insertElementAfter($unknownAnimal, $childElementsByType[2]);
78
        $this->assertCount(3, $animals->getChildElementsByType($unknownAnimalType));
79
    }
80
81
    public function testGetUnknownAttribute(): void
82
    {
83
        $this->assertEquals("true", $this->flipper->getAttributeValue("famous"));
84
        $this->assertFalse("true" == $this->wanda->getAttributeValue("famous"));
85
86
        $this->wanda->setAttributeValue("famous", "true");
87
        $this->assertEquals("true", $this->wanda->getAttributeValue("famous"));
88
    }
89
90
    public function testAddRelationshipDefinitionToUnknownAnimal(): void
91
    {
92
        $friendRelationshipDefinition = $this->modelInstance->newInstance(FriendRelationshipDefinition::class);
93
        $friendRelationshipDefinition->setId("friend-relationship");
94
        $friendRelationshipDefinition->setAttributeValue("animalRef", $this->flipper->getAttributeValue("id"));
95
96
        try {
97
            $this->wanda->addChildElement($friendRelationshipDefinition);
98
        } catch (\Exception $e) {
99
            $this->assertEquals(ModelException::class, get_class($e));
100
        }
101
102
        $this->wanda->insertElementAfter($friendRelationshipDefinition, null);
103
104
        $tweety = $this->modelInstance->getModelElementById("tweety");
105
        $childRelationshipDefinition = $this->modelInstance->newInstance(ChildRelationshipDefinition::class);
106
        $childRelationshipDefinition->setId("child-relationship");
107
        $childRelationshipDefinition->setAnimal($tweety);
108
109
        $this->wanda->insertElementAfter($childRelationshipDefinition, $friendRelationshipDefinition);
110
    }
111
112
    public function testAddChildToUnknownAnimal(): void
113
    {
114
        $this->assertEmpty($this->wanda->getChildElementsByType($this->flipper->getElementType()));
115
        $this->wanda->insertElementAfter($this->flipper, null);
116
        $this->assertCount(1, $this->wanda->getChildElementsByType($this->flipper->getElementType()));
117
    }
118
119
    public function testRemoveChildOfUnknownAnimal(): void
120
    {
121
        $this->assertFalse($this->wanda->removeChildElement($this->flipper));
122
        $this->wanda->insertElementAfter($this->flipper, null);
123
        $this->assertTrue($this->wanda->removeChildElement($this->flipper));
124
        $this->assertEmpty($this->wanda->getChildElementsByType($this->flipper->getElementType()));
125
    }
126
127
    public function testReplaceChildOfUnknownAnimal(): void
128
    {
129
        $yogi = $this->modelInstance->newInstance($this->flipper->getElementType());
130
        $yogi->setAttributeValue("id", "yogi-bear", true);
131
        $yogi->setAttributeValue("gender", "Male");
132
        $yogi->setAttributeValue("species", "bear");
133
134
        $this->assertEmpty($this->wanda->getChildElementsByType($this->flipper->getElementType()));
135
        $this->wanda->insertElementAfter($this->flipper, null);
136
        $this->assertCount(1, $this->wanda->getChildElementsByType($this->flipper->getElementType()));
137
        $this->wanda->replaceChildElement($this->flipper, $yogi);
138
        $this->assertCount(1, $this->wanda->getChildElementsByType($this->flipper->getElementType()));
139
        $this->assertTrue($this->wanda->getChildElementsByType($this->flipper->getElementType())[0]->equals($yogi));
140
    }
141
}
142