2009-07-09 08:18:58 +04:00
< ? php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT
* LIMITED TO , THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT , INCIDENTAL ,
* SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT
* LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
* DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
* ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL . For more information , see
* < http :// www . doctrine - project . org >.
*/
namespace Doctrine\Tests\ORM\Query ;
use Doctrine\ORM\Query\Expr ;
use Doctrine\ORM\Query ;
require_once __DIR__ . '/../../TestInit.php' ;
/**
2010-05-25 22:27:06 +04:00
* Test case for the DQL Expr class used for generating DQL snippets through
2009-07-09 08:18:58 +04: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 $
*/
class ExprTest extends \Doctrine\Tests\OrmTestCase
{
private $_em ;
2014-03-19 16:00:45 +04:00
/**
* @ var Expr
*/
private $_expr ;
2009-07-09 08:18:58 +04:00
protected function setUp ()
{
$this -> _em = $this -> _getTestEntityManager ();
2009-09-09 02:19:03 +04:00
$this -> _expr = new Expr ;
2009-07-09 08:18:58 +04:00
}
public function testAvgExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'AVG(u.id)' , ( string ) $this -> _expr -> avg ( 'u.id' ));
2009-07-09 08:18:58 +04:00
}
public function testMaxExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'MAX(u.id)' , ( string ) $this -> _expr -> max ( 'u.id' ));
2009-07-09 08:18:58 +04:00
}
public function testMinExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'MIN(u.id)' , ( string ) $this -> _expr -> min ( 'u.id' ));
2009-07-09 08:18:58 +04:00
}
public function testCountExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'MAX(u.id)' , ( string ) $this -> _expr -> max ( 'u.id' ));
2009-07-09 08:18:58 +04:00
}
public function testCountDistinctExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'COUNT(DISTINCT u.id)' , ( string ) $this -> _expr -> countDistinct ( 'u.id' ));
2009-07-09 08:18:58 +04:00
}
public function testExistsExpr ()
{
2009-08-15 02:50:36 +04:00
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' ) -> from ( 'User' , 'u' ) -> where ( 'u.name = ?1' );
2010-05-25 22:27:06 +04:00
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'EXISTS(SELECT u FROM User u WHERE u.name = ?1)' , ( string ) $this -> _expr -> exists ( $qb ));
2009-07-09 08:18:58 +04:00
}
public function testAllExpr ()
{
2009-08-15 02:50:36 +04:00
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' ) -> from ( 'User' , 'u' ) -> where ( 'u.name = ?1' );
2010-05-25 22:27:06 +04:00
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'ALL(SELECT u FROM User u WHERE u.name = ?1)' , ( string ) $this -> _expr -> all ( $qb ));
2009-07-09 08:18:58 +04:00
}
public function testSomeExpr ()
{
2009-08-15 02:50:36 +04:00
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' ) -> from ( 'User' , 'u' ) -> where ( 'u.name = ?1' );
2010-05-25 22:27:06 +04:00
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'SOME(SELECT u FROM User u WHERE u.name = ?1)' , ( string ) $this -> _expr -> some ( $qb ));
2009-07-09 08:18:58 +04:00
}
public function testAnyExpr ()
{
2009-08-15 02:50:36 +04:00
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' ) -> from ( 'User' , 'u' ) -> where ( 'u.name = ?1' );
2010-05-25 22:27:06 +04:00
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'ANY(SELECT u FROM User u WHERE u.name = ?1)' , ( string ) $this -> _expr -> any ( $qb ));
2009-07-09 08:18:58 +04:00
}
public function testNotExpr ()
{
2009-08-15 02:50:36 +04:00
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' ) -> from ( 'User' , 'u' ) -> where ( 'u.name = ?1' );
2010-05-25 22:27:06 +04:00
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'NOT(SELECT u FROM User u WHERE u.name = ?1)' , ( string ) $this -> _expr -> not ( $qb ));
2009-07-09 08:18:58 +04:00
}
public function testAndExpr ()
{
2011-05-08 03:14:04 +04: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 08:18:58 +04:00
}
2010-05-25 22:27:06 +04:00
2009-08-20 06:59:42 +04:00
public function testIntelligentParenthesisPreventionAndExpr ()
{
$this -> assertEquals (
2011-05-08 03:14:04 +04:00
'1 = 1 AND 2 = 2' ,
2009-09-09 02:19:03 +04: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 06:59:42 +04:00
);
}
2009-07-09 08:18:58 +04:00
public function testOrExpr ()
{
2011-05-08 03:14:04 +04: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 08:18:58 +04:00
}
public function testAbsExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'ABS(1)' , ( string ) $this -> _expr -> abs ( 1 ));
2009-07-09 08:18:58 +04:00
}
public function testProdExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( '1 * 2' , ( string ) $this -> _expr -> prod ( 1 , 2 ));
2009-07-09 08:18:58 +04:00
}
public function testDiffExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( '1 - 2' , ( string ) $this -> _expr -> diff ( 1 , 2 ));
2009-07-09 08:18:58 +04:00
}
public function testSumExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( '1 + 2' , ( string ) $this -> _expr -> sum ( 1 , 2 ));
2009-07-09 08:18:58 +04:00
}
public function testQuotientExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( '10 / 2' , ( string ) $this -> _expr -> quot ( 10 , 2 ));
2009-07-09 08:18:58 +04:00
}
2010-05-25 22:27:06 +04:00
2009-08-15 01:03:27 +04:00
public function testScopeInArithmeticExpr ()
{
2009-09-09 02:19:03 +04: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-15 01:03:27 +04:00
}
2009-07-09 08:18:58 +04:00
public function testSquareRootExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'SQRT(1)' , ( string ) $this -> _expr -> sqrt ( 1 ));
2009-07-09 08:18:58 +04:00
}
public function testEqualExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( '1 = 1' , ( string ) $this -> _expr -> eq ( 1 , 1 ));
2009-07-09 08:18:58 +04:00
}
public function testLikeExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'a.description LIKE :description' , ( string ) $this -> _expr -> like ( 'a.description' , ':description' ));
2009-07-09 08:18:58 +04:00
}
2012-10-25 14:58:19 +04:00
public function testNotLikeExpr ()
{
$this -> assertEquals ( 'a.description NOT LIKE :description' , ( string ) $this -> _expr -> notLike ( 'a.description' , ':description' ));
}
2009-07-09 08:18:58 +04:00
public function testConcatExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'CONCAT(u.first_name, u.last_name)' , ( string ) $this -> _expr -> concat ( 'u.first_name' , 'u.last_name' ));
2009-07-09 08:18:58 +04:00
}
2010-03-01 23:17:08 +03:00
public function testSubstringExpr ()
2009-07-09 08:18:58 +04:00
{
2010-03-01 23:17:08 +03:00
$this -> assertEquals ( 'SUBSTRING(a.title, 0, 25)' , ( string ) $this -> _expr -> substring ( 'a.title' , 0 , 25 ));
2009-07-09 08:18:58 +04:00
}
2010-05-26 00:10:59 +04:00
/**
* @ group regression
* @ group DDC - 612
*/
public function testSubstringExprAcceptsTwoArguments ()
{
2010-05-26 02:22:42 +04:00
$this -> assertEquals ( 'SUBSTRING(a.title, 5)' , ( string ) $this -> _expr -> substring ( 'a.title' , 5 ));
2010-05-26 00:10:59 +04:00
}
2009-07-09 08:18:58 +04:00
public function testLowerExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'LOWER(u.first_name)' , ( string ) $this -> _expr -> lower ( 'u.first_name' ));
2009-07-09 08:18:58 +04:00
}
public function testUpperExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'UPPER(u.first_name)' , ( string ) $this -> _expr -> upper ( 'u.first_name' ));
2009-07-09 08:18:58 +04:00
}
public function testLengthExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'LENGTH(u.first_name)' , ( string ) $this -> _expr -> length ( 'u.first_name' ));
2009-07-09 08:18:58 +04:00
}
public function testGreaterThanExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( '5 > 2' , ( string ) $this -> _expr -> gt ( 5 , 2 ));
2009-07-09 08:18:58 +04:00
}
public function testLessThanExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( '2 < 5' , ( string ) $this -> _expr -> lt ( 2 , 5 ));
2009-07-09 08:18:58 +04:00
}
public function testStringLiteralExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( " 'word' " , ( string ) $this -> _expr -> literal ( 'word' ));
2009-07-09 08:18:58 +04:00
}
public function testNumericLiteralExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 5 , ( string ) $this -> _expr -> literal ( 5 ));
2009-07-09 08:18:58 +04:00
}
2010-05-25 22:27:06 +04:00
/**
* @ group regression
* @ group DDC - 610
*/
public function testLiteralExprProperlyQuotesStrings ()
{
$this -> assertEquals ( " '00010001' " , ( string ) $this -> _expr -> literal ( '00010001' ));
}
2009-07-09 08:18:58 +04:00
public function testGreaterThanOrEqualToExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( '5 >= 2' , ( string ) $this -> _expr -> gte ( 5 , 2 ));
2009-07-09 08:18:58 +04:00
}
public function testLessThanOrEqualTo ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( '2 <= 5' , ( string ) $this -> _expr -> lte ( 2 , 5 ));
2009-07-09 08:18:58 +04:00
}
public function testBetweenExpr ()
{
2010-03-28 14:30:43 +04:00
$this -> assertEquals ( 'u.id BETWEEN 3 AND 6' , ( string ) $this -> _expr -> between ( 'u.id' , 3 , 6 ));
2009-07-09 08:18:58 +04:00
}
public function testTrimExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'TRIM(u.id)' , ( string ) $this -> _expr -> trim ( 'u.id' ));
2009-07-09 08:18:58 +04:00
}
2011-02-20 07: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 16:00:45 +04:00
public function testIsInstanceOfExpr () {
$this -> assertEquals ( 'u INSTANCE OF Doctrine\Tests\Models\Company\CompanyEmployee' , ( string ) $this -> _expr -> isInstanceOf ( 'u' , 'Doctrine\Tests\Models\Company\CompanyEmployee' ));
}
public function testIsMemberOfExpr () {
$this -> assertEquals ( ':groupId MEMBER OF u.groups' , ( string ) $this -> _expr -> isMemberOf ( ':groupId' , 'u.groups' ));
}
2009-07-09 08:18:58 +04:00
public function testInExpr ()
{
2009-09-09 02:19:03 +04:00
$this -> assertEquals ( 'u.id IN(1, 2, 3)' , ( string ) $this -> _expr -> in ( 'u.id' , array ( 1 , 2 , 3 )));
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
2010-07-01 23:42:38 +04:00
public function testInLiteralExpr ()
{
$this -> assertEquals ( " u.type IN('foo', 'bar') " , ( string ) $this -> _expr -> in ( 'u.type' , array ( 'foo' , 'bar' )));
}
public function testNotInExpr ()
{
$this -> assertEquals ( 'u.id NOT IN(1, 2, 3)' , ( string ) $this -> _expr -> notIn ( 'u.id' , array ( 1 , 2 , 3 )));
}
public function testNotInLiteralExpr ()
{
$this -> assertEquals ( " u.type NOT IN('foo', 'bar') " , ( string ) $this -> _expr -> notIn ( 'u.type' , array ( 'foo' , 'bar' )));
}
2009-07-10 18:02:06 +04:00
public function testAndxOrxExpr ()
2009-07-10 01:56:34 +04:00
{
2009-09-09 02:19:03 +04:00
$andExpr = $this -> _expr -> andx ();
$andExpr -> add ( $this -> _expr -> eq ( 1 , 1 ));
$andExpr -> add ( $this -> _expr -> lt ( 1 , 5 ));
2009-07-10 01:56:34 +04:00
2009-09-09 02:19:03 +04:00
$orExpr = $this -> _expr -> orx ();
2009-07-10 18:02:06 +04:00
$orExpr -> add ( $andExpr );
2009-09-09 02:19:03 +04:00
$orExpr -> add ( $this -> _expr -> eq ( 1 , 1 ));
2009-07-10 01:56:34 +04:00
2011-05-08 03:14:04 +04:00
$this -> assertEquals ( '(1 = 1 AND 1 < 5) OR 1 = 1' , ( string ) $orExpr );
2009-07-10 01:56:34 +04:00
}
2009-07-10 18:02:06 +04:00
public function testOrxExpr ()
2009-07-10 01:56:34 +04:00
{
2009-09-09 02:19:03 +04:00
$orExpr = $this -> _expr -> orx ();
$orExpr -> add ( $this -> _expr -> eq ( 1 , 1 ));
$orExpr -> add ( $this -> _expr -> lt ( 1 , 5 ));
2009-07-10 01:56:34 +04:00
2011-05-08 03:14:04 +04:00
$this -> assertEquals ( '1 = 1 OR 1 < 5' , ( string ) $orExpr );
2009-07-10 01:56:34 +04:00
}
2010-05-25 22:27:06 +04:00
2009-07-10 21:53:48 +04:00
public function testOrderByCountExpr ()
{
2010-02-09 20:13:49 +03:00
$orderExpr = $this -> _expr -> desc ( 'u.username' );
2009-07-10 21:53:48 +04:00
2010-02-09 20:13:49 +03:00
$this -> assertEquals ( $orderExpr -> count (), 1 );
$this -> assertEquals ( 'u.username DESC' , ( string ) $orderExpr );
2009-07-10 21:53:48 +04:00
}
public function testOrderByOrder ()
{
2010-02-09 20:13:49 +03:00
$orderExpr = $this -> _expr -> desc ( 'u.username' );
$this -> assertEquals ( 'u.username DESC' , ( string ) $orderExpr );
2009-07-10 21:53:48 +04:00
}
2010-02-09 20:13:49 +03:00
public function testOrderByAsc ()
2009-07-10 21:53:48 +04:00
{
2010-02-09 20:13:49 +03:00
$orderExpr = $this -> _expr -> asc ( 'u.username' );
$this -> assertEquals ( 'u.username ASC' , ( string ) $orderExpr );
2009-07-10 21:53:48 +04:00
}
/**
2010-02-09 20:13:49 +03:00
* @ expectedException \InvalidArgumentException
2009-07-10 21:53:48 +04:00
*/
public function testAddThrowsException ()
{
2009-09-09 02:19:03 +04:00
$orExpr = $this -> _expr -> orx ();
$orExpr -> add ( $this -> _expr -> quot ( 5 , 2 ));
2009-07-10 01:56:34 +04:00
}
2012-03-14 23:49:25 +04:00
/**
* @ group DDC - 1683
*/
public function testBooleanLiteral ()
{
$this -> assertEquals ( 'true' , $this -> _expr -> literal ( true ));
$this -> assertEquals ( 'false' , $this -> _expr -> literal ( false ));
}
2012-03-25 07:30:58 +04:00
/**
* @ group DDC - 1686
*/
public function testExpressionGetter ()
{
// Andx
$andx = new Expr\Andx ( array ( '1 = 1' , '2 = 2' ));
$this -> assertEquals ( array ( '1 = 1' , '2 = 2' ), $andx -> getParts ());
// 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
$func = new Expr\Func ( 'MAX' , array ( 'f.id' ));
$this -> assertEquals ( 'MAX' , $func -> getName ());
$this -> assertEquals ( array ( 'f.id' ), $func -> getArguments ());
// GroupBy
$group = new Expr\GroupBy ( array ( 'foo DESC' , 'bar ASC' ));
$this -> assertEquals ( array ( 'foo DESC' , 'bar ASC' ), $group -> getParts ());
// 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
$literal = new Expr\Literal ( array ( 'foo' ));
$this -> assertEquals ( array ( 'foo' ), $literal -> getParts ());
// 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' );
$this -> assertEquals ( array ( 'foo DESC' ), $order -> getParts ());
// Andx
$orx = new Expr\Orx ( array ( 'foo = 1' , 'bar = 2' ));
$this -> assertEquals ( array ( 'foo = 1' , 'bar = 2' ), $orx -> getParts ());
// Select
$select = new Expr\Select ( array ( 'foo' , 'bar' ));
$this -> assertEquals ( array ( 'foo' , 'bar' ), $select -> getParts ());
}
2013-03-08 19:33:47 +04:00
public function testAddEmpty () {
$andExpr = $this -> _expr -> andx ();
$andExpr -> add ( $this -> _expr -> andx ());
$this -> assertEquals ( 0 , $andExpr -> count ());
}
public function testAddNull () {
$andExpr = $this -> _expr -> andx ();
$andExpr -> add ( null );
$this -> assertEquals ( 0 , $andExpr -> count ());
}
2012-03-14 23:49:25 +04:00
}