2009-07-09 08:18:58 +04:00
< ? php
/*
* $Id $
*
* 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 ;
use Doctrine\ORM\QueryBuilder ;
use Doctrine\ORM\Query\Expr ;
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
* @ license http :// www . opensource . org / licenses / lgpl - license . php LGPL
* @ link http :// www . phpdoctrine . org
* @ since 2.0
* @ version $Revision $
*/
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 ();
try {
$q -> getSql ();
} catch ( \Exception $e ) {
echo $dql . " \n " ;
echo $e -> getTraceAsString ();
$this -> fail ( $e -> getMessage ());
}
$this -> assertEquals ( $expectedDql , $dql );
}
2009-07-10 01:56:34 +04:00
public function testSelectSetsType ()
2009-07-09 08:18:58 +04:00
{
$qb = QueryBuilder :: create ( $this -> _em )
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 01:56:34 +04:00
public function testDeleteSetsType ()
2009-07-09 08:18:58 +04:00
{
$qb = QueryBuilder :: create ( $this -> _em )
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
{
$qb = QueryBuilder :: create ( $this -> _em )
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
{
$qb = QueryBuilder :: create ( $this -> _em )
-> 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
{
$qb = QueryBuilder :: create ( $this -> _em )
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
{
$qb = QueryBuilder :: create ( $this -> _em )
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
{
$qb = QueryBuilder :: create ( $this -> _em )
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-07-10 01:56:34 +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
}
2009-07-10 01:56:34 +04:00
public function testLeftJoin ()
2009-07-09 08:18:58 +04:00
{
$qb = QueryBuilder :: create ( $this -> _em )
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-07-10 01:56:34 +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
}
2009-07-10 01:56:34 +04:00
public function testWhere ()
2009-07-09 08:18:58 +04:00
{
$qb = QueryBuilder :: create ( $this -> _em )
-> 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-07-10 01:56:34 +04:00
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid' );
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
{
$qb = QueryBuilder :: create ( $this -> _em )
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
2009-07-10 01:56:34 +04:00
-> groupBy ( 'u.id' );
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' );
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
{
$qb = QueryBuilder :: create ( $this -> _em )
-> 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
{
$qb = QueryBuilder :: create ( $this -> _em )
-> 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
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 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
{
$qb = QueryBuilder :: create ( $this -> _em )
-> 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
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 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
{
$qb = QueryBuilder :: create ( $this -> _em )
-> 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
}
2009-07-10 01:56:34 +04:00
public function testAddOrderBy ()
2009-07-09 08:18:58 +04:00
{
$qb = QueryBuilder :: create ( $this -> _em )
-> 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
{
$qb = QueryBuilder :: create ( $this -> _em )
-> 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 01:56:34 +04:00
$this -> assertEquals ( get_class ( $q ), 'Doctrine\ORM\Query' );
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
{
$qb = QueryBuilder :: create ( $this -> _em )
-> 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 01:56:34 +04:00
$q = $qb -> getQuery ();
$this -> assertEquals ( $q -> getParameters (), array ( 'id' => 1 ));
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
{
$qb = QueryBuilder :: create ( $this -> _em )
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
2009-07-10 01:56:34 +04:00
-> where ( 'u.username = :username OR 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 01:56:34 +04:00
$q = $qb -> getQuery ();
2009-07-09 08:18:58 +04:00
2009-07-10 01:56:34 +04:00
$this -> assertEquals ( $q -> getParameters (), array ( 'username' => 'jwage' , 'username2' => 'jonwage' ));
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
public function testExprProxyWithMagicCall ()
2009-07-09 08:18:58 +04:00
{
$qb = QueryBuilder :: create ( $this -> _em )
-> select ( 'u' )
2009-07-10 01:56:34 +04:00
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' );
$qb -> where ( $qb -> eq ( '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 WHERE u.id = 1' );
2009-07-09 08:18:58 +04:00
}
2009-07-10 01:56:34 +04:00
/**
* @ expectedException \Doctrine\Common\DoctrineException
*/
public function testInvalidQueryBuilderMethodThrowsException ()
2009-07-09 08:18:58 +04:00
{
$qb = QueryBuilder :: create ( $this -> _em )
-> select ( 'u' )
2009-07-10 01:56:34 +04:00
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' );
$qb -> where ( $qb -> blah ( 'u.id' , 1 ));
2009-07-09 08:18:58 +04:00
}
public function testLimit ()
{
/*
TODO : Limit fails . Is this not implemented in the DQL parser ? Will look tomorrow .
$qb = QueryBuilder :: create ( $this -> _em )
-> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> limit ( 10 )
-> offset ( 0 );
$this -> assertValidQueryBuilder ( $qb , 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u LIMIT 10' );
*/
}
}