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