2012-01-22 13:35:06 +01:00
< ? php
namespace Doctrine\Tests\ORM\Functional ;
use Doctrine\ORM\Tools\SchemaTool ;
use Doctrine\ORM\Query ;
use Doctrine\Tests\Models\CMS\CmsUser ;
use Doctrine\Tests\Models\CMS\CmsPhonenumber ;
use Doctrine\Tests\Models\CMS\CmsAddress ;
use Doctrine\Tests\Models\CMS\CmsGroup ;
use Doctrine\Tests\Models\CMS\CmsArticle ;
use Doctrine\Tests\Models\CMS\CmsComment ;
use Doctrine\ORM\Tools\Pagination\Paginator ;
/**
* @ group DDC - 1613
*/
class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp ()
{
$this -> useModelSet ( 'cms' );
parent :: setUp ();
$this -> populate ();
}
2012-03-07 08:52:00 +01:00
/**
2012-03-12 08:33:35 +01:00
* @ dataProvider useOutputWalkers
2012-03-07 08:52:00 +01:00
*/
2012-03-12 08:33:35 +01:00
public function testCountSimpleWithoutJoin ( $useOutputWalkers )
2012-01-22 13:35:06 +01:00
{
$dql = " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u " ;
$query = $this -> _em -> createQuery ( $dql );
$paginator = new Paginator ( $query );
2012-03-12 08:33:35 +01:00
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
2012-03-08 09:48:41 +01:00
$this -> assertCount ( 3 , $paginator );
2012-01-22 13:35:06 +01:00
}
2012-03-07 08:52:00 +01:00
/**
2012-03-12 08:33:35 +01:00
* @ dataProvider useOutputWalkers
2012-03-07 08:52:00 +01:00
*/
2012-03-12 08:33:35 +01:00
public function testCountWithFetchJoin ( $useOutputWalkers )
2012-01-22 13:35:06 +01:00
{
$dql = " SELECT u,g FROM Doctrine \T ests \ Models \ CMS \ CmsUser u JOIN u.groups g " ;
$query = $this -> _em -> createQuery ( $dql );
$paginator = new Paginator ( $query );
2012-03-12 08:33:35 +01:00
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
2012-03-08 09:48:41 +01:00
$this -> assertCount ( 3 , $paginator );
2012-01-22 13:35:06 +01:00
}
2012-03-12 08:33:35 +01:00
public function testCountComplexWithOutputWalker ()
2012-03-06 16:24:44 +01:00
{
2012-03-14 07:58:58 +01:00
$dql = " SELECT g, COUNT(u.id) AS userCount FROM Doctrine \T ests \ Models \ CMS \ CmsGroup g LEFT JOIN g.users u GROUP BY g HAVING COUNT(u.id) > 0 " ;
2012-03-06 16:24:44 +01:00
$query = $this -> _em -> createQuery ( $dql );
$paginator = new Paginator ( $query );
2012-03-12 08:33:35 +01:00
$paginator -> setUseOutputWalkers ( true );
2012-03-08 09:48:41 +01:00
$this -> assertCount ( 9 , $paginator );
2012-03-06 16:24:44 +01:00
}
2012-03-07 08:52:00 +01:00
/**
2012-03-12 08:33:35 +01:00
* @ dataProvider useOutputWalkers
2012-03-07 08:52:00 +01:00
*/
2012-03-12 08:33:35 +01:00
public function testIterateSimpleWithoutJoinFetchJoinHandlingOff ( $useOutputWalkers )
2012-01-22 13:35:06 +01:00
{
$dql = " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u " ;
$query = $this -> _em -> createQuery ( $dql );
$paginator = new Paginator ( $query , false );
2012-03-12 08:33:35 +01:00
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
2012-03-08 09:48:41 +01:00
$this -> assertCount ( 3 , $paginator -> getIterator ());
2012-01-22 13:35:06 +01:00
}
2012-03-07 08:52:00 +01:00
/**
2012-03-12 08:33:35 +01:00
* @ dataProvider useOutputWalkers
2012-03-07 08:52:00 +01:00
*/
2012-03-12 08:33:35 +01:00
public function testIterateSimpleWithoutJoinFetchJoinHandlingOn ( $useOutputWalkers )
2012-01-22 13:35:06 +01:00
{
$dql = " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u " ;
$query = $this -> _em -> createQuery ( $dql );
$paginator = new Paginator ( $query , true );
2012-03-12 08:33:35 +01:00
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
2012-03-08 09:48:41 +01:00
$this -> assertCount ( 3 , $paginator -> getIterator ());
2012-01-22 13:35:06 +01:00
}
2012-03-07 08:52:00 +01:00
/**
2012-03-12 08:33:35 +01:00
* @ dataProvider useOutputWalkers
2012-03-07 08:52:00 +01:00
*/
2012-03-12 08:33:35 +01:00
public function testIterateWithFetchJoin ( $useOutputWalkers )
2012-01-22 13:35:06 +01:00
{
$dql = " SELECT u,g FROM Doctrine \T ests \ Models \ CMS \ CmsUser u JOIN u.groups g " ;
$query = $this -> _em -> createQuery ( $dql );
$paginator = new Paginator ( $query , true );
2012-03-12 08:33:35 +01:00
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
2012-03-08 09:48:41 +01:00
$this -> assertCount ( 3 , $paginator -> getIterator ());
2012-01-22 13:35:06 +01:00
}
2012-03-12 08:33:35 +01:00
public function testIterateComplexWithOutputWalker ()
2012-03-06 16:24:44 +01:00
{
2012-03-14 07:58:58 +01:00
$dql = " SELECT g, COUNT(u.id) AS userCount FROM Doctrine \T ests \ Models \ CMS \ CmsGroup g LEFT JOIN g.users u GROUP BY g HAVING COUNT(u.id) > 0 " ;
2012-03-06 16:24:44 +01:00
$query = $this -> _em -> createQuery ( $dql );
$paginator = new Paginator ( $query );
2012-03-12 08:33:35 +01:00
$paginator -> setUseOutputWalkers ( true );
2012-03-08 09:48:41 +01:00
$this -> assertCount ( 9 , $paginator -> getIterator ());
2012-03-06 16:24:44 +01:00
}
2012-03-12 08:33:35 +01:00
public function testDetectOutputWalker ()
2012-03-06 16:24:44 +01:00
{
2012-03-12 08:33:35 +01:00
// This query works using the output walkers but causes an exception using the TreeWalker
2012-03-14 07:58:58 +01:00
$dql = " SELECT g, COUNT(u.id) AS userCount FROM Doctrine \T ests \ Models \ CMS \ CmsGroup g LEFT JOIN g.users u GROUP BY g HAVING COUNT(u.id) > 0 " ;
2012-03-06 16:24:44 +01:00
$query = $this -> _em -> createQuery ( $dql );
2012-03-12 08:33:35 +01:00
// If the Paginator detects the custom output walker it should fall back to using the
// Tree walkers for pagination, which leads to an exception. If the query works, the output walkers were used
2012-03-06 16:24:44 +01:00
$query -> setHint ( Query :: HINT_CUSTOM_OUTPUT_WALKER , 'Doctrine\ORM\Query\SqlWalker' );
$paginator = new Paginator ( $query );
2012-03-07 08:42:09 +01:00
$this -> setExpectedException (
'RuntimeException' ,
2012-03-12 08:33:35 +01:00
'Cannot count query that uses a HAVING clause. Use the output walkers for pagination'
2012-03-07 08:42:09 +01:00
);
count ( $paginator );
2012-03-06 16:24:44 +01:00
}
2012-07-07 17:27:32 +02:00
public function testCloneQuery ()
{
$dql = " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u " ;
$query = $this -> _em -> createQuery ( $dql );
$paginator = new Paginator ( $query );
$paginator -> getIterator ();
$this -> assertTrue ( $query -> getParameters () -> isEmpty ());
}
2013-07-03 11:18:19 +00:00
public function testQueryWalkerIsKept ()
{
$dql = " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u " ;
$query = $this -> _em -> createQuery ( $dql );
2013-07-04 08:29:46 +00:00
$query -> setHint ( Query :: HINT_CUSTOM_TREE_WALKERS , array ( 'Doctrine\Tests\ORM\Functional\CustomPaginationTestTreeWalker' ));
2013-07-03 11:18:19 +00:00
$paginator = new Paginator ( $query , true );
$paginator -> setUseOutputWalkers ( false );
$this -> assertCount ( 1 , $paginator -> getIterator ());
$this -> assertEquals ( 1 , $paginator -> count ());
}
2014-11-24 15:28:37 +01:00
public function testCountQuery_ParametersInSelect ()
{
/** @var $query Query */
$query = $this -> _em -> createQuery (
2014-11-24 16:41:16 +01:00
'SELECT u, (CASE WHEN u.id < :vipMaxId THEN 1 ELSE 0 END) AS hidden promotedFirst FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id < :id or 1=1 ORDER BY promotedFirst'
2014-11-24 15:28:37 +01:00
);
$query -> setParameter ( 'vipMaxId' , 10 );
$query -> setParameter ( 'id' , 100 );
$query -> setFirstResult ( null ) -> setMaxResults ( null );
$paginator = new Paginator ( $query );
$countQuery = $paginator -> getCountQuery ();
$this -> assertEquals ( 2 , count ( $countQuery -> getParameters ()));
$this -> assertEquals ( 3 , $paginator -> count ());
$query -> setHint ( Query :: HINT_CUSTOM_OUTPUT_WALKER , 'Doctrine\ORM\Query\SqlWalker' );
$paginator = new Paginator ( $query );
$countQuery = $paginator -> getCountQuery ();
//if select part of query is replaced with count(...) paginator should remove parameters from query object not used in new query.
$this -> assertEquals ( 1 , count ( $countQuery -> getParameters ()));
$this -> assertEquals ( 3 , $paginator -> count ());
}
2013-07-03 11:18:19 +00:00
2012-01-22 13:35:06 +01:00
public function populate ()
{
for ( $i = 0 ; $i < 3 ; $i ++ ) {
$user = new CmsUser ();
$user -> name = " Name $i " ;
$user -> username = " username $i " ;
$user -> status = " active " ;
$this -> _em -> persist ( $user );
for ( $j = 0 ; $j < 3 ; $j ++ ) {;
$group = new CmsGroup ();
$group -> name = " group $j " ;
$user -> addGroup ( $group );
$this -> _em -> persist ( $group );
}
}
$this -> _em -> flush ();
}
2012-03-07 08:52:00 +01:00
2012-03-12 08:33:35 +01:00
public function useOutputWalkers ()
2012-03-07 08:52:00 +01:00
{
return array (
array ( true ),
array ( false ),
);
}
2012-01-22 13:35:06 +01:00
}
2013-07-03 11:18:19 +00:00
2013-07-04 08:29:46 +00:00
class CustomPaginationTestTreeWalker extends Query\TreeWalkerAdapter
2013-07-03 11:18:19 +00:00
{
public function walkSelectStatement ( Query\AST\SelectStatement $selectStatement )
{
$condition = new Query\AST\ConditionalPrimary ();
$path = new Query\AST\PathExpression ( Query\AST\PathExpression :: TYPE_STATE_FIELD , 'u' , 'name' );
$path -> type = Query\AST\PathExpression :: TYPE_STATE_FIELD ;
$condition -> simpleConditionalExpression = new Query\AST\ComparisonExpression (
$path ,
'=' ,
new Query\AST\Literal ( Query\AST\Literal :: STRING , 'Name1' )
);
$selectStatement -> whereClause = new Query\AST\WhereClause ( $condition );
}
}