testChildElementsCollection()   B
last analyzed

Complexity

Conditions 10
Paths 64

Size

Total Lines 58
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 36
c 0
b 0
f 0
nc 64
nop 0
dl 0
loc 58
rs 7.6666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Tests\Type;
4
5
use PHPUnit\Framework\TestCase;
6
use Xml\ModelBuilder;
7
use Tests\TestModel\{
8
    Gender,
9
    TestModelConstants,
10
    TestModelParser
11
};
12
use Tests\TestModel\TestModelTest;
13
use Tests\TestModel\Instance\{
14
    Animal,
15
    Animals,
16
    FlyingAnimal,
17
    FlightInstructor,
18
    FlightPartnerRef
19
};
20
21
class ChildElementCollectionCreateTest extends TestModelTest
22
{
23
    protected $tweety;
24
    protected $daffy;
25
    protected $daisy;
26
    protected $plucky;
27
    protected $birdo;
28
    protected $flightInstructorChild;
29
    protected $flightPartnerRefCollection;
30
31
    protected function setUp(): void
32
    {
33
        $this->modelParser = new TestModelParser();
34
        $this->modelInstance = $this->modelParser->getEmptyModel();
35
36
        $animals = $this->modelInstance->newInstance(Animals::class);
37
        $this->modelInstance->setDocumentElement($animals);
38
39
        $this->tweety = $this->createBird($this->modelInstance, "tweety", Gender::FEMALE);
40
        $this->daffy = $this->createBird($this->modelInstance, "daffy", Gender::MALE);
41
        $this->daisy = $this->createBird($this->modelInstance, "daisy", Gender::FEMALE);
42
        $this->plucky = $this->createBird($this->modelInstance, "plucky", Gender::MALE);
43
        $this->birdo = $this->createBird($this->modelInstance, "birdo", Gender::FEMALE);
44
45
        $this->tweety->setFlightInstructor($this->daffy);
46
        $this->tweety->addFlightPartnerRef($this->daisy);
47
        $this->tweety->addFlightPartnerRef($this->plucky);
48
49
        $this->flightInstructorChild = FlyingAnimal::$flightInstructorChild->getReferenceSourceCollection();
50
        $this->flightPartnerRefCollection = FlyingAnimal::$flightPartnerRefsColl->getReferenceSourceCollection();
51
    }
52
53
    public function testImmutable(): void
54
    {
55
        $this->assertFalse($this->flightInstructorChild->isImmutable());
56
        $this->assertFalse($this->flightPartnerRefCollection->isImmutable());
57
58
        $this->flightInstructorChild->setImmutable();
59
        $this->flightPartnerRefCollection->setImmutable();
60
        $this->assertTrue($this->flightInstructorChild->isImmutable());
61
        $this->assertTrue($this->flightPartnerRefCollection->isImmutable());
62
63
        $this->flightInstructorChild->setMutable(true);
64
        $this->flightPartnerRefCollection->setMutable(true);
65
        $this->assertFalse($this->flightInstructorChild->isImmutable());
66
        $this->assertFalse($this->flightPartnerRefCollection->isImmutable());
67
    }
68
69
    public function testMinOccurs(): void
70
    {
71
        $this->assertFalse($this->flightInstructorChild->getMinOccurs() != 0);
72
        $this->assertFalse($this->flightPartnerRefCollection->getMinOccurs() != 0);
73
    }
74
75
    public function testMaxOccurs(): void
76
    {
77
        $this->assertEquals(1, $this->flightInstructorChild->getMaxOccurs());
78
        $this->assertEquals(-1, $this->flightPartnerRefCollection->getMaxOccurs());
79
    }
80
81
    public function testChildElementType(): void
82
    {
83
        $this->assertEquals(FlightInstructor::class, $this->flightInstructorChild->getChildElementTypeClass());
84
        $this->assertEquals(FlightPartnerRef::class, $this->flightPartnerRefCollection->getChildElementTypeClass());
85
    }
86
87
    public function testParentElementType(): void
88
    {
89
        $flyingAnimalType = $this->modelInstance->getModel()->getType(FlyingAnimal::class);
90
        $this->assertEquals(get_class($flyingAnimalType), get_class($this->flightInstructorChild->getParentElementType()));
91
        $this->assertEquals(get_class($flyingAnimalType), get_class($this->flightPartnerRefCollection->getParentElementType()));
92
    }
93
94
    public function testGetChildElements(): void
95
    {
96
        $this->assertCount(1, $this->flightInstructorChild->get($this->tweety));
97
        $this->assertCount(2, $this->flightPartnerRefCollection->get($this->tweety));
98
99
        $flightInstructor = $this->flightInstructorChild->getChild($this->tweety);
100
        $this->assertEquals($this->daffy->getId(), $flightInstructor->getTextContent());
101
102
        $collection = $this->flightPartnerRefCollection->get($this->tweety);
103
        foreach ($collection as $flightPartnerRef) {
104
            $this->assertContains($flightPartnerRef->getTextContent(), [$this->daisy->getId(), $this->plucky->getId()]);
105
        }
106
    }
107
108
    public function testRemoveChildElements(): void
109
    {
110
        $this->assertFalse(empty($this->flightInstructorChild->get($this->tweety)));
111
        $this->assertFalse(empty($this->flightPartnerRefCollection->get($this->tweety)));
112
113
        $this->flightInstructorChild->removeChild($this->tweety);
114
        $this->flightPartnerRefCollection->clear($this->tweety);
115
116
        $this->assertTrue(empty($this->flightInstructorChild->get($this->tweety)));
117
        $this->assertTrue(empty($this->flightPartnerRefCollection->get($this->tweety)));
118
    }
119
120
    public function testChildElementsCollection(): void
121
    {
122
        $flightPartnerRefs = $this->flightPartnerRefCollection->get($this->tweety);
123
124
        $daisyRef = $flightPartnerRefs[0];
125
        $pluckyRef = $flightPartnerRefs[1];
126
127
        $this->assertEquals($this->daisy->getId(), $daisyRef->getTextContent());
128
        $this->assertEquals($this->plucky->getId(), $pluckyRef->getTextContent());
129
130
        $birdoRef = $this->modelInstance->newInstance(FlightPartnerRef::class);
131
        $birdoRef->setTextContent($this->birdo->getId());
132
133
        $flightPartners = [$birdoRef, $daisyRef, $pluckyRef];
134
135
        // directly test collection methods and not use the appropriate assertion methods
136
        $this->assertCount(2, $flightPartnerRefs);
137
        $this->flightPartnerRefCollection->add($this->tweety, $birdoRef);
138
139
        $flightPartnerRefs = $this->flightPartnerRefCollection->get($this->tweety);
140
        $this->assertCount(3, $flightPartnerRefs);
141
142
        foreach ($flightPartners as $partner) {
143
            $exists = false;
144
            foreach ($this->flightPartnerRefCollection->get($this->tweety) as $ref) {
145
                if ($ref->equals($partner)) {
146
                    $exists = true;
147
                }
148
            }
149
            $this->assertTrue($exists);
150
        }
151
152
        $this->flightPartnerRefCollection->remove($this->tweety, $daisyRef);
153
        $flightPartnerRefs = $this->flightPartnerRefCollection->get($this->tweety);
154
        $this->assertCount(2, $flightPartnerRefs);
155
        foreach ([$birdoRef, $pluckyRef] as $partner) {
156
            $exists = false;
157
            foreach ($this->flightPartnerRefCollection->get($this->tweety) as $ref) {
158
                if ($ref->equals($partner)) {
159
                    $exists = true;
160
                }
161
            }
162
            $this->assertTrue($exists);
163
        }
164
165
        $this->flightPartnerRefCollection->addAll($this->tweety, $flightPartners);
166
        foreach ([$birdoRef, $daisyRef, $pluckyRef] as $partner) {
167
            $exists = false;
168
            foreach ($this->flightPartnerRefCollection->get($this->tweety) as $ref) {
169
                if ($ref->equals($partner)) {
170
                    $exists = true;
171
                }
172
            }
173
            $this->assertTrue($exists);
174
        }
175
176
        $this->flightPartnerRefCollection->clear($this->tweety);
177
        $this->assertEmpty($this->flightPartnerRefCollection->get($this->tweety));
178
    }
179
}
180