ModelElementTypeTest::testTypeName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Tests\Type;
4
5
use PHPUnit\Framework\TestCase;
6
use Xml\ModelBuilder;
7
use Tests\TestModel\{
8
    TestModelConstants,
9
    TestModelParser
10
};
11
use Tests\TestModel\Instance\{
12
    Animals,
13
    Animal,
14
    Bird,
15
    FlyingAnimal,
16
    RelationshipDefinition,
17
    RelationshipDefinitionRef,
18
    FlightPartnerRef,
19
    Egg,
20
    SpouseRef
21
};
22
23
class ModelElementTypeTest extends TestCase
24
{
25
    private $modelInstance;
26
    private $model;
27
    private $animalsType;
28
    private $animalType;
29
    private $flyingAnimalType;
30
    private $birdType;
31
32
    protected function setUp(): void
33
    {
34
        $modelParser = new TestModelParser();
35
        $this->modelInstance = $modelParser->getEmptyModel();
36
        $this->model = $this->modelInstance->getModel();
37
        $this->animalsType = $this->model->getType(Animals::class);
38
        $this->animalType = $this->model->getType(Animal::class);
39
        $this->flyingAnimalType = $this->model->getType(FlyingAnimal::class);
40
        $this->birdType = $this->model->getType(Bird::class);
41
    }
42
43
    public function testTypeName(): void
44
    {
45
        $this->assertEquals('animals', $this->animalsType->getTypeName());
46
        $this->assertEquals('animal', $this->animalType->getTypeName());
47
        $this->assertEquals('flyingAnimal', $this->flyingAnimalType->getTypeName());
48
        $this->assertEquals('bird', $this->birdType->getTypeName());
49
    }
50
51
    public function testTypeNamespace(): void
52
    {
53
        $this->assertEquals(TestModelConstants::MODEL_NAMESPACE, $this->animalsType->getTypeNamespace());
54
        $this->assertEquals(TestModelConstants::MODEL_NAMESPACE, $this->animalType->getTypeNamespace());
55
        $this->assertEquals(TestModelConstants::MODEL_NAMESPACE, $this->flyingAnimalType->getTypeNamespace());
56
        $this->assertEquals(TestModelConstants::MODEL_NAMESPACE, $this->birdType->getTypeNamespace());
57
    }
58
59
    public function testInstanceType(): void
60
    {
61
        $this->assertEquals(Animals::class, $this->animalsType->getInstanceType());
62
        $this->assertEquals(Animal::class, $this->animalType->getInstanceType());
63
        $this->assertEquals(FlyingAnimal::class, $this->flyingAnimalType->getInstanceType());
64
        $this->assertEquals(Bird::class, $this->birdType->getInstanceType());
65
    }
66
67
    public function testAttributes(): void
68
    {
69
        $this->assertEmpty($this->animalsType->getAttributes());
70
        $attrs = ['id', 'name', 'father', 'mother', 'isEndangered', 'gender', 'age', 'bestFriendRefs'];
71
        $animalAttributes = $this->animalType->getAttributes();
72
        foreach ($animalAttributes as $attribute) {
73
            $this->assertContains($attribute->getAttributeName(), $attrs);
74
        }
75
76
        $flyingAnimalAttributes = array_map(function ($attr) {
77
            return $attr->getAttributeName();
78
        }, $this->flyingAnimalType->getAttributes());
79
        $this->assertContains('wingspan', $flyingAnimalAttributes);
80
81
        $birdAttributes = array_map(function ($attr) {
82
            return $attr->getAttributeName();
83
        }, $this->birdType->getAttributes());
84
        $this->assertContains('canHaveExtendedWings', $birdAttributes);
85
    }
86
87
    public function testBaseType(): void
88
    {
89
        $this->assertNull($this->animalsType->getBaseType());
90
        $this->assertNull($this->animalType->getBaseType());
91
        $this->assertEquals($this->animalType, $this->flyingAnimalType->getBaseType());
92
        $this->assertEquals($this->flyingAnimalType, $this->birdType->getBaseType());
93
    }
94
95
    public function testAbstractType(): void
96
    {
97
        $this->assertFalse($this->animalsType->isAbstract());
98
        $this->assertTrue($this->animalType->isAbstract());
99
        $this->assertTrue($this->flyingAnimalType->isAbstract());
100
        $this->assertFalse($this->birdType->isAbstract());
101
    }
102
103
    public function testModel(): void
104
    {
105
        $this->assertEquals($this->animalsType->getModel(), $this->model);
106
        $this->assertEquals($this->animalType->getModel(), $this->model);
107
        $this->assertEquals($this->flyingAnimalType->getModel(), $this->model);
108
        $this->assertEquals($this->birdType->getModel(), $this->model);
109
    }
110
111
    public function testInstances(): void
112
    {
113
        $this->assertEmpty($this->animalsType->getInstances($this->modelInstance));
114
        $this->assertEmpty($this->animalType->getInstances($this->modelInstance));
115
        $this->assertEmpty($this->flyingAnimalType->getInstances($this->modelInstance));
116
        $this->assertEmpty($this->birdType->getInstances($this->modelInstance));
117
118
        $animals = $this->animalsType->newInstance($this->modelInstance);
119
        $this->modelInstance->setDocumentElement($animals);
120
121
        $animals->addAnimal($this->birdType->newInstance($this->modelInstance));
122
        $animals->addAnimal($this->birdType->newInstance($this->modelInstance));
123
        $animals->addAnimal($this->birdType->newInstance($this->modelInstance));
124
125
        $this->assertCount(1, $this->animalsType->getInstances($this->modelInstance));
126
        $this->assertEmpty($this->animalType->getInstances($this->modelInstance));
127
        $this->assertEmpty($this->flyingAnimalType->getInstances($this->modelInstance));
128
        $this->assertCount(3, $this->birdType->getInstances($this->modelInstance));
129
130
        $this->expectException(\Xml\Exception\ModelTypeException::class);
131
        $this->animalType->newInstance($this->modelInstance);
132
    }
133
134
    public function testChildElementTypes(): void
135
    {
136
        $relationshipDefinitionType = $this->model->getType(RelationshipDefinition::class);
137
        $relationshipDefinitionRefType = $this->model->getType(RelationshipDefinitionRef::class);
138
        $flightPartnerRefType = $this->model->getType(FlightPartnerRef::class);
139
        $eggType = $this->model->getType(Egg::class);
140
        $spouseRefType = $this->model->getType(SpouseRef::class);
141
        $this->assertContains($this->animalType, $this->animalsType->getChildElementTypes());
142
        $this->assertContains($relationshipDefinitionType, $this->animalType->getChildElementTypes());
143
        $this->assertContains($relationshipDefinitionRefType, $this->animalType->getChildElementTypes());
144
        $this->assertContains($flightPartnerRefType, $this->flyingAnimalType->getChildElementTypes());
145
        $this->assertContains($eggType, $this->birdType->getChildElementTypes());
146
        $this->assertContains($spouseRefType, $this->birdType->getChildElementTypes());
147
    }
148
}
149