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.
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>.
20 namespace Doctrine\Tests\ORM\Query;
22 use Doctrine\ORM\Query\Expr;
23 use Doctrine\ORM\Query;
25 require_once __DIR__ . '/../../TestInit.php';
28 * Test case for the DQL Expr class used for generating DQL snippets through
29 * a programmatic interface
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
37 class ExprTest extends \Doctrine\Tests\OrmTestCase
41 protected function setUp()
43 $this->_em = $this->_getTestEntityManager();
44 $this->_expr = new Expr;
47 public function testAvgExpr()
49 $this->assertEquals('AVG(u.id)', (string) $this->_expr->avg('u.id'));
52 public function testMaxExpr()
54 $this->assertEquals('MAX(u.id)', (string) $this->_expr->max('u.id'));
57 public function testMinExpr()
59 $this->assertEquals('MIN(u.id)', (string) $this->_expr->min('u.id'));
62 public function testCountExpr()
64 $this->assertEquals('MAX(u.id)', (string) $this->_expr->max('u.id'));
67 public function testCountDistinctExpr()
69 $this->assertEquals('COUNT(DISTINCT u.id)', (string) $this->_expr->countDistinct('u.id'));
72 public function testExistsExpr()
74 $qb = $this->_em->createQueryBuilder();
75 $qb->select('u')->from('User', 'u')->where('u.name = ?1');
77 $this->assertEquals('EXISTS(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->exists($qb));
80 public function testAllExpr()
82 $qb = $this->_em->createQueryBuilder();
83 $qb->select('u')->from('User', 'u')->where('u.name = ?1');
85 $this->assertEquals('ALL(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->all($qb));
88 public function testSomeExpr()
90 $qb = $this->_em->createQueryBuilder();
91 $qb->select('u')->from('User', 'u')->where('u.name = ?1');
93 $this->assertEquals('SOME(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->some($qb));
96 public function testAnyExpr()
98 $qb = $this->_em->createQueryBuilder();
99 $qb->select('u')->from('User', 'u')->where('u.name = ?1');
101 $this->assertEquals('ANY(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->any($qb));
104 public function testNotExpr()
106 $qb = $this->_em->createQueryBuilder();
107 $qb->select('u')->from('User', 'u')->where('u.name = ?1');
109 $this->assertEquals('NOT(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->not($qb));
112 public function testAndExpr()
114 $this->assertEquals('1 = 1 AND 2 = 2', (string) $this->_expr->andx((string) $this->_expr->eq(1, 1), (string) $this->_expr->eq(2, 2)));
117 public function testIntelligentParenthesisPreventionAndExpr()
121 (string) $this->_expr->andx($this->_expr->orx($this->_expr->andx($this->_expr->eq(1, 1))), (string) $this->_expr->eq(2, 2))
125 public function testOrExpr()
127 $this->assertEquals('1 = 1 OR 2 = 2', (string) $this->_expr->orx((string) $this->_expr->eq(1, 1), (string) $this->_expr->eq(2, 2)));
130 public function testAbsExpr()
132 $this->assertEquals('ABS(1)', (string) $this->_expr->abs(1));
135 public function testProdExpr()
137 $this->assertEquals('1 * 2', (string) $this->_expr->prod(1, 2));
140 public function testDiffExpr()
142 $this->assertEquals('1 - 2', (string) $this->_expr->diff(1, 2));
145 public function testSumExpr()
147 $this->assertEquals('1 + 2', (string) $this->_expr->sum(1, 2));
150 public function testQuotientExpr()
152 $this->assertEquals('10 / 2', (string) $this->_expr->quot(10, 2));
155 public function testScopeInArithmeticExpr()
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)));
161 public function testSquareRootExpr()
163 $this->assertEquals('SQRT(1)', (string) $this->_expr->sqrt(1));
166 public function testEqualExpr()
168 $this->assertEquals('1 = 1', (string) $this->_expr->eq(1, 1));
171 public function testLikeExpr()
173 $this->assertEquals('a.description LIKE :description', (string) $this->_expr->like('a.description', ':description'));
176 public function testConcatExpr()
178 $this->assertEquals('CONCAT(u.first_name, u.last_name)', (string) $this->_expr->concat('u.first_name', 'u.last_name'));
181 public function testSubstringExpr()
183 $this->assertEquals('SUBSTRING(a.title, 0, 25)', (string) $this->_expr->substring('a.title', 0, 25));
190 public function testSubstringExprAcceptsTwoArguments()
192 $this->assertEquals('SUBSTRING(a.title, 5)', (string) $this->_expr->substring('a.title', 5));
195 public function testLowerExpr()
197 $this->assertEquals('LOWER(u.first_name)', (string) $this->_expr->lower('u.first_name'));
200 public function testUpperExpr()
202 $this->assertEquals('UPPER(u.first_name)', (string) $this->_expr->upper('u.first_name'));
205 public function testLengthExpr()
207 $this->assertEquals('LENGTH(u.first_name)', (string) $this->_expr->length('u.first_name'));
210 public function testGreaterThanExpr()
212 $this->assertEquals('5 > 2', (string) $this->_expr->gt(5, 2));
215 public function testLessThanExpr()
217 $this->assertEquals('2 < 5', (string) $this->_expr->lt(2, 5));
220 public function testStringLiteralExpr()
222 $this->assertEquals("'word'", (string) $this->_expr->literal('word'));
225 public function testNumericLiteralExpr()
227 $this->assertEquals(5, (string) $this->_expr->literal(5));
234 public function testLiteralExprProperlyQuotesStrings()
236 $this->assertEquals("'00010001'", (string) $this->_expr->literal('00010001'));
239 public function testGreaterThanOrEqualToExpr()
241 $this->assertEquals('5 >= 2', (string) $this->_expr->gte(5, 2));
244 public function testLessThanOrEqualTo()
246 $this->assertEquals('2 <= 5', (string) $this->_expr->lte(2, 5));
249 public function testBetweenExpr()
251 $this->assertEquals('u.id BETWEEN 3 AND 6', (string) $this->_expr->between('u.id', 3, 6));
254 public function testTrimExpr()
256 $this->assertEquals('TRIM(u.id)', (string) $this->_expr->trim('u.id'));
259 public function testIsNullExpr()
261 $this->assertEquals('u.id IS NULL', (string) $this->_expr->isNull('u.id'));
264 public function testIsNotNullExpr()
266 $this->assertEquals('u.id IS NOT NULL', (string) $this->_expr->isNotNull('u.id'));
269 public function testInExpr()
271 $this->assertEquals('u.id IN(1, 2, 3)', (string) $this->_expr->in('u.id', array(1, 2, 3)));
274 public function testInLiteralExpr()
276 $this->assertEquals("u.type IN('foo', 'bar')", (string) $this->_expr->in('u.type', array('foo', 'bar')));
279 public function testNotInExpr()
281 $this->assertEquals('u.id NOT IN(1, 2, 3)', (string) $this->_expr->notIn('u.id', array(1, 2, 3)));
284 public function testNotInLiteralExpr()
286 $this->assertEquals("u.type NOT IN('foo', 'bar')", (string) $this->_expr->notIn('u.type', array('foo', 'bar')));
289 public function testAndxOrxExpr()
291 $andExpr = $this->_expr->andx();
292 $andExpr->add($this->_expr->eq(1, 1));
293 $andExpr->add($this->_expr->lt(1, 5));
295 $orExpr = $this->_expr->orx();
296 $orExpr->add($andExpr);
297 $orExpr->add($this->_expr->eq(1, 1));
299 $this->assertEquals('(1 = 1 AND 1 < 5) OR 1 = 1', (string) $orExpr);
302 public function testOrxExpr()
304 $orExpr = $this->_expr->orx();
305 $orExpr->add($this->_expr->eq(1, 1));
306 $orExpr->add($this->_expr->lt(1, 5));
308 $this->assertEquals('1 = 1 OR 1 < 5', (string) $orExpr);
311 public function testOrderByCountExpr()
313 $orderExpr = $this->_expr->desc('u.username');
315 $this->assertEquals($orderExpr->count(), 1);
316 $this->assertEquals('u.username DESC', (string) $orderExpr);
319 public function testOrderByOrder()
321 $orderExpr = $this->_expr->desc('u.username');
322 $this->assertEquals('u.username DESC', (string) $orderExpr);
325 public function testOrderByAsc()
327 $orderExpr = $this->_expr->asc('u.username');
328 $this->assertEquals('u.username ASC', (string) $orderExpr);
332 * @expectedException \InvalidArgumentException
334 public function testAddThrowsException()
336 $orExpr = $this->_expr->orx();
337 $orExpr->add($this->_expr->quot(5, 2));
343 public function testBooleanLiteral()
345 $this->assertEquals('true', $this->_expr->literal(true));
346 $this->assertEquals('false', $this->_expr->literal(false));
353 public function testExpressionGetter()
357 $andx = new Expr\Andx(array('1 = 1', '2 = 2'));
358 $this->assertEquals(array('1 = 1', '2 = 2'), $andx->getParts());
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());
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());
373 $func = new Expr\Func('MAX', array('f.id'));
374 $this->assertEquals('MAX', $func->getName());
375 $this->assertEquals(array('f.id'), $func->getArguments());
378 $group = new Expr\GroupBy(array('foo DESC', 'bar ASC'));
379 $this->assertEquals(array('foo DESC', 'bar ASC'), $group->getParts());
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());
391 $literal = new Expr\Literal(array('foo'));
392 $this->assertEquals(array('foo'), $literal->getParts());
395 $math = new Expr\Math(10, '+', 20);
396 $this->assertEquals(10, $math->getLeftExpr());
397 $this->assertEquals(20, $math->getRightExpr());
398 $this->assertEquals('+', $math->getOperator());
401 $order = new Expr\OrderBy('foo', 'DESC');
402 $this->assertEquals(array('foo DESC'), $order->getParts());
405 $orx = new Expr\Orx(array('foo = 1', 'bar = 2'));
406 $this->assertEquals(array('foo = 1', 'bar = 2'), $orx->getParts());
409 $select = new Expr\Select(array('foo', 'bar'));
410 $this->assertEquals(array('foo', 'bar'), $select->getParts());