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 ;
2009-08-13 14:13:06 +04:00
use Doctrine\ORM\QueryBuilder ,
Doctrine\ORM\Query\Expr ;
2009-07-09 08:18:58 +04:00
require_once __DIR__ . '/../TestInit.php' ;
/**
* Test case for the QueryBuilder class used to build DQL query string in a
* object oriented way .
*
* @ author Jonathan H . Wage < jonwage @ gmail . com >
* @ author Roman Borschel < roman @ code - factory . org
* @ since 2.0
*/
class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase
{
private $_em ;
protected function setUp ()
{
$this -> _em = $this -> _getTestEntityManager ();
}
protected function assertValidQueryBuilder ( QueryBuilder $qb , $expectedDql )
{
$dql = $qb -> getDql ();
$q = $qb -> getQuery ();
$this -> assertEquals ( $expectedDql , $dql );
}
2009-07-10 01:56:34 +04:00
public function testSelectSetsType ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-10 01:56:34 +04:00
-> delete ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> select ( 'u.id' , 'u.username' );
2009-07-09 08:18:58 +04:00
2009-07-10 01:56:34 +04:00
$this -> assertEquals ( $qb -> getType (), QueryBuilder :: SELECT );
2009-07-09 08:18:58 +04:00
}
2009-07-10 21:53:48 +04:00
public function testEmptySelectSetsType ()
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-10 21:53:48 +04:00
-> delete ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> select ();
$this -> assertEquals ( $qb -> getType (), QueryBuilder :: SELECT );
}
2009-07-10 01:56:34 +04:00
public function testDeleteSetsType ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-10 01:56:34 +04:00
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> delete ();
2009-07-09 08:18:58 +04:00
2009-07-10 01:56:34 +04:00
$this -> assertEquals ( $qb -> getType (), QueryBuilder :: DELETE );
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
public function testUpdateSetsType ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-10 01:56:34 +04:00
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> update ();
2009-07-09 08:18:58 +04:00
2009-07-10 01:56:34 +04:00
$this -> assertEquals ( $qb -> getType (), QueryBuilder :: UPDATE );
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
public function testSimpleSelect ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-09 08:18:58 +04:00
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
2009-07-10 01:56:34 +04:00
-> select ( 'u.id' , 'u.username' );
2009-07-09 08:18:58 +04:00
2009-07-10 01:56:34 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u' );
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
public function testSimpleDelete ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-10 01:56:34 +04:00
-> delete ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' );
2009-07-09 08:18:58 +04:00
2009-07-10 01:56:34 +04:00
$this -> assertValidQueryBuilder ( $qb , 'DELETE Doctrine\Tests\Models\CMS\CmsUser u' );
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
public function testSimpleUpdate ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-10 01:56:34 +04:00
-> update ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> set ( 'u.username' , ':username' );
2009-07-09 08:18:58 +04:00
2009-07-10 01:56:34 +04:00
$this -> assertValidQueryBuilder ( $qb , 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.username = :username' );
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
public function testInnerJoin ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-10 01:56:34 +04:00
-> select ( 'u' , 'a' )
2009-07-09 08:18:58 +04:00
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
2009-08-15 02:50:36 +04:00
-> innerJoin ( 'u.articles' , 'a' );
2009-07-09 08:18:58 +04:00
2009-07-10 01:56:34 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a' );
2009-07-09 08:18:58 +04:00
}
2011-12-20 01:56:19 +04:00
2009-08-15 02:50:36 +04:00
public function testComplexInnerJoin ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' , 'a' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> innerJoin ( 'u.articles' , 'a' , 'ON' , 'u.id = a.author_id' );
$this -> assertValidQueryBuilder (
2011-12-20 01:56:19 +04:00
$qb ,
2009-08-15 02:50:36 +04:00
'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a ON u.id = a.author_id'
);
}
2009-07-09 08:18:58 +04:00
2009-07-10 01:56:34 +04:00
public function testLeftJoin ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-10 01:56:34 +04:00
-> select ( 'u' , 'a' )
2009-07-09 08:18:58 +04:00
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
2009-08-15 02:50:36 +04:00
-> leftJoin ( 'u.articles' , 'a' );
2009-07-09 08:18:58 +04:00
2009-07-10 01:56:34 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a' );
2009-07-09 08:18:58 +04:00
}
2011-05-12 04:40:27 +04:00
public function testLeftJoinWithIndexBy ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' , 'a' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> leftJoin ( 'u.articles' , 'a' , null , null , 'a.name' );
$this -> assertValidQueryBuilder ( $qb , 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a INDEX BY a.name' );
}
2011-12-20 01:56:19 +04:00
2011-05-12 04:40:27 +04:00
public function testMultipleFrom ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' , 'g' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsGroup' , 'g' );
2011-12-20 01:56:19 +04:00
2011-05-12 04:40:27 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsGroup g' );
}
2011-12-20 01:56:19 +04:00
2011-05-12 04:40:27 +04:00
public function testMultipleFromWithJoin ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' , 'g' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsGroup' , 'g' )
-> innerJoin ( 'u.articles' , 'a' , 'ON' , 'u.id = a.author_id' );
2011-12-20 01:56:19 +04:00
2011-05-12 04:40:27 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a ON u.id = a.author_id, Doctrine\Tests\Models\CMS\CmsGroup g' );
}
2011-12-20 01:56:19 +04:00
2011-05-12 04:40:27 +04:00
public function testMultipleFromWithMultipleJoin ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' , 'g' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsArticle' , 'a' )
-> innerJoin ( 'u.groups' , 'g' )
-> leftJoin ( 'u.address' , 'ad' )
-> innerJoin ( 'a.comments' , 'c' );
2011-12-20 01:56:19 +04:00
2011-05-12 04:40:27 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.groups g LEFT JOIN u.address ad, Doctrine\Tests\Models\CMS\CmsArticle a INNER JOIN a.comments c' );
}
2009-07-10 01:56:34 +04:00
public function testWhere ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-09 08:18:58 +04:00
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
2009-07-10 01:56:34 +04:00
-> where ( 'u.id = :uid' );
2009-07-09 08:18:58 +04:00
2009-08-20 06:59:42 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid' );
2009-07-10 18:02:06 +04:00
}
2011-12-20 01:56:19 +04:00
2011-07-03 08:48:18 +04:00
public function testComplexAndWhere ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( 'u.id = :uid OR u.id = :uid2 OR u.id = :uid3' )
-> andWhere ( 'u.name = :name' );
2011-12-20 01:56:19 +04:00
2011-07-03 08:48:18 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id = :uid OR u.id = :uid2 OR u.id = :uid3) AND u.name = :name' );
}
2009-07-10 18:02:06 +04:00
public function testAndWhere ()
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-10 18:02:06 +04:00
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( 'u.id = :uid' )
-> andWhere ( 'u.id = :uid2' );
2011-05-08 03:14:04 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2' );
2009-07-10 18:02:06 +04:00
}
public function testOrWhere ()
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-10 18:02:06 +04:00
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( 'u.id = :uid' )
-> orWhere ( 'u.id = :uid2' );
2011-05-08 03:14:04 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id = :uid2' );
2009-07-10 18:02:06 +04:00
}
2010-05-07 15:36:25 +04:00
public function testComplexAndWhereOrWhereNesting ()
{
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( 'u.id = :uid' )
-> orWhere ( 'u.id = :uid2' )
-> andWhere ( 'u.id = :uid3' )
-> orWhere ( 'u.name = :name1' , 'u.name = :name2' )
-> andWhere ( 'u.name <> :noname' );
2011-05-08 03:14:04 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (((u.id = :uid OR u.id = :uid2) AND u.id = :uid3) OR u.name = :name1 OR u.name = :name2) AND u.name <> :noname' );
2010-05-07 15:36:25 +04:00
}
2009-07-10 18:02:06 +04:00
public function testAndWhereIn ()
{
2009-09-09 02:19:03 +04:00
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( 'u.id = :uid' )
-> andWhere ( $qb -> expr () -> in ( 'u.id' , array ( 1 , 2 , 3 )));
2009-07-10 18:02:06 +04:00
2011-05-08 03:14:04 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id IN(1, 2, 3)' );
2009-07-10 18:02:06 +04:00
}
public function testOrWhereIn ()
{
2009-09-09 02:19:03 +04:00
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( 'u.id = :uid' )
-> orWhere ( $qb -> expr () -> in ( 'u.id' , array ( 1 , 2 , 3 )));
2009-07-10 18:02:06 +04:00
2011-05-08 03:14:04 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id IN(1, 2, 3)' );
2009-07-10 18:02:06 +04:00
}
public function testAndWhereNotIn ()
{
2009-09-09 02:19:03 +04:00
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( 'u.id = :uid' )
-> andWhere ( $qb -> expr () -> notIn ( 'u.id' , array ( 1 , 2 , 3 )));
2009-07-10 18:02:06 +04:00
2011-05-08 03:14:04 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id NOT IN(1, 2, 3)' );
2009-07-10 18:02:06 +04:00
}
public function testOrWhereNotIn ()
{
2009-09-09 02:19:03 +04:00
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( 'u.id = :uid' )
-> orWhere ( $qb -> expr () -> notIn ( 'u.id' , array ( 1 , 2 , 3 )));
2009-07-10 18:02:06 +04:00
2011-05-08 03:14:04 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id NOT IN(1, 2, 3)' );
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
public function testGroupBy ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-09 08:18:58 +04:00
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
2009-07-10 21:53:48 +04:00
-> groupBy ( 'u.id' )
-> addGroupBy ( 'u.username' );
2009-07-09 08:18:58 +04:00
2009-07-10 21:53:48 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id, u.username' );
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
public function testHaving ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-09 08:18:58 +04:00
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
2009-07-10 01:56:34 +04:00
-> groupBy ( 'u.id' )
-> having ( 'COUNT(u.id) > 1' );
2009-07-09 08:18:58 +04:00
2009-07-10 01:56:34 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING COUNT(u.id) > 1' );
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
public function testAndHaving ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-09 08:18:58 +04:00
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
2009-07-10 01:56:34 +04:00
-> groupBy ( 'u.id' )
-> having ( 'COUNT(u.id) > 1' )
-> andHaving ( 'COUNT(u.id) < 1' );
2009-07-09 08:18:58 +04:00
2011-05-08 03:14:04 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING COUNT(u.id) > 1 AND COUNT(u.id) < 1' );
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
public function testOrHaving ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-09 08:18:58 +04:00
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
2009-07-10 01:56:34 +04:00
-> groupBy ( 'u.id' )
-> having ( 'COUNT(u.id) > 1' )
-> andHaving ( 'COUNT(u.id) < 1' )
-> orHaving ( 'COUNT(u.id) > 1' );
2009-07-09 08:18:58 +04:00
2011-05-08 03:14:04 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING (COUNT(u.id) > 1 AND COUNT(u.id) < 1) OR COUNT(u.id) > 1' );
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
public function testOrderBy ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-09 08:18:58 +04:00
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
2009-07-10 01:56:34 +04:00
-> orderBy ( 'u.username' , 'ASC' );
2009-07-09 08:18:58 +04:00
2009-07-10 01:56:34 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC' );
2009-07-09 08:18:58 +04:00
}
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
public function testOrderByWithExpression ()
{
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> orderBy ( $qb -> expr () -> asc ( 'u.username' ));
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC' );
}
2009-07-09 08:18:58 +04:00
2009-07-10 01:56:34 +04:00
public function testAddOrderBy ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-09 08:18:58 +04:00
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
2009-07-10 01:56:34 +04:00
-> orderBy ( 'u.username' , 'ASC' )
-> addOrderBy ( 'u.username' , 'DESC' );
2009-07-09 08:18:58 +04:00
2009-07-10 01:56:34 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC, u.username DESC' );
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
public function testGetQuery ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-09 08:18:58 +04:00
-> select ( 'u' )
2009-07-10 01:56:34 +04:00
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' );
$q = $qb -> getQuery ();
2009-07-09 08:18:58 +04:00
2009-07-10 22:26:43 +04:00
$this -> assertEquals ( 'Doctrine\ORM\Query' , get_class ( $q ));
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
public function testSetParameter ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-09 08:18:58 +04:00
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
2009-07-10 01:56:34 +04:00
-> where ( 'u.id = :id' )
-> setParameter ( 'id' , 1 );
2009-07-09 08:18:58 +04:00
2009-07-10 22:26:43 +04:00
$this -> assertEquals ( array ( 'id' => 1 ), $qb -> getParameters ());
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
public function testSetParameters ()
2009-07-09 08:18:58 +04:00
{
2009-09-09 02:19:03 +04:00
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( $qb -> expr () -> orx ( 'u.username = :username' , 'u.username = :username2' ));
2009-07-09 08:18:58 +04:00
2009-07-10 01:56:34 +04:00
$qb -> setParameters ( array ( 'username' => 'jwage' , 'username2' => 'jonwage' ));
2009-07-09 08:18:58 +04:00
2009-07-10 22:26:43 +04:00
$this -> assertEquals ( array ( 'username' => 'jwage' , 'username2' => 'jonwage' ), $qb -> getQuery () -> getParameters ());
2009-07-09 08:18:58 +04:00
}
2009-07-10 21:53:48 +04:00
public function testGetParameters ()
{
2009-09-09 02:19:03 +04:00
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( 'u.id = :id' );
2009-07-10 21:53:48 +04:00
$qb -> setParameters ( array ( 'id' => 1 ));
2010-02-09 20:13:49 +03:00
$this -> assertEquals ( array ( 'id' => 1 ), $qb -> getParameters ());
2009-07-10 21:53:48 +04:00
}
public function testGetParameter ()
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-10 21:53:48 +04:00
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( 'u.id = :id' );
$qb -> setParameters ( array ( 'id' => 1 ));
$this -> assertEquals ( 1 , $qb -> getParameter ( 'id' ));
}
2009-07-10 18:02:06 +04:00
public function testMultipleWhere ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-09 08:18:58 +04:00
-> select ( 'u' )
2009-07-10 18:02:06 +04:00
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( 'u.id = :uid' , 'u.id = :uid2' );
2009-07-09 08:18:58 +04:00
2011-05-08 03:14:04 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2' );
2009-07-09 08:18:58 +04:00
}
2009-07-10 18:02:06 +04:00
public function testMultipleAndWhere ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-09 08:18:58 +04:00
-> select ( 'u' )
2009-07-10 18:02:06 +04:00
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> andWhere ( 'u.id = :uid' , 'u.id = :uid2' );
2011-05-08 03:14:04 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2' );
2009-07-10 18:02:06 +04:00
}
public function testMultipleOrWhere ()
{
2009-09-09 02:19:03 +04:00
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> orWhere ( 'u.id = :uid' , $qb -> expr () -> eq ( 'u.id' , ':uid2' ));
2009-07-10 18:02:06 +04:00
2011-05-08 03:14:04 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id = :uid2' );
2009-07-10 18:02:06 +04:00
}
public function testComplexWhere ()
{
2009-09-09 02:19:03 +04:00
$qb = $this -> _em -> createQueryBuilder ();
2010-02-09 20:13:49 +03:00
$orExpr = $qb -> expr () -> orX ();
2009-09-09 02:19:03 +04:00
$orExpr -> add ( $qb -> expr () -> eq ( 'u.id' , ':uid3' ));
$orExpr -> add ( $qb -> expr () -> in ( 'u.id' , array ( 1 )));
2009-07-10 18:02:06 +04:00
2009-09-09 02:19:03 +04:00
$qb -> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( $orExpr );
2009-07-10 18:02:06 +04:00
2011-05-08 03:14:04 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid3 OR u.id IN(1)' );
2009-07-09 08:18:58 +04:00
}
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
public function testWhereInWithStringLiterals ()
{
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( $qb -> expr () -> in ( 'u.name' , array ( 'one' , 'two' , 'three' )));
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
$this -> assertValidQueryBuilder ( $qb , " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.name IN('one', 'two', 'three') " );
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
$qb -> where ( $qb -> expr () -> in ( 'u.name' , array ( " O'Reilly " , " O'Neil " , 'Smith' )));
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
$this -> assertValidQueryBuilder ( $qb , " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.name IN('O''Reilly', 'O''Neil', 'Smith') " );
}
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
public function testWhereInWithObjectLiterals ()
{
$qb = $this -> _em -> createQueryBuilder ();
$expr = $this -> _em -> getExpressionBuilder ();
$qb -> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( $expr -> in ( 'u.name' , array ( $expr -> literal ( 'one' ), $expr -> literal ( 'two' ), $expr -> literal ( 'three' ))));
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
$this -> assertValidQueryBuilder ( $qb , " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.name IN('one', 'two', 'three') " );
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
$qb -> where ( $expr -> in ( 'u.name' , array ( $expr -> literal ( " O'Reilly " ), $expr -> literal ( " O'Neil " ), $expr -> literal ( 'Smith' ))));
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
$this -> assertValidQueryBuilder ( $qb , " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.name IN('O''Reilly', 'O''Neil', 'Smith') " );
}
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
public function testNegation ()
{
$expr = $this -> _em -> getExpressionBuilder ();
$orExpr = $expr -> orX ();
$orExpr -> add ( $expr -> eq ( 'u.id' , ':uid3' ));
$orExpr -> add ( $expr -> not ( $expr -> in ( 'u.id' , array ( 1 ))));
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( $orExpr );
2011-05-08 03:14:04 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid3 OR NOT(u.id IN(1))' );
2010-02-09 20:13:49 +03:00
}
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
public function testSomeAllAny ()
{
$qb = $this -> _em -> createQueryBuilder ();
$expr = $this -> _em -> getExpressionBuilder ();
//$subquery = $qb->subquery('Doctrine\Tests\Models\CMS\CmsArticle', 'a')->select('a.id');
$qb -> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( $expr -> gt ( 'u.id' , $expr -> all ( 'select a.id from Doctrine\Tests\Models\CMS\CmsArticle a' )));
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id > ALL(select a.id from Doctrine\Tests\Models\CMS\CmsArticle a)' );
}
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
public function testMultipleIsolatedQueryConstruction ()
{
$qb = $this -> _em -> createQueryBuilder ();
$expr = $this -> _em -> getExpressionBuilder ();
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
$qb -> select ( 'u' ) -> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' );
$qb -> where ( $expr -> eq ( 'u.name' , ':name' ));
$qb -> setParameter ( 'name' , 'romanb' );
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
$q1 = $qb -> getQuery ();
$this -> assertEquals ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = :name' , $q1 -> getDql ());
$this -> assertEquals ( 1 , count ( $q1 -> getParameters ()));
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
// add another condition and construct a second query
$qb -> andWhere ( $expr -> eq ( 'u.id' , ':id' ));
$qb -> setParameter ( 'id' , 42 );
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
$q2 = $qb -> getQuery ();
2011-12-20 01:56:19 +04:00
2011-05-08 03:14:04 +04:00
$this -> assertEquals ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = :name AND u.id = :id' , $q2 -> getDql ());
2010-02-09 20:13:49 +03:00
$this -> assertTrue ( $q1 !== $q2 ); // two different, independent queries
$this -> assertEquals ( 2 , count ( $q2 -> getParameters ()));
$this -> assertEquals ( 1 , count ( $q1 -> getParameters ())); // $q1 unaffected
}
2009-07-09 08:18:58 +04:00
2009-07-10 21:53:48 +04:00
public function testGetEntityManager ()
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ();
2009-07-10 21:53:48 +04:00
$this -> assertEquals ( $this -> _em , $qb -> getEntityManager ());
}
public function testInitialStateIsClean ()
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ();
2009-07-10 21:53:48 +04:00
$this -> assertEquals ( QueryBuilder :: STATE_CLEAN , $qb -> getState ());
}
public function testAlteringQueryChangesStateToDirty ()
2009-07-09 08:18:58 +04:00
{
2009-07-10 22:26:43 +04:00
$qb = $this -> _em -> createQueryBuilder ()
2009-07-09 08:18:58 +04:00
-> select ( 'u' )
2009-07-10 21:53:48 +04:00
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' );
2009-07-09 08:18:58 +04:00
2009-07-10 21:53:48 +04:00
$this -> assertEquals ( QueryBuilder :: STATE_DIRTY , $qb -> getState ());
2009-07-09 08:18:58 +04:00
}
2010-06-16 19:55:22 +04:00
public function testSelectWithFuncExpression ()
{
$qb = $this -> _em -> createQueryBuilder ();
$expr = $qb -> expr ();
$qb -> select ( $expr -> count ( 'e.id' ));
$this -> assertValidQueryBuilder ( $qb , 'SELECT COUNT(e.id)' );
}
2010-09-24 00:32:23 +04:00
public function testResetDQLPart ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( 'u.username = ?1' ) -> orderBy ( 'u.username' );
$this -> assertEquals ( 'u.username = ?1' , ( string ) $qb -> getDQLPart ( 'where' ));
$this -> assertEquals ( 1 , count ( $qb -> getDQLPart ( 'orderBy' )));
$qb -> resetDqlPart ( 'where' ) -> resetDqlPart ( 'orderBy' );
$this -> assertNull ( $qb -> getDQLPart ( 'where' ));
$this -> assertEquals ( 0 , count ( $qb -> getDQLPart ( 'orderBy' )));
}
public function testResetDQLParts ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( 'u.username = ?1' ) -> orderBy ( 'u.username' );
$qb -> resetDQLParts ( array ( 'where' , 'orderBy' ));
$this -> assertEquals ( 1 , count ( $qb -> getDQLPart ( 'select' )));
$this -> assertNull ( $qb -> getDQLPart ( 'where' ));
$this -> assertEquals ( 0 , count ( $qb -> getDQLPart ( 'orderBy' )));
}
public function testResetAllDQLParts ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( 'u.username = ?1' ) -> orderBy ( 'u.username' );
$qb -> resetDQLParts ();
$this -> assertEquals ( 0 , count ( $qb -> getDQLPart ( 'select' )));
$this -> assertNull ( $qb -> getDQLPart ( 'where' ));
$this -> assertEquals ( 0 , count ( $qb -> getDQLPart ( 'orderBy' )));
}
2010-11-15 23:32:38 +03:00
/**
* @ group DDC - 867
*/
public function testDeepClone ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> andWhere ( 'u.username = ?1' )
-> andWhere ( 'u.status = ?2' );
$expr = $qb -> getDQLPart ( 'where' );
$this -> assertEquals ( 2 , $expr -> count (), " Modifying the second query should affect the first one. " );
2011-12-20 01:56:19 +04:00
2010-11-15 23:32:38 +03:00
$qb2 = clone $qb ;
$qb2 -> andWhere ( 'u.name = ?3' );
$this -> assertEquals ( 2 , $expr -> count (), " Modifying the second query should affect the first one. " );
}
2011-12-20 01:56:19 +04:00
2011-05-16 00:11:10 +04:00
public function testGetRootAlias ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' );
2011-12-20 01:56:19 +04:00
2011-05-16 00:11:10 +04:00
$this -> assertEquals ( 'u' , $qb -> getRootAlias ());
}
2011-12-20 01:56:19 +04:00
2011-05-16 00:11:10 +04:00
public function testGetRootAliases ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' );
2011-12-20 01:56:19 +04:00
2011-05-16 00:11:10 +04:00
$this -> assertEquals ( array ( 'u' ), $qb -> getRootAliases ());
}
2011-12-20 01:56:19 +04:00
2011-06-09 22:42:40 +04:00
public function testGetRootEntities ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' );
2011-12-20 01:56:19 +04:00
2011-06-09 22:42:40 +04:00
$this -> assertEquals ( array ( 'Doctrine\Tests\Models\CMS\CmsUser' ), $qb -> getRootEntities ());
}
2011-12-20 01:56:19 +04:00
2011-05-16 00:11:10 +04:00
public function testGetSeveralRootAliases ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u2' );
2011-12-20 01:56:19 +04:00
2011-05-16 00:11:10 +04:00
$this -> assertEquals ( array ( 'u' , 'u2' ), $qb -> getRootAliases ());
$this -> assertEquals ( 'u' , $qb -> getRootAlias ());
}
2011-12-20 01:56:19 +04:00
2011-05-16 01:16:44 +04:00
public function testBCAddJoinWithoutRootAlias ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> add ( 'join' , array ( 'INNER JOIN u.groups g' ), true );
2011-12-20 01:56:19 +04:00
2011-05-16 01:16:44 +04:00
$this -> assertEquals ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.groups g' , $qb -> getDQL ());
}
2011-12-20 01:56:19 +04:00
2011-06-19 12:17:35 +04:00
/**
* @ group DDC - 1211
*/
public function testEmptyStringLiteral ()
{
$expr = $this -> _em -> getExpressionBuilder ();
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( $expr -> eq ( 'u.username' , $expr -> literal ( " " )));
2011-12-20 01:56:19 +04:00
2011-06-19 12:17:35 +04:00
$this -> assertEquals ( " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.username = '' " , $qb -> getDQL ());
}
2011-12-20 01:56:19 +04:00
2011-06-19 12:17:35 +04:00
/**
* @ group DDC - 1211
*/
public function testEmptyNumericLiteral ()
{
$expr = $this -> _em -> getExpressionBuilder ();
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> where ( $expr -> eq ( 'u.username' , $expr -> literal ( 0 )));
2011-12-20 01:56:19 +04:00
2011-06-19 12:17:35 +04:00
$this -> assertEquals ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 0' , $qb -> getDQL ());
}
2011-12-20 01:56:19 +04:00
2011-06-25 12:27:06 +04:00
/**
* @ group DDC - 1227
*/
public function testAddFromString ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> add ( 'select' , 'u' )
-> add ( 'from' , 'Doctrine\Tests\Models\CMS\CmsUser u' );
2011-12-20 01:56:19 +04:00
2011-06-25 12:27:06 +04:00
$this -> assertEquals ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' , $qb -> getDQL ());
}
2012-01-25 13:19:01 +04:00
/**
* @ group DDC - 1619
*/
public function testAddDistinct ()
{
$qb = $this -> _em -> createQueryBuilder ()
-> select ( 'u' )
-> distinct ()
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' );
$this -> assertEquals ( 'SELECT DISTINCT u FROM Doctrine\Tests\Models\CMS\CmsUser u' , $qb -> getDQL ());
}
}