testTargetElementsCollection()   B
last analyzed

Complexity

Conditions 10
Paths 108

Size

Total Lines 66
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 46
c 0
b 0
f 0
nc 108
nop 0
dl 0
loc 66
rs 7.2581

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 ReferenceCreateTest extends TestModelTest
22
{
23
    protected $tweety;
24
    protected $daffy;
25
    protected $daisy;
26
    protected $plucky;
27
    protected $birdo;
28
    protected $flightPartnerRef;
29
30
    protected $animalType;
31
    protected $fatherReference;
32
    protected $motherReference;
33
    protected $flightPartnerRefsColl;
34
35
    protected function setUp(): void
36
    {
37
        $this->modelParser = new TestModelParser();
38
        $this->modelInstance = $this->modelParser->getEmptyModel();
39
40
        $animals = $this->modelInstance->newInstance(Animals::class);
41
        $this->modelInstance->setDocumentElement($animals);
42
43
        $this->tweety = $this->createBird($this->modelInstance, "tweety", Gender::FEMALE);
44
        $this->daffy = $this->createBird($this->modelInstance, "daffy", Gender::MALE);
45
        $this->daisy = $this->createBird($this->modelInstance, "daisy", Gender::FEMALE);
46
        $this->plucky = $this->createBird($this->modelInstance, "plucky", Gender::MALE);
47
        $this->birdo = $this->createBird($this->modelInstance, "birdo", Gender::FEMALE);
48
49
        $this->tweety->setFather($this->daffy);
50
        $this->tweety->setMother($this->daisy);
51
52
        // ID attribute reference
53
        $this->animalType = $this->modelInstance->getModel()->getType(Animal::class);
54
        $this->fatherReference = $this->animalType->getAttribute("father")->getOutgoingReferences()[0];
0 ignored issues
show
Bug introduced by
The method getAttribute() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        $this->fatherReference = $this->animalType->/** @scrutinizer ignore-call */ getAttribute("father")->getOutgoingReferences()[0];

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
        $this->motherReference = $this->animalType->getAttribute("mother")->getOutgoingReferences()[0];
56
        $this->flightPartnerRefsColl = FlyingAnimal::$flightPartnerRefsColl;
57
58
        $this->tweety->addFlightPartnerRef($this->daffy);
59
60
        $flightPartnerRefType = $this->modelInstance->getModel()->getType(FlightPartnerRef::class);
61
        $this->flightPartnerRef = $this->modelInstance->getModelElementsByType($flightPartnerRefType)[0];
62
    }
63
64
    public function testReferenceIdentifier(): void
65
    {
66
        $this->assertEquals($this->daffy->getId(), $this->fatherReference->getReferenceIdentifier($this->tweety));
67
        $this->assertEquals($this->daisy->getId(), $this->motherReference->getReferenceIdentifier($this->tweety));
68
        $this->assertEquals($this->daffy->getId(), $this->flightPartnerRefsColl->getReferenceIdentifier($this->tweety));
69
    }
70
71
    public function testReferenceTargetElement(): void
72
    {
73
        $this->assertTrue($this->fatherReference->getReferenceTargetElement($this->tweety)->equals($this->daffy));
74
        $this->assertTrue($this->motherReference->getReferenceTargetElement($this->tweety)->equals($this->daisy));
75
        $this->assertTrue($this->flightPartnerRefsColl->getReferenceTargetElement($this->tweety)->equals($this->daffy));
76
77
        $this->fatherReference->setReferenceTargetElement($this->tweety, $this->plucky);
78
        $this->motherReference->setReferenceTargetElement($this->tweety, $this->birdo);
79
        $this->flightPartnerRefsColl->setReferenceTargetElement($this->flightPartnerRef, $this->daisy);
80
81
        $this->assertTrue($this->fatherReference->getReferenceTargetElement($this->tweety)->equals($this->plucky));
82
        $this->assertTrue($this->motherReference->getReferenceTargetElement($this->tweety)->equals($this->birdo));
83
        $this->assertTrue($this->flightPartnerRefsColl->getReferenceTargetElement($this->tweety)->equals($this->daisy));
84
    }
85
86
    public function testReferenceSourceAttribute(): void
87
    {
88
        $fatherAttribute = $this->animalType->getAttribute("father");
89
        $motherAttribute = $this->animalType->getAttribute("mother");
90
91
        $this->assertTrue($this->fatherReference->getReferenceSourceAttribute() == $fatherAttribute);
92
        $this->assertTrue($this->motherReference->getReferenceSourceAttribute() == $motherAttribute);
93
    }
94
95
    public function testRemoveReference(): void
96
    {
97
        $this->fatherReference->referencedElementRemoved($this->daffy, $this->daffy->getId());
98
        $this->assertNull($this->fatherReference->getReferenceTargetElement($this->tweety));
99
100
        $this->motherReference->referencedElementRemoved($this->daisy, $this->daisy->getId());
101
        $this->assertNull($this->motherReference->getReferenceTargetElement($this->tweety));
102
    }
103
104
    public function testTargetElementsCollection(): void
105
    {
106
        $referenceTargetElements = $this->flightPartnerRefsColl->getReferenceTargetElements($this->tweety);
107
        $flightPartners = [$this->birdo, $this->daffy, $this->daisy, $this->plucky];
108
109
        $this->assertCount(1, $referenceTargetElements);
110
        $exists = false;
111
        foreach ($referenceTargetElements as $ref) {
112
            if ($ref->equals($this->daffy)) {
113
                $exists = true;
114
                break;
115
            }
116
        }
117
        $this->assertTrue($exists);
118
119
        $this->flightPartnerRefsColl->add($this->tweety, $this->daisy);
120
        $referenceTargetElements = $this->flightPartnerRefsColl->getReferenceTargetElements($this->tweety);
121
        $this->assertCount(2, $referenceTargetElements);
122
123
        $exists = false;
124
        foreach ($referenceTargetElements as $ref) {
125
            if ($ref->equals($this->daisy)) {
126
                $exists = true;
127
                break;
128
            }
129
        }
130
        $this->assertTrue($exists);
131
132
        $this->flightPartnerRefsColl->remove($this->tweety, $this->daisy);
133
        $referenceTargetElements = $this->flightPartnerRefsColl->getReferenceTargetElements($this->tweety);
134
        $this->assertCount(1, $referenceTargetElements);
135
        $exists = false;
136
        foreach ($referenceTargetElements as $ref) {
137
            if ($ref->equals($this->daffy)) {
138
                $exists = true;
139
                break;
140
            }
141
        }
142
        $this->assertTrue($exists);
143
144
        $this->flightPartnerRefsColl->addAll($this->tweety, $flightPartners);
145
        $referenceTargetElements = $this->flightPartnerRefsColl->getReferenceTargetElements($this->tweety);
146
        $this->assertCount(4, $referenceTargetElements);
147
148
        foreach ($flightPartners as $partner) {
149
            $exists = false;
150
            foreach ($referenceTargetElements as $ref) {
151
                if ($ref->equals($partner)) {
152
                    $exists = true;
153
                    break;
154
                }
155
            }
156
            $this->assertTrue($exists);
157
        }
158
159
        $this->flightPartnerRefsColl->removeAll($this->tweety, $flightPartners);
160
        $referenceTargetElements = $this->flightPartnerRefsColl->getReferenceTargetElements($this->tweety);
161
        $this->assertCount(0, $referenceTargetElements);
162
163
        $this->flightPartnerRefsColl->addAll($this->tweety, $flightPartners);
164
        $referenceTargetElements = $this->flightPartnerRefsColl->getReferenceTargetElements($this->tweety);
165
        $this->assertFalse(empty($referenceTargetElements));
166
167
        $this->flightPartnerRefsColl->clear($this->tweety);
168
        $referenceTargetElements = $this->flightPartnerRefsColl->getReferenceTargetElements($this->tweety);
169
        $this->assertTrue(empty($referenceTargetElements));
170
    }
171
}
172