Failed Conditions
Pull Request — master (#6747)
by
unknown
11:55 queued 03:29
created

ExprTest::testAllExpr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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