Passed
Pull Request — 2.6 (#7850)
by
unknown
07:46
created

testMatchingOverrulesCollectionOrdering()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 1
nc 1
nop 0
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
use function assert;
11
12
/**
13
 * @group GH7836
14
 */
15
class GH7836Test extends OrmFunctionalTestCase
16
{
17
    protected function setUp() : void
18
    {
19
        parent::setUp();
20
21
        $this->setUpEntitySchema([GH7836ParentEntity::class, GH7836ChildEntity::class]);
22
23
        $parent = new GH7836ParentEntity();
24
        $parent->addChild(100, 'foo');
25
        $parent->addChild(100, 'bar');
26
        $parent->addChild(200, 'baz');
27
28
        $this->_em->persist($parent);
29
        $this->_em->flush();
30
        $this->_em->clear();
31
    }
32
33
    public function testMatchingRespectsCollectionOrdering() : void
34
    {
35
        $parent = $this->_em->find(GH7836ParentEntity::class, 1);
36
        assert($parent instanceof GH7836ParentEntity);
37
38
        $children = $parent->getChildren()->matching(Criteria::create());
39
40
        self::assertSame(100, $children[0]->position);
41
        self::assertSame('bar', $children[0]->name);
42
        self::assertSame(100, $children[1]->position);
43
        self::assertSame('foo', $children[1]->name);
44
        self::assertSame(200, $children[2]->position);
45
        self::assertSame('baz', $children[2]->name);
46
    }
47
48
    public function testMatchingOverrulesCollectionOrdering() : void
49
    {
50
        $parent = $this->_em->find(GH7836ParentEntity::class, 1);
51
        assert($parent instanceof GH7836ParentEntity);
52
53
        $children = $parent->getChildren()->matching(Criteria::create()->orderBy(['position' => 'DESC', 'name' => 'ASC']));
54
55
        self::assertSame(200, $children[0]->position);
56
        self::assertSame('baz', $children[0]->name);
57
        self::assertSame(100, $children[1]->position);
58
        self::assertSame('bar', $children[1]->name);
59
        self::assertSame(100, $children[2]->position);
60
        self::assertSame('foo', $children[2]->name);
61
    }
62
63
    public function testMatchingKeepsOrderOfCriteriaOrderingKeys() : void
64
    {
65
        $parent = $this->_em->find(GH7836ParentEntity::class, 1);
66
        assert($parent instanceof GH7836ParentEntity);
67
68
        $children = $parent->getChildren()->matching(Criteria::create()->orderBy(['name' => 'ASC', 'position' => 'ASC']));
69
70
        self::assertSame(100, $children[0]->position);
71
        self::assertSame('bar', $children[0]->name);
72
        self::assertSame(200, $children[1]->position);
73
        self::assertSame('baz', $children[1]->name);
74
        self::assertSame(100, $children[2]->position);
75
        self::assertSame('foo', $children[2]->name);
76
    }
77
}
78
79
/**
80
 * @Entity
81
 */
82
class GH7836ParentEntity
83
{
84
    /**
85
     * @Id
86
     * @Column(type="integer")
87
     * @GeneratedValue
88
     */
89
    private $id;
0 ignored issues
show
introduced by
The private property $id is not used, and could be removed.
Loading history...
90
91
    /**
92
     * @OneToMany(targetEntity=GH7836ChildEntity::class, mappedBy="parent", fetch="EXTRA_LAZY", cascade={"persist"})
93
     * @OrderBy({"position" = "ASC", "name" = "ASC"})
94
     */
95
    private $children;
96
97
    public function addChild(int $position, string $name) : void
98
    {
99
        $this->children[] = new GH7836ChildEntity($this, $position, $name);
100
    }
101
102
    public function getChildren() : PersistentCollection
103
    {
104
        return $this->children;
105
    }
106
}
107
108
/**
109
 * @Entity
110
 */
111
class GH7836ChildEntity
112
{
113
    /**
114
     * @Id
115
     * @Column(type="integer")
116
     * @GeneratedValue
117
     */
118
    private $id;
119
120
    /** @Column(type="integer") */
121
    public $position;
122
123
    /** @Column(type="string") */
124
    public $name;
125
126
    /** @ManyToOne(targetEntity=GH7836ParentEntity::class, inversedBy="children") */
127
    private $parent;
128
129
    public function __construct(GH7836ParentEntity $parent, int $position, string $name)
130
    {
131
        $this->parent   = $parent;
132
        $this->position = $position;
133
        $this->name     = $name;
134
    }
135
}
136