Passed
Pull Request — 2.6 (#7766)
by
unknown
62:06
created

GH7767ParentEntity   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 29
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addChild() 0 4 1
A getChildren() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Doctrine\Common\Collections\Criteria;
8
use Doctrine\ORM\PersistentCollection;
9
use Doctrine\Tests\OrmFunctionalTestCase;
10
11
/**
12
 * Tests sorting mechanism of Selectable::matching() of PersistentCollection
13
 */
14
class GH7767Test extends OrmFunctionalTestCase
15
{
16
    /**
17
     * @group 7767
18
     */
19
    public function testMatchingRespectsCollectionOrdering(): void
20
    {
21
        $this->createFixture();
22
23
        $query = $this->_em->createQuery('select p from Doctrine\Tests\ORM\Functional\Ticket\GH7767ParentEntity p');
24
        $result = $query->getResult();
25
        $parent = $result[0];
26
27
        assert($parent instanceof GH7767ParentEntity);
28
29
        $children = $parent
30
            ->getChildren()
31
            ->matching(
32
                Criteria::create()
33
            );
34
35
        self::assertEquals(100, $children[0]->position);
36
        self::assertEquals(200, $children[1]->position);
37
        self::assertEquals(300, $children[2]->position);
38
    }
39
40
    /**
41
     * @group 7767
42
     */
43
    public function testMatchingOverrulesCollectionOrdering(): void
44
    {
45
        $this->createFixture();
46
47
        $query = $this->_em->createQuery('select p from Doctrine\Tests\ORM\Functional\Ticket\GH7767ParentEntity p');
48
        $result = $query->getResult();
49
        $parent = $result[0];
50
51
        assert($parent instanceof GH7767ParentEntity);
52
53
        $children = $parent
54
            ->getChildren()
55
            ->matching(
56
                Criteria::create()
57
                    ->orderBy(['position' => 'DESC'])
58
            );
59
60
        self::assertEquals(300, $children[0]->position);
61
        self::assertEquals(200, $children[1]->position);
62
        self::assertEquals(100, $children[2]->position);
63
    }
64
65
    protected function setUp(): void
66
    {
67
        parent::setUp();
68
69
        $this->setUpEntitySchema(
70
            [
71
                GH7767ParentEntity::class,
72
                GH7767ChildEntity::class
73
            ]
74
        );
75
    }
76
77
    private function createFixture()
78
    {
79
        $parent = new GH7767ParentEntity();
80
81
        $firstChild = new GH7767ChildEntity();
82
        $firstChild->position = 200;
83
84
        $secondChild = new GH7767ChildEntity();
85
        $secondChild->position = 100;
86
87
        $thirdChild = new GH7767ChildEntity();
88
        $thirdChild->position = 300;
89
90
        $parent->addChild($firstChild);
91
        $parent->addChild($secondChild);
92
        $parent->addChild($thirdChild);
93
94
        $this->_em->persist($parent);
95
96
        $this->_em->flush();
97
        $this->_em->clear();
98
    }
99
}
100
101
/**
102
 * @Entity
103
 */
104
class GH7767ParentEntity
105
{
106
    /**
107
     * @Id
108
     * @Column(type="integer")
109
     * @GeneratedValue
110
     */
111
    private $id;
0 ignored issues
show
introduced by
The private property $id is not used, and could be removed.
Loading history...
112
113
    /**
114
     * @OneToMany(
115
     *     targetEntity=GH7767ChildEntity::class,
116
     *     mappedBy="parent",
117
     *     fetch="EXTRA_LAZY",
118
     *     cascade={"persist"}
119
     * )
120
     * @OrderBy({"position" = "ASC"})
121
     */
122
    private $children;
123
124
    public function addChild(GH7767ChildEntity $child)
125
    {
126
        $this->children[] = $child;
127
        $child->setParent($this);
128
    }
129
130
    public function getChildren(): PersistentCollection
131
    {
132
        return $this->children;
133
    }
134
}
135
136
/**
137
 * @Entity
138
 */
139
class GH7767ChildEntity
140
{
141
    /**
142
     * @Column(type="integer")
143
     */
144
    public $position;
145
    /**
146
     * @Id
147
     * @Column(type="integer")
148
     * @GeneratedValue
149
     */
150
    private $id;
151
    /**
152
     * @ManyToOne(targetEntity=GH7767ParentEntity::class, inversedBy="children")
153
     * @JoinColumn(name="parent_id", referencedColumnName="id")
154
     */
155
    private $parent;
156
157
    public function setParent(GH7767ParentEntity $parent)
158
    {
159
        $this->parent = $parent;
160
    }
161
}