Failed Conditions
Pull Request — master (#7155)
by Gabriel
12:44
created

Ticket69Test::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\ORM\Annotation as ORM;
9
use Doctrine\Tests\OrmFunctionalTestCase;
10
11
/**
12
 * Functional tests for the Single Table Inheritance mapping strategy.
13
 */
14
class Ticket69Test extends OrmFunctionalTestCase
15
{
16
    protected function setUp() : void
17
    {
18
        parent::setUp();
19
        try {
20
            $this->schemaTool->createSchema(
21
                [
22
                    $this->em->getClassMetadata(Lemma::class),
23
                    $this->em->getClassMetadata(Relation::class),
24
                    $this->em->getClassMetadata(RelationType::class),
25
                ]
26
            );
27
        } catch (\Exception $e) {
28
            // Swallow all exceptions. We do not test the schema tool here.
29
        }
30
    }
31
32
    public function testIssue() : void
33
    {
34
        //setup
35
        $lemma1 = new Lemma();
36
        $lemma1->setLemma('foo');
37
38
        $lemma2 = new Lemma();
39
        $lemma2->setLemma('bar');
40
41
        $lemma3 = new Lemma();
42
        $lemma3->setLemma('batz');
43
44
        $lemma4 = new Lemma();
45
        $lemma4->setLemma('bla');
46
47
        $type1 = new RelationType();
48
        $type1->setType('nonsense');
49
        $type1->setAbbreviation('non');
50
51
        $type2 = new RelationType();
52
        $type2->setType('quatsch');
53
        $type2->setAbbreviation('qu');
54
55
        $relation1 = new Relation();
56
        $relation1->setParent($lemma1);
57
        $relation1->setChild($lemma2);
58
        $relation1->setType($type1);
59
60
        $relation2 = new Relation();
61
        $relation2->setParent($lemma1);
62
        $relation2->setChild($lemma3);
63
        $relation2->setType($type1);
64
65
        $relation3 = new Relation();
66
        $relation3->setParent($lemma1);
67
        $relation3->setChild($lemma4);
68
        $relation3->setType($type2);
69
70
        $lemma1->addRelation($relation1);
71
        $lemma1->addRelation($relation2);
72
        $lemma1->addRelation($relation3);
73
74
        $this->em->persist($type1);
75
        $this->em->persist($type2);
76
        $this->em->persist($lemma1);
77
        $this->em->persist($lemma2);
78
        $this->em->persist($lemma3);
79
        $this->em->persist($lemma4);
80
81
        $this->em->flush();
82
        $this->em->clear();
83
        //end setup
84
85
        // test One To Many
86
        $query = $this->em->createQuery("SELECT l FROM Doctrine\Tests\ORM\Functional\Ticket\Lemma l Where l.lemma = 'foo'");
87
        $res   = $query->getResult();
88
        $lemma = $res[0];
89
90
        self::assertEquals('foo', $lemma->getLemma());
91
        self::assertInstanceOf(Lemma::class, $lemma);
92
        $relations = $lemma->getRelations();
93
94
        foreach ($relations as $relation) {
95
            self::assertInstanceOf(Relation::class, $relation);
96
            self::assertTrue($relation->getType()->getType() !== '');
97
        }
98
99
        $this->em->clear();
100
    }
101
}
102
103
/**
104
 * @ORM\Entity
105
 * @ORM\Table(name="lemma")
106
 */
107
class Lemma
108
{
109
    public const CLASS_NAME = __CLASS__;
110
111
    /**
112
     * @var int
113
     * @ORM\Id
114
     * @ORM\Column(type="integer", name="lemma_id")
115
     * @ORM\GeneratedValue(strategy="AUTO")
116
     */
117
    private $id;
118
119
    /**
120
     * @var string
121
     * @ORM\Column(type="string", name="lemma_name", unique=true, length=255)
122
     */
123
    private $lemma;
124
125
126
    /**
127
     * @var ArrayCollection
128
     * @ORM\OneToMany(targetEntity=Relation::class, mappedBy="parent", cascade={"persist"})
129
     */
130
    private $relations;
131
132
    public function __construct()
133
    {
134
        $this->types     = new ArrayCollection();
0 ignored issues
show
Bug Best Practice introduced by
The property types does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
135
        $this->relations = new ArrayCollection();
136
    }
137
138
139
    /**
140
     * @return int
141
     */
142
    public function getId()
143
    {
144
        return $this->id;
145
    }
146
147
    /**
148
     * @param string $lemma
149
     */
150
    public function setLemma($lemma)
151
    {
152
        $this->lemma = $lemma;
153
    }
154
155
    /**
156
     * @return string
157
     */
158
    public function getLemma()
159
    {
160
        return $this->lemma;
161
    }
162
163
164
    public function addRelation(Relation $relation)
165
    {
166
        $this->relations[] = $relation;
167
        $relation->setParent($this);
168
    }
169
170
    public function removeRelation(Relation $relation)
171
    {
172
        /*@var $removed Relation */
173
        $removed = $this->relations->removeElement($relation);
174
        if ($removed !== null) {
175
            $removed->removeParent();
176
        }
177
    }
178
179
    /**
180
     * @return ArrayCollection
181
     */
182
    public function getRelations()
183
    {
184
        return $this->relations;
185
    }
186
}
187
188
/**
189
 * @ORM\Entity
190
 * @ORM\Table(name="relation")
191
 */
192
class Relation
193
{
194
    public const CLASS_NAME = __CLASS__;
195
196
    /**
197
     * @var int
198
     * @ORM\Id
199
     * @ORM\Column(type="integer", name="relation_id")
200
     * @ORM\GeneratedValue(strategy="AUTO")
201
     */
202
    private $id;
203
204
    /**
205
     * @var Lemma
206
     * @ORM\ManyToOne(targetEntity=Lemma::class, inversedBy="relations")
207
     * @ORM\JoinColumn(name="relation_parent_id", referencedColumnName="lemma_id")
208
     */
209
    private $parent;
210
211
    /**
212
     * @var Lemma
213
     * @ORM\OneToOne(targetEntity=Lemma::class)
214
     * @ORM\JoinColumn(name="relation_child_id", referencedColumnName="lemma_id")
215
     */
216
    private $child;
217
218
    /**
219
     * @var RelationType
220
     * @ORM\ManyToOne(targetEntity=RelationType::class, inversedBy="relations")
221
     * @ORM\JoinColumn(name="relation_type_id", referencedColumnName="relation_type_id")
222
     */
223
    private $type;
224
225
    public function setParent(Lemma $parent)
226
    {
227
        $this->parent = $parent;
228
    }
229
230
    /**
231
     * @return Phrase
0 ignored issues
show
Bug introduced by
The type Doctrine\Tests\ORM\Functional\Ticket\Phrase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
232
     */
233
    public function getParent()
234
    {
235
        return $this->parent;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->parent returns the type Doctrine\Tests\ORM\Functional\Ticket\Lemma which is incompatible with the documented return type Doctrine\Tests\ORM\Functional\Ticket\Phrase.
Loading history...
236
    }
237
238
    public function removeParent()
239
    {
240
        if ($this->lemma !== null) {
0 ignored issues
show
Bug Best Practice introduced by
The property lemma does not exist on Doctrine\Tests\ORM\Functional\Ticket\Relation. Did you maybe forget to declare it?
Loading history...
241
            /*@var $phrase Lemma */
242
            $lemma        = $this->parent;
243
            $this->parent = null;
244
            $lemma->removeRelation($this);
245
        }
246
    }
247
248
    public function setChild(Lemma $child)
249
    {
250
        $this->child = $child;
251
    }
252
253
    /**
254
     * @return Lemma
255
     */
256
    public function getChild()
257
    {
258
        return $this->child;
259
    }
260
261
    public function setType(RelationType $type)
262
    {
263
        $this->type = $type;
264
    }
265
266
    /**
267
     * @return RelationType
268
     */
269
    public function getType()
270
    {
271
        return $this->type;
272
    }
273
274
    public function removeType()
275
    {
276
        if ($this->type !== null) {
277
            /*@var $phrase RelationType */
278
            $type       = $this->type;
279
            $this->type = null;
280
            $type->removeRelation($this);
281
        }
282
    }
283
}
284
285
/**
286
 * @ORM\Entity
287
 * @ORM\Table(name="relation_type")
288
 */
289
class RelationType
290
{
291
    public const CLASS_NAME = __CLASS__;
292
293
    /**
294
     * @var int
295
     * @ORM\Id
296
     * @ORM\Column(type="integer", name="relation_type_id")
297
     * @ORM\GeneratedValue(strategy="AUTO")
298
     */
299
    private $id;
300
301
    /**
302
     * @var string
303
     * @ORM\Column(type="string", name="relation_type_name", unique=true, length=255)
304
     */
305
    private $type;
306
307
    /**
308
     * @var string
309
     * @ORM\Column(type="string", name="relation_type_abbreviation", unique=true, length=255)
310
     */
311
    private $abbreviation;
312
313
    /**
314
     * @var ArrayCollection
315
     * @ORM\OneToMany(targetEntity=Relation::class, mappedBy="type", cascade={"persist"})
316
     */
317
    private $relations;
318
319
    public function __construct()
320
    {
321
        $relations = new ArrayCollection();
0 ignored issues
show
Unused Code introduced by
The assignment to $relations is dead and can be removed.
Loading history...
322
    }
323
324
    /**
325
     * @return int
326
     */
327
    public function getId()
328
    {
329
        return $this->id;
330
    }
331
332
    /**
333
     * @param string $type
334
     */
335
    public function setType($type)
336
    {
337
        $this->type = $type;
338
    }
339
340
    /**
341
     * @return string
342
     */
343
    public function getType()
344
    {
345
        return $this->type;
346
    }
347
348
    /**
349
     * @param string $abbreviation
350
     */
351
    public function setAbbreviation($abbreviation)
352
    {
353
        $this->abbreviation = $abbreviation;
354
    }
355
356
    /**
357
     * @return string
358
     */
359
    public function getAbbreviation()
360
    {
361
        return $this->abbreviation;
362
    }
363
364
    public function addRelation(Relation $relation)
365
    {
366
        $this->relations[] = $relation;
367
        $relation->setType($this);
368
    }
369
370
    public function removeRelation(Relation $relation)
371
    {
372
        /*@var $removed Relation */
373
        $removed = $this->relations->removeElement($relation);
374
        if ($removed !== null) {
375
            $removed->removeLemma();
0 ignored issues
show
Bug introduced by
The method removeLemma() does not exist on Doctrine\Tests\ORM\Functional\Ticket\Relation. ( Ignorable by Annotation )

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

375
            $removed->/** @scrutinizer ignore-call */ 
376
                      removeLemma();

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...
376
        }
377
    }
378
379
    /**
380
     * @return ArrayCollection
381
     */
382
    public function getRelations()
383
    {
384
        return $this->relations;
385
    }
386
}
387