Completed
Pull Request — master (#8035)
by Vincent
06:19
created

ExprTest::testNOTInExprForEmptyArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 testProdExpr() : void
123
    {
124
        self::assertEquals('1 * 2', (string) $this->expr->prod(1, 2));
125
    }
126
127
    public function testDiffExpr() : void
128
    {
129
        self::assertEquals('1 - 2', (string) $this->expr->diff(1, 2));
130
    }
131
132
    public function testSumExpr() : void
133
    {
134
        self::assertEquals('1 + 2', (string) $this->expr->sum(1, 2));
135
    }
136
137
    public function testQuotientExpr() : void
138
    {
139
        self::assertEquals('10 / 2', (string) $this->expr->quot(10, 2));
140
    }
141
142
    public function testScopeInArithmeticExpr() : void
143
    {
144
        self::assertEquals('(100 - 20) / 2', (string) $this->expr->quot($this->expr->diff(100, 20), 2));
145
        self::assertEquals('100 - (20 / 2)', (string) $this->expr->diff(100, $this->expr->quot(20, 2)));
146
    }
147
148
    public function testSquareRootExpr() : void
149
    {
150
        self::assertEquals('SQRT(1)', (string) $this->expr->sqrt(1));
151
    }
152
153
    public function testEqualExpr() : void
154
    {
155
        self::assertEquals('1 = 1', (string) $this->expr->eq(1, 1));
156
    }
157
158
    public function testLikeExpr() : void
159
    {
160
        self::assertEquals('a.description LIKE :description', (string) $this->expr->like('a.description', ':description'));
161
    }
162
163
    public function testNotLikeExpr() : void
164
    {
165
        self::assertEquals('a.description NOT LIKE :description', (string) $this->expr->notLike('a.description', ':description'));
166
    }
167
168
    public function testConcatExpr() : void
169
    {
170
        self::assertEquals('CONCAT(u.first_name, u.last_name)', (string) $this->expr->concat('u.first_name', 'u.last_name'));
171
        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'));
172
    }
173
174
    public function testSubstringExpr() : void
175
    {
176
        self::assertEquals('SUBSTRING(a.title, 0, 25)', (string) $this->expr->substring('a.title', 0, 25));
177
    }
178
179
    /**
180
     * @group regression
181
     * @group DDC-612
182
     */
183
    public function testSubstringExprAcceptsTwoArguments() : void
184
    {
185
        self::assertEquals('SUBSTRING(a.title, 5)', (string) $this->expr->substring('a.title', 5));
186
    }
187
188
    public function testLowerExpr() : void
189
    {
190
        self::assertEquals('LOWER(u.first_name)', (string) $this->expr->lower('u.first_name'));
191
    }
192
193
    public function testUpperExpr() : void
194
    {
195
        self::assertEquals('UPPER(u.first_name)', (string) $this->expr->upper('u.first_name'));
196
    }
197
198
    public function testLengthExpr() : void
199
    {
200
        self::assertEquals('LENGTH(u.first_name)', (string) $this->expr->length('u.first_name'));
201
    }
202
203
    public function testGreaterThanExpr() : void
204
    {
205
        self::assertEquals('5 > 2', (string) $this->expr->gt(5, 2));
206
    }
207
208
    public function testLessThanExpr() : void
209
    {
210
        self::assertEquals('2 < 5', (string) $this->expr->lt(2, 5));
211
    }
212
213
    public function testStringLiteralExpr() : void
214
    {
215
        self::assertEquals("'word'", (string) $this->expr->literal('word'));
216
    }
217
218
    public function testNumericLiteralExpr() : void
219
    {
220
        self::assertEquals(5, (string) $this->expr->literal(5));
221
    }
222
223
    /**
224
     * @group regression
225
     * @group DDC-610
226
     */
227
    public function testLiteralExprProperlyQuotesStrings() : void
228
    {
229
        self::assertEquals("'00010001'", (string) $this->expr->literal('00010001'));
230
    }
231
232
    public function testGreaterThanOrEqualToExpr() : void
233
    {
234
        self::assertEquals('5 >= 2', (string) $this->expr->gte(5, 2));
235
    }
236
237
    public function testLessThanOrEqualTo() : void
238
    {
239
        self::assertEquals('2 <= 5', (string) $this->expr->lte(2, 5));
240
    }
241
242
    public function testBetweenExpr() : void
243
    {
244
        self::assertEquals('u.id BETWEEN 3 AND 6', (string) $this->expr->between('u.id', 3, 6));
245
    }
246
247
    public function testTrimExpr() : void
248
    {
249
        self::assertEquals('TRIM(u.id)', (string) $this->expr->trim('u.id'));
250
    }
251
252
    public function testIsNullExpr() : void
253
    {
254
        self::assertEquals('u.id IS NULL', (string) $this->expr->isNull('u.id'));
255
    }
256
257
    public function testIsNotNullExpr() : void
258
    {
259
        self::assertEquals('u.id IS NOT NULL', (string) $this->expr->isNotNull('u.id'));
260
    }
261
262
    public function testIsInstanceOfExpr() : void
263
    {
264
        self::assertEquals('u INSTANCE OF Doctrine\Tests\Models\Company\CompanyEmployee', (string) $this->expr->isInstanceOf('u', CompanyEmployee::class));
265
    }
266
267
    public function testIsMemberOfExpr() : void
268
    {
269
        self::assertEquals(':groupId MEMBER OF u.groups', (string) $this->expr->isMemberOf(':groupId', 'u.groups'));
270
    }
271
272
    public function testInExpr() : void
273
    {
274
        self::assertEquals('u.id IN(1, 2, 3)', (string) $this->expr->in('u.id', [1, 2, 3]));
275
    }
276
277
    public function testInLiteralExpr() : void
278
    {
279
        self::assertEquals("u.type IN('foo', 'bar')", (string) $this->expr->in('u.type', ['foo', 'bar']));
280
    }
281
282
    public function testInExprForEmptyArray() : void
283
    {
284
        self::assertEquals('u.id IN(FALSE)', (string) $this->expr->in('u.id', []));
285
    }
286
287
    public function testNotInExpr() : void
288
    {
289
        self::assertEquals('u.id NOT IN(1, 2, 3)', (string) $this->expr->notIn('u.id', [1, 2, 3]));
290
    }
291
292
    public function testNotInLiteralExpr() : void
293
    {
294
        self::assertEquals("u.type NOT IN('foo', 'bar')", (string) $this->expr->notIn('u.type', ['foo', 'bar']));
295
    }
296
297
    public function testNOTInExprForEmptyArray() : void
298
    {
299
        self::assertEquals('u.id NOT IN(FALSE)', (string) $this->expr->notIn('u.id', []));
300
    }
301
302
    public function testAndxOrxExpr() : void
303
    {
304
        $andExpr = $this->expr->andX();
305
        $andExpr->add($this->expr->eq(1, 1));
306
        $andExpr->add($this->expr->lt(1, 5));
307
308
        $orExpr = $this->expr->orX();
309
        $orExpr->add($andExpr);
310
        $orExpr->add($this->expr->eq(1, 1));
311
312
        self::assertEquals('(1 = 1 AND 1 < 5) OR 1 = 1', (string) $orExpr);
313
    }
314
315
    public function testOrxExpr() : void
316
    {
317
        $orExpr = $this->expr->orX();
318
        $orExpr->add($this->expr->eq(1, 1));
319
        $orExpr->add($this->expr->lt(1, 5));
320
321
        self::assertEquals('1 = 1 OR 1 < 5', (string) $orExpr);
322
    }
323
324
    public function testOrderByCountExpr() : void
325
    {
326
        $orderExpr = $this->expr->desc('u.username');
327
328
        self::assertEquals($orderExpr->count(), 1);
329
        self::assertEquals('u.username DESC', (string) $orderExpr);
330
    }
331
332
    public function testOrderByOrder() : void
333
    {
334
        $orderExpr = $this->expr->desc('u.username');
335
        self::assertEquals('u.username DESC', (string) $orderExpr);
336
    }
337
338
    public function testOrderByAsc() : void
339
    {
340
        $orderExpr = $this->expr->asc('u.username');
341
        self::assertEquals('u.username ASC', (string) $orderExpr);
342
    }
343
344
    /**
345
     * @expectedException InvalidArgumentException
346
     */
347
    public function testAddThrowsException() : void
348
    {
349
        $orExpr = $this->expr->orX();
350
        $orExpr->add($this->expr->quot(5, 2));
351
    }
352
353
    /**
354
     * @group DDC-1683
355
     */
356
    public function testBooleanLiteral() : void
357
    {
358
        self::assertEquals('true', $this->expr->literal(true));
359
        self::assertEquals('false', $this->expr->literal(false));
360
    }
361
362
    /**
363
     * @group DDC-1686
364
     */
365
    public function testExpressionGetter() : void
366
    {
367
        // Andx
368
        $andx = new Expr\Andx(['1 = 1', '2 = 2']);
369
        self::assertEquals(['1 = 1', '2 = 2'], $andx->getParts());
370
371
        // Comparison
372
        $comparison = new Expr\Comparison('foo', Expr\Comparison::EQ, 'bar');
373
        self::assertEquals('foo', $comparison->getLeftExpr());
374
        self::assertEquals('bar', $comparison->getRightExpr());
375
        self::assertEquals(Expr\Comparison::EQ, $comparison->getOperator());
376
377
        // From
378
        $from = new Expr\From('Foo', 'f', 'f.id');
379
        self::assertEquals('f', $from->getAlias());
380
        self::assertEquals('Foo', $from->getFrom());
381
        self::assertEquals('f.id', $from->getIndexBy());
382
383
        // Func
384
        $func = new Expr\Func('MAX', ['f.id']);
385
        self::assertEquals('MAX', $func->getName());
386
        self::assertEquals(['f.id'], $func->getArguments());
387
388
        // GroupBy
389
        $group = new Expr\GroupBy(['foo DESC', 'bar ASC']);
390
        self::assertEquals(['foo DESC', 'bar ASC'], $group->getParts());
391
392
        // Join
393
        $join = new Expr\Join(Expr\Join::INNER_JOIN, 'f.bar', 'b', Expr\Join::ON, 'b.bar_id = 1', 'b.bar_id');
394
        self::assertEquals(Expr\Join::INNER_JOIN, $join->getJoinType());
395
        self::assertEquals(Expr\Join::ON, $join->getConditionType());
396
        self::assertEquals('b.bar_id = 1', $join->getCondition());
397
        self::assertEquals('b.bar_id', $join->getIndexBy());
398
        self::assertEquals('f.bar', $join->getJoin());
399
        self::assertEquals('b', $join->getAlias());
400
401
        // Literal
402
        $literal = new Expr\Literal(['foo']);
403
        self::assertEquals(['foo'], $literal->getParts());
404
405
        // Math
406
        $math = new Expr\Math(10, '+', 20);
407
        self::assertEquals(10, $math->getLeftExpr());
408
        self::assertEquals(20, $math->getRightExpr());
409
        self::assertEquals('+', $math->getOperator());
410
411
        // OrderBy
412
        $order = new Expr\OrderBy('foo', 'DESC');
413
        self::assertEquals(['foo DESC'], $order->getParts());
414
415
        // Andx
416
        $orx = new Expr\Orx(['foo = 1', 'bar = 2']);
417
        self::assertEquals(['foo = 1', 'bar = 2'], $orx->getParts());
418
419
        // Select
420
        $select = new Expr\Select(['foo', 'bar']);
421
        self::assertEquals(['foo', 'bar'], $select->getParts());
422
    }
423
424
    public function testAddEmpty() : void
425
    {
426
        $andExpr = $this->expr->andX();
427
        $andExpr->add($this->expr->andX());
428
429
        self::assertEquals(0, $andExpr->count());
430
    }
431
432
    public function testAddNull() : void
433
    {
434
        $andExpr = $this->expr->andX();
435
        $andExpr->add(null);
436
437
        self::assertEquals(0, $andExpr->count());
438
    }
439
}
440