1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Query; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Query\Expr; |
6
|
|
|
use Doctrine\Tests\Models\Company\CompanyEmployee; |
7
|
|
|
use Doctrine\Tests\OrmTestCase; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Test case for the DQL Expr class used for generating DQL snippets through |
11
|
|
|
* a programmatic interface |
12
|
|
|
* |
13
|
|
|
* @author Jonathan H. Wage <[email protected]> |
14
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
15
|
|
|
* @link http://www.phpdoctrine.org |
16
|
|
|
* @since 2.0 |
17
|
|
|
* @version $Revision$ |
18
|
|
|
*/ |
19
|
|
|
class ExprTest extends OrmTestCase |
20
|
|
|
{ |
21
|
|
|
private $_em; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Expr |
25
|
|
|
*/ |
26
|
|
|
private $_expr; |
27
|
|
|
|
28
|
|
|
protected function setUp() |
29
|
|
|
{ |
30
|
|
|
$this->_em = $this->_getTestEntityManager(); |
31
|
|
|
$this->_expr = new Expr; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testAvgExpr() |
35
|
|
|
{ |
36
|
|
|
$this->assertEquals('AVG(u.id)', (string) $this->_expr->avg('u.id')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testMaxExpr() |
40
|
|
|
{ |
41
|
|
|
$this->assertEquals('MAX(u.id)', (string) $this->_expr->max('u.id')); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testMinExpr() |
45
|
|
|
{ |
46
|
|
|
$this->assertEquals('MIN(u.id)', (string) $this->_expr->min('u.id')); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testCountExpr() |
50
|
|
|
{ |
51
|
|
|
$this->assertEquals('MAX(u.id)', (string) $this->_expr->max('u.id')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testCountDistinctExpr() |
55
|
|
|
{ |
56
|
|
|
$this->assertEquals('COUNT(DISTINCT u.id)', (string) $this->_expr->countDistinct('u.id')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testCountDistinctExprMulti() |
60
|
|
|
{ |
61
|
|
|
$this->assertEquals('COUNT(DISTINCT u.id, u.name)', (string) $this->_expr->countDistinct('u.id', 'u.name')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testExistsExpr() |
65
|
|
|
{ |
66
|
|
|
$qb = $this->_em->createQueryBuilder(); |
67
|
|
|
$qb->select('u')->from('User', 'u')->where('u.name = ?1'); |
68
|
|
|
|
69
|
|
|
$this->assertEquals('EXISTS(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->exists($qb)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testAllExpr() |
73
|
|
|
{ |
74
|
|
|
$qb = $this->_em->createQueryBuilder(); |
75
|
|
|
$qb->select('u')->from('User', 'u')->where('u.name = ?1'); |
76
|
|
|
|
77
|
|
|
$this->assertEquals('ALL(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->all($qb)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testSomeExpr() |
81
|
|
|
{ |
82
|
|
|
$qb = $this->_em->createQueryBuilder(); |
83
|
|
|
$qb->select('u')->from('User', 'u')->where('u.name = ?1'); |
84
|
|
|
|
85
|
|
|
$this->assertEquals('SOME(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->some($qb)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testAnyExpr() |
89
|
|
|
{ |
90
|
|
|
$qb = $this->_em->createQueryBuilder(); |
91
|
|
|
$qb->select('u')->from('User', 'u')->where('u.name = ?1'); |
92
|
|
|
|
93
|
|
|
$this->assertEquals('ANY(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->any($qb)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testNotExpr() |
97
|
|
|
{ |
98
|
|
|
$qb = $this->_em->createQueryBuilder(); |
99
|
|
|
$qb->select('u')->from('User', 'u')->where('u.name = ?1'); |
100
|
|
|
|
101
|
|
|
$this->assertEquals('NOT(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->not($qb)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testAndExpr() |
105
|
|
|
{ |
106
|
|
|
$this->assertEquals('1 = 1 AND 2 = 2', (string) $this->_expr->andX((string) $this->_expr->eq(1, 1), (string) $this->_expr->eq(2, 2))); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testIntelligentParenthesisPreventionAndExpr() |
110
|
|
|
{ |
111
|
|
|
$this->assertEquals( |
112
|
|
|
'1 = 1 AND 2 = 2', |
113
|
|
|
(string) $this->_expr->andX($this->_expr->orX($this->_expr->andX($this->_expr->eq(1, 1))), (string) $this->_expr->eq(2, 2)) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testOrExpr() |
118
|
|
|
{ |
119
|
|
|
$this->assertEquals('1 = 1 OR 2 = 2', (string) $this->_expr->orX((string) $this->_expr->eq(1, 1), (string) $this->_expr->eq(2, 2))); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testAbsExpr() |
123
|
|
|
{ |
124
|
|
|
$this->assertEquals('ABS(1)', (string) $this->_expr->abs(1)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testProdExpr() |
128
|
|
|
{ |
129
|
|
|
$this->assertEquals('1 * 2', (string) $this->_expr->prod(1, 2)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testDiffExpr() |
133
|
|
|
{ |
134
|
|
|
$this->assertEquals('1 - 2', (string) $this->_expr->diff(1, 2)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testSumExpr() |
138
|
|
|
{ |
139
|
|
|
$this->assertEquals('1 + 2', (string) $this->_expr->sum(1, 2)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testQuotientExpr() |
143
|
|
|
{ |
144
|
|
|
$this->assertEquals('10 / 2', (string) $this->_expr->quot(10, 2)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testScopeInArithmeticExpr() |
148
|
|
|
{ |
149
|
|
|
$this->assertEquals('(100 - 20) / 2', (string) $this->_expr->quot($this->_expr->diff(100, 20), 2)); |
150
|
|
|
$this->assertEquals('100 - (20 / 2)', (string) $this->_expr->diff(100, $this->_expr->quot(20, 2))); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testSquareRootExpr() |
154
|
|
|
{ |
155
|
|
|
$this->assertEquals('SQRT(1)', (string) $this->_expr->sqrt(1)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testEqualExpr() |
159
|
|
|
{ |
160
|
|
|
$this->assertEquals('1 = 1', (string) $this->_expr->eq(1, 1)); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testLikeExpr() |
164
|
|
|
{ |
165
|
|
|
$this->assertEquals('a.description LIKE :description', (string) $this->_expr->like('a.description', ':description')); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testNotLikeExpr() |
169
|
|
|
{ |
170
|
|
|
$this->assertEquals('a.description NOT LIKE :description', (string) $this->_expr->notLike('a.description', ':description')); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testConcatExpr() |
174
|
|
|
{ |
175
|
|
|
$this->assertEquals('CONCAT(u.first_name, u.last_name)', (string) $this->_expr->concat('u.first_name', 'u.last_name')); |
176
|
|
|
$this->assertEquals('CONCAT(u.first_name, u.middle_name, u.last_name)', (string) $this->_expr->concat('u.first_name', 'u.middle_name', 'u.last_name')); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testSubstringExpr() |
180
|
|
|
{ |
181
|
|
|
$this->assertEquals('SUBSTRING(a.title, 0, 25)', (string) $this->_expr->substring('a.title', 0, 25)); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @group regression |
186
|
|
|
* @group DDC-612 |
187
|
|
|
*/ |
188
|
|
|
public function testSubstringExprAcceptsTwoArguments() |
189
|
|
|
{ |
190
|
|
|
$this->assertEquals('SUBSTRING(a.title, 5)', (string) $this->_expr->substring('a.title', 5)); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testLowerExpr() |
194
|
|
|
{ |
195
|
|
|
$this->assertEquals('LOWER(u.first_name)', (string) $this->_expr->lower('u.first_name')); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function testUpperExpr() |
199
|
|
|
{ |
200
|
|
|
$this->assertEquals('UPPER(u.first_name)', (string) $this->_expr->upper('u.first_name')); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function testLengthExpr() |
204
|
|
|
{ |
205
|
|
|
$this->assertEquals('LENGTH(u.first_name)', (string) $this->_expr->length('u.first_name')); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function testGreaterThanExpr() |
209
|
|
|
{ |
210
|
|
|
$this->assertEquals('5 > 2', (string) $this->_expr->gt(5, 2)); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function testLessThanExpr() |
214
|
|
|
{ |
215
|
|
|
$this->assertEquals('2 < 5', (string) $this->_expr->lt(2, 5)); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testStringLiteralExpr() |
219
|
|
|
{ |
220
|
|
|
$this->assertEquals("'word'", (string) $this->_expr->literal('word')); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function testNumericLiteralExpr() |
224
|
|
|
{ |
225
|
|
|
$this->assertEquals(5, (string) $this->_expr->literal(5)); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @group regression |
230
|
|
|
* @group DDC-610 |
231
|
|
|
*/ |
232
|
|
|
public function testLiteralExprProperlyQuotesStrings() |
233
|
|
|
{ |
234
|
|
|
$this->assertEquals("'00010001'", (string) $this->_expr->literal('00010001')); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function testGreaterThanOrEqualToExpr() |
238
|
|
|
{ |
239
|
|
|
$this->assertEquals('5 >= 2', (string) $this->_expr->gte(5, 2)); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function testLessThanOrEqualTo() |
243
|
|
|
{ |
244
|
|
|
$this->assertEquals('2 <= 5', (string) $this->_expr->lte(2, 5)); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function testBetweenExpr() |
248
|
|
|
{ |
249
|
|
|
$this->assertEquals('u.id BETWEEN 3 AND 6', (string) $this->_expr->between('u.id', 3, 6)); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function testTrimExpr() |
253
|
|
|
{ |
254
|
|
|
$this->assertEquals('TRIM(u.id)', (string) $this->_expr->trim('u.id')); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public function testIsNullExpr() |
258
|
|
|
{ |
259
|
|
|
$this->assertEquals('u.id IS NULL', (string) $this->_expr->isNull('u.id')); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public function testIsNotNullExpr() |
263
|
|
|
{ |
264
|
|
|
$this->assertEquals('u.id IS NOT NULL', (string) $this->_expr->isNotNull('u.id')); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function testIsInstanceOfExpr() { |
268
|
|
|
$this->assertEquals('u INSTANCE OF Doctrine\Tests\Models\Company\CompanyEmployee', (string) $this->_expr->isInstanceOf('u', CompanyEmployee::class)); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function testIsMemberOfExpr() { |
272
|
|
|
$this->assertEquals(':groupId MEMBER OF u.groups', (string) $this->_expr->isMemberOf(':groupId', 'u.groups')); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function testInExpr() |
276
|
|
|
{ |
277
|
|
|
$this->assertEquals('u.id IN(1, 2, 3)', (string) $this->_expr->in('u.id', [1, 2, 3])); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function testInLiteralExpr() |
281
|
|
|
{ |
282
|
|
|
$this->assertEquals("u.type IN('foo', 'bar')", (string) $this->_expr->in('u.type', ['foo', 'bar'])); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function testInExprForEmptyArray() |
286
|
|
|
{ |
287
|
|
|
self::assertEquals('u.id IN(NULL)', (string) $this->_expr->in('u.id', [])); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
public function testNotInExpr() |
291
|
|
|
{ |
292
|
|
|
$this->assertEquals('u.id NOT IN(1, 2, 3)', (string) $this->_expr->notIn('u.id', [1, 2, 3])); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function testNotInLiteralExpr() |
296
|
|
|
{ |
297
|
|
|
$this->assertEquals("u.type NOT IN('foo', 'bar')", (string) $this->_expr->notIn('u.type', ['foo', 'bar'])); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
public function testNotInExprForEmptyArray() |
301
|
|
|
{ |
302
|
|
|
self::assertEquals('u.id NOT IN(NULL)', (string) $this->_expr->notIn('u.id', [])); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
public function testAndxOrxExpr() |
306
|
|
|
{ |
307
|
|
|
$andExpr = $this->_expr->andX(); |
308
|
|
|
$andExpr->add($this->_expr->eq(1, 1)); |
309
|
|
|
$andExpr->add($this->_expr->lt(1, 5)); |
310
|
|
|
|
311
|
|
|
$orExpr = $this->_expr->orX(); |
312
|
|
|
$orExpr->add($andExpr); |
313
|
|
|
$orExpr->add($this->_expr->eq(1, 1)); |
314
|
|
|
|
315
|
|
|
$this->assertEquals('(1 = 1 AND 1 < 5) OR 1 = 1', (string) $orExpr); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
public function testOrxExpr() |
319
|
|
|
{ |
320
|
|
|
$orExpr = $this->_expr->orX(); |
321
|
|
|
$orExpr->add($this->_expr->eq(1, 1)); |
322
|
|
|
$orExpr->add($this->_expr->lt(1, 5)); |
323
|
|
|
|
324
|
|
|
$this->assertEquals('1 = 1 OR 1 < 5', (string) $orExpr); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
public function testOrderByCountExpr() |
328
|
|
|
{ |
329
|
|
|
$orderExpr = $this->_expr->desc('u.username'); |
330
|
|
|
|
331
|
|
|
$this->assertEquals($orderExpr->count(), 1); |
332
|
|
|
$this->assertEquals('u.username DESC', (string) $orderExpr); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function testOrderByOrder() |
336
|
|
|
{ |
337
|
|
|
$orderExpr = $this->_expr->desc('u.username'); |
338
|
|
|
$this->assertEquals('u.username DESC', (string) $orderExpr); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
public function testOrderByAsc() |
342
|
|
|
{ |
343
|
|
|
$orderExpr = $this->_expr->asc('u.username'); |
344
|
|
|
$this->assertEquals('u.username ASC', (string) $orderExpr); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @expectedException \InvalidArgumentException |
349
|
|
|
*/ |
350
|
|
|
public function testAddThrowsException() |
351
|
|
|
{ |
352
|
|
|
$orExpr = $this->_expr->orX(); |
353
|
|
|
$orExpr->add($this->_expr->quot(5, 2)); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @group DDC-1683 |
358
|
|
|
*/ |
359
|
|
|
public function testBooleanLiteral() |
360
|
|
|
{ |
361
|
|
|
$this->assertEquals('true', $this->_expr->literal(true)); |
362
|
|
|
$this->assertEquals('false', $this->_expr->literal(false)); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @group DDC-1686 |
368
|
|
|
*/ |
369
|
|
|
public function testExpressionGetter() |
370
|
|
|
{ |
371
|
|
|
|
372
|
|
|
// Andx |
373
|
|
|
$andx = new Expr\Andx(['1 = 1', '2 = 2']); |
374
|
|
|
$this->assertEquals(['1 = 1', '2 = 2'], $andx->getParts()); |
375
|
|
|
|
376
|
|
|
// Comparison |
377
|
|
|
$comparison = new Expr\Comparison('foo', Expr\Comparison::EQ, 'bar'); |
378
|
|
|
$this->assertEquals('foo', $comparison->getLeftExpr()); |
379
|
|
|
$this->assertEquals('bar', $comparison->getRightExpr()); |
380
|
|
|
$this->assertEquals(Expr\Comparison::EQ, $comparison->getOperator()); |
381
|
|
|
|
382
|
|
|
// From |
383
|
|
|
$from = new Expr\From('Foo', 'f', 'f.id'); |
384
|
|
|
$this->assertEquals('f', $from->getAlias()); |
385
|
|
|
$this->assertEquals('Foo', $from->getFrom()); |
386
|
|
|
$this->assertEquals('f.id', $from->getIndexBy()); |
387
|
|
|
|
388
|
|
|
// Func |
389
|
|
|
$func = new Expr\Func('MAX', ['f.id']); |
390
|
|
|
$this->assertEquals('MAX', $func->getName()); |
391
|
|
|
$this->assertEquals(['f.id'], $func->getArguments()); |
392
|
|
|
|
393
|
|
|
// GroupBy |
394
|
|
|
$group = new Expr\GroupBy(['foo DESC', 'bar ASC']); |
395
|
|
|
$this->assertEquals(['foo DESC', 'bar ASC'], $group->getParts()); |
396
|
|
|
|
397
|
|
|
// Join |
398
|
|
|
$join = new Expr\Join(Expr\Join::INNER_JOIN, 'f.bar', 'b', Expr\Join::ON, 'b.bar_id = 1', 'b.bar_id'); |
399
|
|
|
$this->assertEquals(Expr\Join::INNER_JOIN, $join->getJoinType()); |
400
|
|
|
$this->assertEquals(Expr\Join::ON, $join->getConditionType()); |
401
|
|
|
$this->assertEquals('b.bar_id = 1', $join->getCondition()); |
402
|
|
|
$this->assertEquals('b.bar_id', $join->getIndexBy()); |
403
|
|
|
$this->assertEquals('f.bar', $join->getJoin()); |
404
|
|
|
$this->assertEquals('b', $join->getAlias()); |
405
|
|
|
|
406
|
|
|
// Literal |
407
|
|
|
$literal = new Expr\Literal(['foo']); |
408
|
|
|
$this->assertEquals(['foo'], $literal->getParts()); |
409
|
|
|
|
410
|
|
|
// Math |
411
|
|
|
$math = new Expr\Math(10, '+', 20); |
412
|
|
|
$this->assertEquals(10, $math->getLeftExpr()); |
413
|
|
|
$this->assertEquals(20, $math->getRightExpr()); |
414
|
|
|
$this->assertEquals('+', $math->getOperator()); |
415
|
|
|
|
416
|
|
|
// OrderBy |
417
|
|
|
$order = new Expr\OrderBy('foo', 'DESC'); |
418
|
|
|
$this->assertEquals(['foo DESC'], $order->getParts()); |
419
|
|
|
|
420
|
|
|
// Andx |
421
|
|
|
$orx = new Expr\Orx(['foo = 1', 'bar = 2']); |
422
|
|
|
$this->assertEquals(['foo = 1', 'bar = 2'], $orx->getParts()); |
423
|
|
|
|
424
|
|
|
// Select |
425
|
|
|
$select = new Expr\Select(['foo', 'bar']); |
426
|
|
|
$this->assertEquals(['foo', 'bar'], $select->getParts()); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
public function testAddEmpty() |
430
|
|
|
{ |
431
|
|
|
$andExpr = $this->_expr->andX(); |
432
|
|
|
$andExpr->add($this->_expr->andX()); |
433
|
|
|
|
434
|
|
|
$this->assertEquals(0, $andExpr->count()); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
public function testAddNull() |
438
|
|
|
{ |
439
|
|
|
$andExpr = $this->_expr->andX(); |
440
|
|
|
$andExpr->add(null); |
441
|
|
|
|
442
|
|
|
$this->assertEquals(0, $andExpr->count()); |
443
|
|
|
} |
444
|
|
|
} |
445
|
|
|
|