2009-07-09 04:18:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Query;
|
|
|
|
|
|
|
|
use Doctrine\ORM\Query\Expr;
|
2016-12-08 18:01:04 +01:00
|
|
|
use Doctrine\Tests\Models\Company\CompanyEmployee;
|
2016-05-11 01:55:12 +07:00
|
|
|
use Doctrine\Tests\OrmTestCase;
|
2009-07-09 04:18:58 +00:00
|
|
|
|
|
|
|
/**
|
2010-05-25 13:27:06 -05:00
|
|
|
* Test case for the DQL Expr class used for generating DQL snippets through
|
2009-07-09 04:18:58 +00:00
|
|
|
* a programmatic interface
|
|
|
|
*
|
|
|
|
* @author Jonathan H. Wage <jonwage@gmail.com>
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link http://www.phpdoctrine.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
2016-05-11 01:55:12 +07:00
|
|
|
class ExprTest extends OrmTestCase
|
2009-07-09 04:18:58 +00:00
|
|
|
{
|
|
|
|
private $_em;
|
|
|
|
|
2014-03-19 13:00:45 +01:00
|
|
|
/**
|
|
|
|
* @var Expr
|
|
|
|
*/
|
|
|
|
private $_expr;
|
|
|
|
|
2009-07-09 04:18:58 +00:00
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->_em = $this->_getTestEntityManager();
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->_expr = new Expr;
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAvgExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('AVG(u.id)', (string) $this->_expr->avg('u.id'));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMaxExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('MAX(u.id)', (string) $this->_expr->max('u.id'));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMinExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('MIN(u.id)', (string) $this->_expr->min('u.id'));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCountExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('MAX(u.id)', (string) $this->_expr->max('u.id'));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCountDistinctExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('COUNT(DISTINCT u.id)', (string) $this->_expr->countDistinct('u.id'));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
2016-12-07 23:33:41 +01:00
|
|
|
|
2015-07-13 10:45:17 +03:00
|
|
|
public function testCountDistinctExprMulti()
|
|
|
|
{
|
|
|
|
$this->assertEquals('COUNT(DISTINCT u.id, u.name)', (string) $this->_expr->countDistinct('u.id', 'u.name'));
|
|
|
|
}
|
2009-07-09 04:18:58 +00:00
|
|
|
|
|
|
|
public function testExistsExpr()
|
|
|
|
{
|
2009-08-14 22:50:36 +00:00
|
|
|
$qb = $this->_em->createQueryBuilder();
|
|
|
|
$qb->select('u')->from('User', 'u')->where('u.name = ?1');
|
2010-05-25 13:27:06 -05:00
|
|
|
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('EXISTS(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->exists($qb));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAllExpr()
|
|
|
|
{
|
2009-08-14 22:50:36 +00:00
|
|
|
$qb = $this->_em->createQueryBuilder();
|
|
|
|
$qb->select('u')->from('User', 'u')->where('u.name = ?1');
|
2010-05-25 13:27:06 -05:00
|
|
|
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('ALL(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->all($qb));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSomeExpr()
|
|
|
|
{
|
2009-08-14 22:50:36 +00:00
|
|
|
$qb = $this->_em->createQueryBuilder();
|
|
|
|
$qb->select('u')->from('User', 'u')->where('u.name = ?1');
|
2010-05-25 13:27:06 -05:00
|
|
|
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('SOME(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->some($qb));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAnyExpr()
|
|
|
|
{
|
2009-08-14 22:50:36 +00:00
|
|
|
$qb = $this->_em->createQueryBuilder();
|
|
|
|
$qb->select('u')->from('User', 'u')->where('u.name = ?1');
|
2010-05-25 13:27:06 -05:00
|
|
|
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('ANY(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->any($qb));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotExpr()
|
|
|
|
{
|
2009-08-14 22:50:36 +00:00
|
|
|
$qb = $this->_em->createQueryBuilder();
|
|
|
|
$qb->select('u')->from('User', 'u')->where('u.name = ?1');
|
2010-05-25 13:27:06 -05:00
|
|
|
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('NOT(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->not($qb));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAndExpr()
|
|
|
|
{
|
2016-05-11 02:41:26 +07:00
|
|
|
$this->assertEquals('1 = 1 AND 2 = 2', (string) $this->_expr->andX((string) $this->_expr->eq(1, 1), (string) $this->_expr->eq(2, 2)));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
2010-05-25 13:27:06 -05:00
|
|
|
|
2009-08-20 02:59:42 +00:00
|
|
|
public function testIntelligentParenthesisPreventionAndExpr()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2011-05-07 20:14:04 -03:00
|
|
|
'1 = 1 AND 2 = 2',
|
2016-05-11 02:41:26 +07:00
|
|
|
(string) $this->_expr->andX($this->_expr->orX($this->_expr->andX($this->_expr->eq(1, 1))), (string) $this->_expr->eq(2, 2))
|
2009-08-20 02:59:42 +00:00
|
|
|
);
|
|
|
|
}
|
2009-07-09 04:18:58 +00:00
|
|
|
|
|
|
|
public function testOrExpr()
|
|
|
|
{
|
2016-05-11 02:41:26 +07:00
|
|
|
$this->assertEquals('1 = 1 OR 2 = 2', (string) $this->_expr->orX((string) $this->_expr->eq(1, 1), (string) $this->_expr->eq(2, 2)));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAbsExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('ABS(1)', (string) $this->_expr->abs(1));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testProdExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('1 * 2', (string) $this->_expr->prod(1, 2));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDiffExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('1 - 2', (string) $this->_expr->diff(1, 2));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSumExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('1 + 2', (string) $this->_expr->sum(1, 2));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testQuotientExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('10 / 2', (string) $this->_expr->quot(10, 2));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
2010-05-25 13:27:06 -05:00
|
|
|
|
2009-08-14 21:03:27 +00:00
|
|
|
public function testScopeInArithmeticExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('(100 - 20) / 2', (string) $this->_expr->quot($this->_expr->diff(100, 20), 2));
|
|
|
|
$this->assertEquals('100 - (20 / 2)', (string) $this->_expr->diff(100, $this->_expr->quot(20, 2)));
|
2009-08-14 21:03:27 +00:00
|
|
|
}
|
2009-07-09 04:18:58 +00:00
|
|
|
|
|
|
|
public function testSquareRootExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('SQRT(1)', (string) $this->_expr->sqrt(1));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEqualExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('1 = 1', (string) $this->_expr->eq(1, 1));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLikeExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('a.description LIKE :description', (string) $this->_expr->like('a.description', ':description'));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
2012-10-25 12:58:19 +02:00
|
|
|
public function testNotLikeExpr()
|
|
|
|
{
|
|
|
|
$this->assertEquals('a.description NOT LIKE :description', (string) $this->_expr->notLike('a.description', ':description'));
|
|
|
|
}
|
|
|
|
|
2009-07-09 04:18:58 +00:00
|
|
|
public function testConcatExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('CONCAT(u.first_name, u.last_name)', (string) $this->_expr->concat('u.first_name', 'u.last_name'));
|
2015-04-28 10:30:23 +02:00
|
|
|
$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'));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
2010-03-01 20:17:08 +00:00
|
|
|
public function testSubstringExpr()
|
2009-07-09 04:18:58 +00:00
|
|
|
{
|
2010-03-01 20:17:08 +00:00
|
|
|
$this->assertEquals('SUBSTRING(a.title, 0, 25)', (string) $this->_expr->substring('a.title', 0, 25));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
2010-05-25 15:10:59 -05:00
|
|
|
/**
|
|
|
|
* @group regression
|
|
|
|
* @group DDC-612
|
|
|
|
*/
|
|
|
|
public function testSubstringExprAcceptsTwoArguments()
|
|
|
|
{
|
2010-05-25 18:22:42 -04:00
|
|
|
$this->assertEquals('SUBSTRING(a.title, 5)', (string) $this->_expr->substring('a.title', 5));
|
2010-05-25 15:10:59 -05:00
|
|
|
}
|
|
|
|
|
2009-07-09 04:18:58 +00:00
|
|
|
public function testLowerExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('LOWER(u.first_name)', (string) $this->_expr->lower('u.first_name'));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpperExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('UPPER(u.first_name)', (string) $this->_expr->upper('u.first_name'));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLengthExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('LENGTH(u.first_name)', (string) $this->_expr->length('u.first_name'));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGreaterThanExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('5 > 2', (string) $this->_expr->gt(5, 2));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLessThanExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('2 < 5', (string) $this->_expr->lt(2, 5));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testStringLiteralExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals("'word'", (string) $this->_expr->literal('word'));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNumericLiteralExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals(5, (string) $this->_expr->literal(5));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
2010-05-25 13:27:06 -05:00
|
|
|
/**
|
|
|
|
* @group regression
|
|
|
|
* @group DDC-610
|
|
|
|
*/
|
|
|
|
public function testLiteralExprProperlyQuotesStrings()
|
|
|
|
{
|
|
|
|
$this->assertEquals("'00010001'", (string) $this->_expr->literal('00010001'));
|
|
|
|
}
|
|
|
|
|
2009-07-09 04:18:58 +00:00
|
|
|
public function testGreaterThanOrEqualToExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('5 >= 2', (string) $this->_expr->gte(5, 2));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLessThanOrEqualTo()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('2 <= 5', (string) $this->_expr->lte(2, 5));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBetweenExpr()
|
|
|
|
{
|
2010-03-28 10:30:43 +00:00
|
|
|
$this->assertEquals('u.id BETWEEN 3 AND 6', (string) $this->_expr->between('u.id', 3, 6));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTrimExpr()
|
|
|
|
{
|
2009-09-08 22:19:03 +00:00
|
|
|
$this->assertEquals('TRIM(u.id)', (string) $this->_expr->trim('u.id'));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
|
|
|
|
2011-02-20 01:53:55 -03:00
|
|
|
public function testIsNullExpr()
|
|
|
|
{
|
|
|
|
$this->assertEquals('u.id IS NULL', (string) $this->_expr->isNull('u.id'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsNotNullExpr()
|
|
|
|
{
|
|
|
|
$this->assertEquals('u.id IS NOT NULL', (string) $this->_expr->isNotNull('u.id'));
|
|
|
|
}
|
|
|
|
|
2014-03-19 13:00:45 +01:00
|
|
|
public function testIsInstanceOfExpr() {
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertEquals('u INSTANCE OF Doctrine\Tests\Models\Company\CompanyEmployee', (string) $this->_expr->isInstanceOf('u', CompanyEmployee::class));
|
2014-03-19 13:00:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsMemberOfExpr() {
|
|
|
|
$this->assertEquals(':groupId MEMBER OF u.groups', (string) $this->_expr->isMemberOf(':groupId', 'u.groups'));
|
|
|
|
}
|
|
|
|
|
2009-07-09 04:18:58 +00:00
|
|
|
public function testInExpr()
|
|
|
|
{
|
2016-12-07 23:33:41 +01:00
|
|
|
$this->assertEquals('u.id IN(1, 2, 3)', (string) $this->_expr->in('u.id', [1, 2, 3]));
|
2009-07-09 04:18:58 +00:00
|
|
|
}
|
2009-07-09 21:56:34 +00:00
|
|
|
|
2010-07-01 21:42:38 +02:00
|
|
|
public function testInLiteralExpr()
|
|
|
|
{
|
2016-12-07 23:33:41 +01:00
|
|
|
$this->assertEquals("u.type IN('foo', 'bar')", (string) $this->_expr->in('u.type', ['foo', 'bar']));
|
2010-07-01 21:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotInExpr()
|
|
|
|
{
|
2016-12-07 23:33:41 +01:00
|
|
|
$this->assertEquals('u.id NOT IN(1, 2, 3)', (string) $this->_expr->notIn('u.id', [1, 2, 3]));
|
2010-07-01 21:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotInLiteralExpr()
|
|
|
|
{
|
2016-12-07 23:33:41 +01:00
|
|
|
$this->assertEquals("u.type NOT IN('foo', 'bar')", (string) $this->_expr->notIn('u.type', ['foo', 'bar']));
|
2010-07-01 21:42:38 +02:00
|
|
|
}
|
|
|
|
|
2009-07-10 14:02:06 +00:00
|
|
|
public function testAndxOrxExpr()
|
2009-07-09 21:56:34 +00:00
|
|
|
{
|
2016-05-11 02:41:26 +07:00
|
|
|
$andExpr = $this->_expr->andX();
|
2009-09-08 22:19:03 +00:00
|
|
|
$andExpr->add($this->_expr->eq(1, 1));
|
|
|
|
$andExpr->add($this->_expr->lt(1, 5));
|
2009-07-09 21:56:34 +00:00
|
|
|
|
2016-05-11 02:41:26 +07:00
|
|
|
$orExpr = $this->_expr->orX();
|
2009-07-10 14:02:06 +00:00
|
|
|
$orExpr->add($andExpr);
|
2009-09-08 22:19:03 +00:00
|
|
|
$orExpr->add($this->_expr->eq(1, 1));
|
2009-07-09 21:56:34 +00:00
|
|
|
|
2011-05-07 20:14:04 -03:00
|
|
|
$this->assertEquals('(1 = 1 AND 1 < 5) OR 1 = 1', (string) $orExpr);
|
2009-07-09 21:56:34 +00:00
|
|
|
}
|
|
|
|
|
2009-07-10 14:02:06 +00:00
|
|
|
public function testOrxExpr()
|
2009-07-09 21:56:34 +00:00
|
|
|
{
|
2016-05-11 02:41:26 +07:00
|
|
|
$orExpr = $this->_expr->orX();
|
2009-09-08 22:19:03 +00:00
|
|
|
$orExpr->add($this->_expr->eq(1, 1));
|
|
|
|
$orExpr->add($this->_expr->lt(1, 5));
|
2009-07-09 21:56:34 +00:00
|
|
|
|
2011-05-07 20:14:04 -03:00
|
|
|
$this->assertEquals('1 = 1 OR 1 < 5', (string) $orExpr);
|
2009-07-09 21:56:34 +00:00
|
|
|
}
|
2010-05-25 13:27:06 -05:00
|
|
|
|
2009-07-10 17:53:48 +00:00
|
|
|
public function testOrderByCountExpr()
|
|
|
|
{
|
2010-02-09 17:13:49 +00:00
|
|
|
$orderExpr = $this->_expr->desc('u.username');
|
2009-07-10 17:53:48 +00:00
|
|
|
|
2010-02-09 17:13:49 +00:00
|
|
|
$this->assertEquals($orderExpr->count(), 1);
|
|
|
|
$this->assertEquals('u.username DESC', (string) $orderExpr);
|
2009-07-10 17:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testOrderByOrder()
|
|
|
|
{
|
2010-02-09 17:13:49 +00:00
|
|
|
$orderExpr = $this->_expr->desc('u.username');
|
|
|
|
$this->assertEquals('u.username DESC', (string) $orderExpr);
|
2009-07-10 17:53:48 +00:00
|
|
|
}
|
|
|
|
|
2010-02-09 17:13:49 +00:00
|
|
|
public function testOrderByAsc()
|
2009-07-10 17:53:48 +00:00
|
|
|
{
|
2010-02-09 17:13:49 +00:00
|
|
|
$orderExpr = $this->_expr->asc('u.username');
|
|
|
|
$this->assertEquals('u.username ASC', (string) $orderExpr);
|
2009-07-10 17:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-09 17:13:49 +00:00
|
|
|
* @expectedException \InvalidArgumentException
|
2009-07-10 17:53:48 +00:00
|
|
|
*/
|
|
|
|
public function testAddThrowsException()
|
|
|
|
{
|
2016-05-11 02:41:26 +07:00
|
|
|
$orExpr = $this->_expr->orX();
|
2009-09-08 22:19:03 +00:00
|
|
|
$orExpr->add($this->_expr->quot(5, 2));
|
2009-07-09 21:56:34 +00:00
|
|
|
}
|
2012-03-14 20:49:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-1683
|
|
|
|
*/
|
|
|
|
public function testBooleanLiteral()
|
|
|
|
{
|
|
|
|
$this->assertEquals('true', $this->_expr->literal(true));
|
|
|
|
$this->assertEquals('false', $this->_expr->literal(false));
|
|
|
|
}
|
2012-03-25 00:30:58 -03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-1686
|
|
|
|
*/
|
|
|
|
public function testExpressionGetter()
|
|
|
|
{
|
|
|
|
|
|
|
|
// Andx
|
2016-12-07 23:33:41 +01:00
|
|
|
$andx = new Expr\Andx(['1 = 1', '2 = 2']);
|
|
|
|
$this->assertEquals(['1 = 1', '2 = 2'], $andx->getParts());
|
2012-03-25 00:30:58 -03:00
|
|
|
|
|
|
|
// Comparison
|
|
|
|
$comparison = new Expr\Comparison('foo', Expr\Comparison::EQ, 'bar');
|
|
|
|
$this->assertEquals('foo', $comparison->getLeftExpr());
|
|
|
|
$this->assertEquals('bar', $comparison->getRightExpr());
|
|
|
|
$this->assertEquals(Expr\Comparison::EQ, $comparison->getOperator());
|
|
|
|
|
|
|
|
// From
|
|
|
|
$from = new Expr\From('Foo', 'f', 'f.id');
|
|
|
|
$this->assertEquals('f', $from->getAlias());
|
|
|
|
$this->assertEquals('Foo', $from->getFrom());
|
|
|
|
$this->assertEquals('f.id', $from->getIndexBy());
|
|
|
|
|
|
|
|
// Func
|
2016-12-07 23:33:41 +01:00
|
|
|
$func = new Expr\Func('MAX', ['f.id']);
|
2012-03-25 00:30:58 -03:00
|
|
|
$this->assertEquals('MAX', $func->getName());
|
2016-12-07 23:33:41 +01:00
|
|
|
$this->assertEquals(['f.id'], $func->getArguments());
|
2012-03-25 00:30:58 -03:00
|
|
|
|
|
|
|
// GroupBy
|
2016-12-07 23:33:41 +01:00
|
|
|
$group = new Expr\GroupBy(['foo DESC', 'bar ASC']);
|
|
|
|
$this->assertEquals(['foo DESC', 'bar ASC'], $group->getParts());
|
2012-03-25 00:30:58 -03:00
|
|
|
|
|
|
|
// Join
|
|
|
|
$join = new Expr\Join(Expr\Join::INNER_JOIN, 'f.bar', 'b', Expr\Join::ON, 'b.bar_id = 1', 'b.bar_id');
|
|
|
|
$this->assertEquals(Expr\Join::INNER_JOIN, $join->getJoinType());
|
|
|
|
$this->assertEquals(Expr\Join::ON, $join->getConditionType());
|
|
|
|
$this->assertEquals('b.bar_id = 1', $join->getCondition());
|
|
|
|
$this->assertEquals('b.bar_id', $join->getIndexBy());
|
|
|
|
$this->assertEquals('f.bar', $join->getJoin());
|
|
|
|
$this->assertEquals('b', $join->getAlias());
|
|
|
|
|
|
|
|
// Literal
|
2016-12-07 23:33:41 +01:00
|
|
|
$literal = new Expr\Literal(['foo']);
|
|
|
|
$this->assertEquals(['foo'], $literal->getParts());
|
2012-03-25 00:30:58 -03:00
|
|
|
|
|
|
|
// Math
|
|
|
|
$math = new Expr\Math(10, '+', 20);
|
|
|
|
$this->assertEquals(10, $math->getLeftExpr());
|
|
|
|
$this->assertEquals(20, $math->getRightExpr());
|
|
|
|
$this->assertEquals('+', $math->getOperator());
|
|
|
|
|
|
|
|
// OrderBy
|
|
|
|
$order = new Expr\OrderBy('foo', 'DESC');
|
2016-12-07 23:33:41 +01:00
|
|
|
$this->assertEquals(['foo DESC'], $order->getParts());
|
2012-03-25 00:30:58 -03:00
|
|
|
|
|
|
|
// Andx
|
2016-12-07 23:33:41 +01:00
|
|
|
$orx = new Expr\Orx(['foo = 1', 'bar = 2']);
|
|
|
|
$this->assertEquals(['foo = 1', 'bar = 2'], $orx->getParts());
|
2012-03-25 00:30:58 -03:00
|
|
|
|
|
|
|
// Select
|
2016-12-07 23:33:41 +01:00
|
|
|
$select = new Expr\Select(['foo', 'bar']);
|
|
|
|
$this->assertEquals(['foo', 'bar'], $select->getParts());
|
2012-03-25 00:30:58 -03:00
|
|
|
}
|
2013-03-08 16:33:47 +01:00
|
|
|
|
2016-05-11 02:41:26 +07:00
|
|
|
public function testAddEmpty()
|
|
|
|
{
|
|
|
|
$andExpr = $this->_expr->andX();
|
|
|
|
$andExpr->add($this->_expr->andX());
|
2016-12-07 23:33:41 +01:00
|
|
|
|
2013-03-08 16:33:47 +01:00
|
|
|
$this->assertEquals(0, $andExpr->count());
|
|
|
|
}
|
|
|
|
|
2016-05-11 02:41:26 +07:00
|
|
|
public function testAddNull()
|
|
|
|
{
|
|
|
|
$andExpr = $this->_expr->andX();
|
2013-03-08 16:33:47 +01:00
|
|
|
$andExpr->add(null);
|
2016-12-07 23:33:41 +01:00
|
|
|
|
2013-03-08 16:33:47 +01:00
|
|
|
$this->assertEquals(0, $andExpr->count());
|
|
|
|
}
|
2012-03-14 20:49:25 +01:00
|
|
|
}
|