Passed
Pull Request — master (#7162)
by
unknown
12:11
created

GH7162Test::setUp()   A

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
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
0 ignored issues
show
introduced by
Expected 2 newlines between PHP open tag and declare statement, found 1.
Loading history...
3
4
namespace Doctrine\Tests\ORM\Functional\Ticket;
5
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\ORM\Annotation as ORM;
9
use Doctrine\ORM\ORMInvalidArgumentException;
10
use Doctrine\Tests\OrmFunctionalTestCase;
11
12
final class GH7162Test extends OrmFunctionalTestCase
13
{
14
    public function setUp(): void
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
15
    {
16
        parent::setUp();
17
        $this->setUpEntitySchema([
18
            GH7162Parent::class,
19
            GH7162Child::class,
20
        ]);
21
    }
22
23
    /**
24
     * @group 7067
25
     */
26
    public function testIssueWithDetachedEntity(): void
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
27
    {
28
        // Create a parent without children
29
        $parent = new GH7162Parent();
30
        $this->em->persist($parent);
31
        $this->em->flush();
32
        $this->em->clear();
33
34
        $parentId = $parent->id;
35
36
        // Fetch the parent as a non-cached entity
37
        /** @var GH7162Parent $parent */
38
        $parent = $this->em->find(GH7162Parent::class, $parentId);
39
40
        // Create a new child and add it to the persistent collection
41
        // of children: $parent->children
42
        $child1 = new GH7162Child();
43
        $parent->addChild($child1);
44
45
        // Then, remove the same child, causing an orphan removal
46
        $parent->removeChild($child1);
47
48
        $caughtException = null;
49
        $message = '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
50
        try {
51
            $this->em->flush();
52
        } catch (ORMInvalidArgumentException $exception) {
53
            $message = $exception->getMessage();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
54
            $caughtException = $exception;
55
        }
56
        $this->em->clear();
57
58
        self::assertNull(
59
            $caughtException,
60
            'Child entity is detached and should not be scheduled for orphan removal, but it is.'
61
            . ' '
62
            . 'Message: '
63
            . $message
64
        );
65
    }
66
}
67
68
/**
69
 * @ORM\Entity
70
 */
71
class GH7162Parent
72
{
73
    /**
74
     * @ORM\Id
75
     * @ORM\GeneratedValue
76
     * @ORM\Column(type="integer")
77
     *
78
     * @var int
79
     */
80
    public $id;
81
82
    /**
83
     * @ORM\OneToMany(targetEntity=GH7162Child::class, mappedBy="parent", cascade={"remove","persist"}, orphanRemoval=true)
84
     *
85
     * @var GH7162Child[]|Collection
86
     */
87
    public $children;
88
89
    public function __construct()
90
    {
91
        $this->children = new ArrayCollection();
92
    }
93
94
    /**
95
     * @param GH7162Child $child
96
     */
97
    public function addChild(GH7162Child $child): void
0 ignored issues
show
introduced by
Method \Doctrine\Tests\ORM\Functional\Ticket\GH7162Parent::addChild() does not need documentation comment.
Loading history...
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
98
    {
99
        if ($this->children->contains($child)) {
100
            return;
101
        }
102
103
        $this->children->add($child);
104
    }
105
106
    /**
107
     * @param GH7162Child $child
108
     */
109
    public function removeChild(GH7162Child $child): void
0 ignored issues
show
introduced by
Method \Doctrine\Tests\ORM\Functional\Ticket\GH7162Parent::removeChild() does not need documentation comment.
Loading history...
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
110
    {
111
        if (!$this->children->contains($child)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
112
            return;
113
        }
114
115
        $this->children->removeElement($child);
116
    }
117
}
118
119
/**
120
 * @ORM\Entity
121
 */
122
class GH7162Child
123
{
124
    /**
125
     * @ORM\Id
126
     * @ORM\GeneratedValue
127
     * @ORM\Column(type="integer")
128
     *
129
     * @var int
130
     */
131
    public $id;
132
133
    /**
134
     * @ORM\ManyToOne(targetEntity=GH7162Parent::class, inversedBy="children")
135
     * @ORM\JoinColumn(referencedColumnName="id")
136
     *
137
     * @var GH7162Parent
138
     */
139
    public $parent;
140
}
141