Completed
Pull Request — master (#5580)
by Marco
10:58
created

ExprTest::testNotInLiteralExpr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

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