1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Functional; |
6
|
|
|
|
7
|
|
|
use Doctrine\Tests\Models\Company\CompanyEmployee; |
8
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Functional tests for ordering with arithmetic expression. |
12
|
|
|
* |
13
|
|
|
* @group GH8011 |
14
|
|
|
*/ |
15
|
|
|
class GH8011Test extends OrmFunctionalTestCase |
16
|
|
|
{ |
17
|
|
|
protected function setUp() |
18
|
|
|
{ |
19
|
|
|
$this->useModelSet('company'); |
20
|
|
|
parent::setUp(); |
21
|
|
|
|
22
|
|
|
$this->generateFixture(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testOrderWithArithmeticExpressionWithSingleValuedPathExpression() |
26
|
|
|
{ |
27
|
|
|
$dql = 'SELECT p ' . |
28
|
|
|
'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' . |
29
|
|
|
'ORDER BY p.id + p.id ASC'; |
30
|
|
|
|
31
|
|
|
/** @var CompanyEmployee[] $result */ |
32
|
|
|
$result = $this->_em->createQuery($dql)->getResult(); |
33
|
|
|
|
34
|
|
|
$this->assertEquals(2, count($result)); |
35
|
|
|
$this->assertEquals('Benjamin E.', $result[0]->getName()); |
36
|
|
|
$this->assertEquals('Guilherme B.', $result[1]->getName()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testOrderWithArithmeticExpressionWithLiteralAndSingleValuedPathExpression() |
40
|
|
|
{ |
41
|
|
|
$dql = 'SELECT p ' . |
42
|
|
|
'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' . |
43
|
|
|
'ORDER BY 1 + p.id ASC'; |
44
|
|
|
|
45
|
|
|
/** @var CompanyEmployee[] $result */ |
46
|
|
|
$result = $this->_em->createQuery($dql)->getResult(); |
47
|
|
|
|
48
|
|
|
$this->assertEquals(2, count($result)); |
49
|
|
|
$this->assertEquals('Benjamin E.', $result[0]->getName()); |
50
|
|
|
$this->assertEquals('Guilherme B.', $result[1]->getName()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testOrderWithArithmeticExpressionWithLiteralAndSingleValuedPathExpression2() |
54
|
|
|
{ |
55
|
|
|
$dql = 'SELECT p ' . |
56
|
|
|
'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' . |
57
|
|
|
'ORDER BY ((1 + p.id)) ASC'; |
58
|
|
|
|
59
|
|
|
/** @var CompanyEmployee[] $result */ |
60
|
|
|
$result = $this->_em->createQuery($dql)->getResult(); |
61
|
|
|
|
62
|
|
|
$this->assertEquals(2, count($result)); |
63
|
|
|
$this->assertEquals('Benjamin E.', $result[0]->getName()); |
64
|
|
|
$this->assertEquals('Guilherme B.', $result[1]->getName()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testOrderWithArithmeticExpressionWithSingleValuedPathExpressionAndLiteral() |
68
|
|
|
{ |
69
|
|
|
$dql = 'SELECT p ' . |
70
|
|
|
'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' . |
71
|
|
|
'ORDER BY p.id + 1 ASC'; |
72
|
|
|
|
73
|
|
|
/** @var CompanyEmployee[] $result */ |
74
|
|
|
$result = $this->_em->createQuery($dql)->getResult(); |
75
|
|
|
|
76
|
|
|
$this->assertEquals(2, count($result)); |
77
|
|
|
$this->assertEquals('Benjamin E.', $result[0]->getName()); |
78
|
|
|
$this->assertEquals('Guilherme B.', $result[1]->getName()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testOrderWithArithmeticExpressionWithResultVariableAndLiteral() |
82
|
|
|
{ |
83
|
|
|
$dql = 'SELECT p, p.salary AS HIDDEN s ' . |
84
|
|
|
'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' . |
85
|
|
|
'ORDER BY s + 1 DESC'; |
86
|
|
|
|
87
|
|
|
/** @var CompanyEmployee[] $result */ |
88
|
|
|
$result = $this->_em->createQuery($dql)->getResult(); |
89
|
|
|
|
90
|
|
|
$this->assertEquals(2, count($result)); |
91
|
|
|
$this->assertEquals('Guilherme B.', $result[0]->getName()); |
92
|
|
|
$this->assertEquals('Benjamin E.', $result[1]->getName()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testOrderWithArithmeticExpressionWithResultVariableAndLiteral2() |
96
|
|
|
{ |
97
|
|
|
$dql = 'SELECT p, p.salary AS HIDDEN s ' . |
98
|
|
|
'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' . |
99
|
|
|
'ORDER BY ((s + 1)) DESC'; |
100
|
|
|
|
101
|
|
|
/** @var CompanyEmployee[] $result */ |
102
|
|
|
$result = $this->_em->createQuery($dql)->getResult(); |
103
|
|
|
|
104
|
|
|
$this->assertEquals(2, count($result)); |
105
|
|
|
$this->assertEquals('Guilherme B.', $result[0]->getName()); |
106
|
|
|
$this->assertEquals('Benjamin E.', $result[1]->getName()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testOrderWithArithmeticExpressionWithLiteralAndResultVariable() |
110
|
|
|
{ |
111
|
|
|
$dql = 'SELECT p, p.salary AS HIDDEN s ' . |
112
|
|
|
'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' . |
113
|
|
|
'ORDER BY 1 + s DESC'; |
114
|
|
|
|
115
|
|
|
/** @var CompanyEmployee[] $result */ |
116
|
|
|
$result = $this->_em->createQuery($dql)->getResult(); |
117
|
|
|
|
118
|
|
|
$this->assertEquals(2, count($result)); |
119
|
|
|
$this->assertEquals('Guilherme B.', $result[0]->getName()); |
120
|
|
|
$this->assertEquals('Benjamin E.', $result[1]->getName()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testOrderWithArithmeticExpressionWithLiteralAndResultVariable2() |
124
|
|
|
{ |
125
|
|
|
$dql = 'SELECT p, p.salary AS HIDDEN s ' . |
126
|
|
|
'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' . |
127
|
|
|
'ORDER BY ((1 + s)) DESC'; |
128
|
|
|
|
129
|
|
|
/** @var CompanyEmployee[] $result */ |
130
|
|
|
$result = $this->_em->createQuery($dql)->getResult(); |
131
|
|
|
|
132
|
|
|
$this->assertEquals(2, count($result)); |
133
|
|
|
$this->assertEquals('Guilherme B.', $result[0]->getName()); |
134
|
|
|
$this->assertEquals('Benjamin E.', $result[1]->getName()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testOrderWithArithmeticExpressionWithResultVariableAndSingleValuedPathExpression() |
138
|
|
|
{ |
139
|
|
|
$dql = 'SELECT p, p.salary AS HIDDEN s ' . |
140
|
|
|
'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' . |
141
|
|
|
'ORDER BY s + p.id DESC'; |
142
|
|
|
|
143
|
|
|
/** @var CompanyEmployee[] $result */ |
144
|
|
|
$result = $this->_em->createQuery($dql)->getResult(); |
145
|
|
|
|
146
|
|
|
$this->assertEquals(2, count($result)); |
147
|
|
|
$this->assertEquals('Guilherme B.', $result[0]->getName()); |
148
|
|
|
$this->assertEquals('Benjamin E.', $result[1]->getName()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testOrderWithArithmeticExpressionWithResultVariableAndSingleValuedPathExpression2() |
152
|
|
|
{ |
153
|
|
|
$dql = 'SELECT p, p.salary AS HIDDEN s ' . |
154
|
|
|
'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' . |
155
|
|
|
'ORDER BY ((s + p.id)) DESC'; |
156
|
|
|
|
157
|
|
|
/** @var CompanyEmployee[] $result */ |
158
|
|
|
$result = $this->_em->createQuery($dql)->getResult(); |
159
|
|
|
|
160
|
|
|
$this->assertEquals(2, count($result)); |
161
|
|
|
$this->assertEquals('Guilherme B.', $result[0]->getName()); |
162
|
|
|
$this->assertEquals('Benjamin E.', $result[1]->getName()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testOrderWithArithmeticExpressionWithSingleValuedPathExpressionAndResultVariable() |
166
|
|
|
{ |
167
|
|
|
$dql = 'SELECT p, p.salary AS HIDDEN s ' . |
168
|
|
|
'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' . |
169
|
|
|
'ORDER BY p.id + s DESC'; |
170
|
|
|
|
171
|
|
|
/** @var CompanyEmployee[] $result */ |
172
|
|
|
$result = $this->_em->createQuery($dql)->getResult(); |
173
|
|
|
|
174
|
|
|
$this->assertEquals(2, count($result)); |
175
|
|
|
$this->assertEquals('Guilherme B.', $result[0]->getName()); |
176
|
|
|
$this->assertEquals('Benjamin E.', $result[1]->getName()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function generateFixture() |
180
|
|
|
{ |
181
|
|
|
$person1 = new CompanyEmployee(); |
182
|
|
|
$person1->setName('Benjamin E.'); |
183
|
|
|
$person1->setDepartment('IT'); |
184
|
|
|
$person1->setSalary(200000); |
185
|
|
|
|
186
|
|
|
$person2 = new CompanyEmployee(); |
187
|
|
|
$person2->setName('Guilherme B.'); |
188
|
|
|
$person2->setDepartment('IT2'); |
189
|
|
|
$person2->setSalary(400000); |
190
|
|
|
|
191
|
|
|
$this->_em->persist($person1); |
192
|
|
|
$this->_em->persist($person2); |
193
|
|
|
$this->_em->flush(); |
194
|
|
|
$this->_em->clear(); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|