2012-01-22 13:35:06 +01:00
< ? php
namespace Doctrine\Tests\ORM\Functional ;
use Doctrine\ORM\Query ;
2015-03-19 21:59:15 -04:00
use Doctrine\Tests\Models\CMS\CmsEmail ;
2012-01-22 13:35:06 +01:00
use Doctrine\Tests\Models\CMS\CmsUser ;
use Doctrine\Tests\Models\CMS\CmsGroup ;
use Doctrine\ORM\Tools\Pagination\Paginator ;
2015-03-19 21:59:15 -04:00
use Doctrine\Tests\Models\Pagination\Company ;
2015-03-25 22:28:32 -04:00
use Doctrine\Tests\Models\Pagination\Department ;
2015-03-19 21:59:15 -04:00
use Doctrine\Tests\Models\Pagination\Logo ;
2014-11-27 17:50:27 +01:00
use ReflectionMethod ;
2012-01-22 13:35:06 +01:00
/**
* @ group DDC - 1613
*/
class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp ()
{
$this -> useModelSet ( 'cms' );
2015-03-19 21:59:15 -04:00
$this -> useModelSet ( 'pagination' );
2012-01-22 13:35:06 +01:00
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
{
2015-03-20 12:28:55 -04:00
$dql = 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' ;
2012-01-22 13:35:06 +01:00
$query = $this -> _em -> createQuery ( $dql );
$paginator = new Paginator ( $query );
2012-03-12 08:33:35 +01:00
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
2015-03-19 21:59:15 -04:00
$this -> assertCount ( 9 , $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
{
2015-03-20 12:28:55 -04:00
$dql = 'SELECT u,g FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.groups g' ;
2012-01-22 13:35:06 +01:00
$query = $this -> _em -> createQuery ( $dql );
$paginator = new Paginator ( $query );
2012-03-12 08:33:35 +01:00
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
2015-03-19 21:59:15 -04:00
$this -> assertCount ( 9 , $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
{
2015-03-20 12:28:55 -04:00
$dql = 'SELECT g, COUNT(u.id) AS userCount FROM Doctrine\Tests\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 );
2015-03-19 21:59:15 -04:00
$this -> assertCount ( 3 , $paginator );
}
public function testCountComplexWithoutOutputWalker ()
{
2015-03-20 12:28:55 -04:00
$dql = 'SELECT g, COUNT(u.id) AS userCount FROM Doctrine\Tests\Models\CMS\CmsGroup g LEFT JOIN g.users u GROUP BY g HAVING COUNT(u.id) > 0' ;
2015-03-19 21:59:15 -04:00
$query = $this -> _em -> createQuery ( $dql );
$paginator = new Paginator ( $query );
$paginator -> setUseOutputWalkers ( false );
$this -> setExpectedException (
'RuntimeException' ,
'Cannot count query that uses a HAVING clause. Use the output walkers for pagination'
);
$this -> assertCount ( 3 , $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
*/
2015-03-19 21:59:15 -04:00
public function testCountWithComplexScalarOrderBy ( $useOutputWalkers )
2012-01-22 13:35:06 +01:00
{
2015-03-19 21:59:15 -04:00
$dql = 'SELECT l FROM Doctrine\Tests\Models\Pagination\Logo l ORDER BY l.image_width * l.image_height DESC' ;
2012-01-22 13:35:06 +01:00
$query = $this -> _em -> createQuery ( $dql );
2015-03-19 21:59:15 -04:00
$paginator = new Paginator ( $query );
2012-03-12 08:33:35 +01:00
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
2015-03-19 21:59:15 -04:00
$this -> assertCount ( 9 , $paginator );
2012-01-22 13:35:06 +01:00
}
2012-03-07 08:52:00 +01:00
/**
2015-03-19 21:59:15 -04:00
* @ dataProvider useOutputWalkersAndFetchJoinCollection
2012-03-07 08:52:00 +01:00
*/
2015-03-19 21:59:15 -04:00
public function testIterateSimpleWithoutJoin ( $useOutputWalkers , $fetchJoinCollection )
2012-01-22 13:35:06 +01:00
{
2015-03-20 12:28:55 -04:00
$dql = 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' ;
2012-01-22 13:35:06 +01:00
$query = $this -> _em -> createQuery ( $dql );
2015-03-19 21:59:15 -04:00
$paginator = new Paginator ( $query , $fetchJoinCollection );
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
$this -> assertCount ( 9 , $paginator -> getIterator ());
// Test with limit
$query -> setMaxResults ( 3 );
$paginator = new Paginator ( $query , $fetchJoinCollection );
2012-03-12 08:33:35 +01:00
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
2012-03-08 09:48:41 +01:00
$this -> assertCount ( 3 , $paginator -> getIterator ());
2015-03-19 21:59:15 -04:00
// Test with limit and offset
$query -> setMaxResults ( 3 ) -> setFirstResult ( 4 );
$paginator = new Paginator ( $query , $fetchJoinCollection );
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
$this -> assertCount ( 3 , $paginator -> getIterator ());
}
2015-03-20 12:28:55 -04:00
private function iterateWithOrderAsc ( $useOutputWalkers , $fetchJoinCollection , $baseDql , $checkField )
2015-03-19 21:59:15 -04:00
{
2015-03-20 12:28:55 -04:00
2015-03-19 21:59:15 -04:00
// Ascending
$dql = " $baseDql ASC " ;
$query = $this -> _em -> createQuery ( $dql );
$paginator = new Paginator ( $query , $fetchJoinCollection );
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
$iter = $paginator -> getIterator ();
$this -> assertCount ( 9 , $iter );
$result = iterator_to_array ( $iter );
$this -> assertEquals ( $checkField . " 0 " , $result [ 0 ] -> $checkField );
2015-03-20 12:28:55 -04:00
}
private function iterateWithOrderAscWithLimit ( $useOutputWalkers , $fetchJoinCollection , $baseDql , $checkField )
{
// Ascending
$dql = " $baseDql ASC " ;
$query = $this -> _em -> createQuery ( $dql );
2015-03-19 21:59:15 -04:00
// With limit
$query -> setMaxResults ( 3 );
$paginator = new Paginator ( $query , $fetchJoinCollection );
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
$iter = $paginator -> getIterator ();
$this -> assertCount ( 3 , $iter );
$result = iterator_to_array ( $iter );
$this -> assertEquals ( $checkField . " 0 " , $result [ 0 ] -> $checkField );
2015-03-20 12:28:55 -04:00
}
private function iterateWithOrderAscWithLimitAndOffset ( $useOutputWalkers , $fetchJoinCollection , $baseDql , $checkField )
{
// Ascending
$dql = " $baseDql ASC " ;
$query = $this -> _em -> createQuery ( $dql );
2015-03-19 21:59:15 -04:00
// With offset
$query -> setMaxResults ( 3 ) -> setFirstResult ( 3 );
$paginator = new Paginator ( $query , $fetchJoinCollection );
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
$iter = $paginator -> getIterator ();
$this -> assertCount ( 3 , $iter );
$result = iterator_to_array ( $iter );
$this -> assertEquals ( $checkField . " 3 " , $result [ 0 ] -> $checkField );
2015-03-20 12:28:55 -04:00
}
2015-03-19 21:59:15 -04:00
2015-03-20 12:28:55 -04:00
private function iterateWithOrderDesc ( $useOutputWalkers , $fetchJoinCollection , $baseDql , $checkField )
{
2015-03-19 21:59:15 -04:00
$dql = " $baseDql DESC " ;
$query = $this -> _em -> createQuery ( $dql );
$paginator = new Paginator ( $query , $fetchJoinCollection );
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
$iter = $paginator -> getIterator ();
$this -> assertCount ( 9 , $iter );
$result = iterator_to_array ( $iter );
$this -> assertEquals ( $checkField . " 8 " , $result [ 0 ] -> $checkField );
2015-03-20 12:28:55 -04:00
}
private function iterateWithOrderDescWithLimit ( $useOutputWalkers , $fetchJoinCollection , $baseDql , $checkField )
{
$dql = " $baseDql DESC " ;
$query = $this -> _em -> createQuery ( $dql );
2015-03-19 21:59:15 -04:00
// With limit
$query -> setMaxResults ( 3 );
$paginator = new Paginator ( $query , $fetchJoinCollection );
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
$iter = $paginator -> getIterator ();
$this -> assertCount ( 3 , $iter );
$result = iterator_to_array ( $iter );
$this -> assertEquals ( $checkField . " 8 " , $result [ 0 ] -> $checkField );
2015-03-20 12:28:55 -04:00
}
private function iterateWithOrderDescWithLimitAndOffset ( $useOutputWalkers , $fetchJoinCollection , $baseDql , $checkField )
{
$dql = " $baseDql DESC " ;
$query = $this -> _em -> createQuery ( $dql );
2015-03-19 21:59:15 -04:00
// With offset
$query -> setMaxResults ( 3 ) -> setFirstResult ( 3 );
$paginator = new Paginator ( $query , $fetchJoinCollection );
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
$iter = $paginator -> getIterator ();
$this -> assertCount ( 3 , $iter );
$result = iterator_to_array ( $iter );
$this -> assertEquals ( $checkField . " 5 " , $result [ 0 ] -> $checkField );
}
/**
* @ dataProvider useOutputWalkersAndFetchJoinCollection
*/
public function testIterateSimpleWithoutJoinWithOrder ( $useOutputWalkers , $fetchJoinCollection )
{
// Ascending
2015-03-20 12:28:55 -04:00
$dql = 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username' ;
$this -> iterateWithOrderAsc ( $useOutputWalkers , $fetchJoinCollection , $dql , " username " );
$this -> iterateWithOrderDesc ( $useOutputWalkers , $fetchJoinCollection , $dql , " username " );
}
/**
* @ dataProvider useOutputWalkersAndFetchJoinCollection
*/
public function testIterateSimpleWithoutJoinWithOrderAndLimit ( $useOutputWalkers , $fetchJoinCollection )
{
// Ascending
$dql = 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username' ;
$this -> iterateWithOrderAscWithLimit ( $useOutputWalkers , $fetchJoinCollection , $dql , " username " );
$this -> iterateWithOrderDescWithLimit ( $useOutputWalkers , $fetchJoinCollection , $dql , " username " );
}
/**
* @ dataProvider useOutputWalkersAndFetchJoinCollection
*/
public function testIterateSimpleWithoutJoinWithOrderAndLimitAndOffset ( $useOutputWalkers , $fetchJoinCollection )
{
// Ascending
$dql = 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username' ;
$this -> iterateWithOrderAscWithLimitAndOffset ( $useOutputWalkers , $fetchJoinCollection , $dql , " username " );
$this -> iterateWithOrderDescWithLimitAndOffset ( $useOutputWalkers , $fetchJoinCollection , $dql , " username " );
2015-03-19 21:59:15 -04:00
}
/**
* @ dataProvider fetchJoinCollection
*/
public function testIterateSimpleWithOutputWalkerWithoutJoinWithComplexOrder ( $fetchJoinCollection )
{
// Ascending
2015-03-20 12:28:55 -04:00
$dql = 'SELECT l FROM Doctrine\Tests\Models\Pagination\Logo l ORDER BY l.image_width * l.image_height' ;
$this -> iterateWithOrderAsc ( true , $fetchJoinCollection , $dql , " image " );
$this -> iterateWithOrderDesc ( true , $fetchJoinCollection , $dql , " image " );
}
/**
* @ dataProvider fetchJoinCollection
*/
public function testIterateSimpleWithOutputWalkerWithoutJoinWithComplexOrderAndLimit ( $fetchJoinCollection )
{
// Ascending
$dql = 'SELECT l FROM Doctrine\Tests\Models\Pagination\Logo l ORDER BY l.image_width * l.image_height' ;
$this -> iterateWithOrderAscWithLimit ( true , $fetchJoinCollection , $dql , " image " );
$this -> iterateWithOrderDescWithLimit ( true , $fetchJoinCollection , $dql , " image " );
}
/**
* @ dataProvider fetchJoinCollection
*/
public function testIterateSimpleWithOutputWalkerWithoutJoinWithComplexOrderAndLimitAndOffset ( $fetchJoinCollection )
{
// Ascending
$dql = 'SELECT l FROM Doctrine\Tests\Models\Pagination\Logo l ORDER BY l.image_width * l.image_height' ;
$this -> iterateWithOrderAscWithLimitAndOffset ( true , $fetchJoinCollection , $dql , " image " );
$this -> iterateWithOrderDescWithLimitAndOffset ( true , $fetchJoinCollection , $dql , " image " );
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
{
2015-03-20 12:28:55 -04:00
$dql = 'SELECT u,g FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.groups g' ;
2012-01-22 13:35:06 +01:00
$query = $this -> _em -> createQuery ( $dql );
$paginator = new Paginator ( $query , true );
2012-03-12 08:33:35 +01:00
$paginator -> setUseOutputWalkers ( $useOutputWalkers );
2015-03-19 21:59:15 -04:00
$this -> assertCount ( 9 , $paginator -> getIterator ());
}
/**
* @ dataProvider useOutputWalkers
*/
public function testIterateWithFetchJoinWithOrder ( $useOutputWalkers )
{
$dql = 'SELECT u,g FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.groups g ORDER BY u.username' ;
2015-03-20 12:28:55 -04:00
$this -> iterateWithOrderAsc ( $useOutputWalkers , true , $dql , " username " );
$this -> iterateWithOrderDesc ( $useOutputWalkers , true , $dql , " username " );
}
/**
* @ dataProvider useOutputWalkers
*/
public function testIterateWithFetchJoinWithOrderAndLimit ( $useOutputWalkers )
{
$dql = 'SELECT u,g FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.groups g ORDER BY u.username' ;
$this -> iterateWithOrderAscWithLimit ( $useOutputWalkers , true , $dql , " username " );
$this -> iterateWithOrderDescWithLimit ( $useOutputWalkers , true , $dql , " username " );
}
/**
* @ dataProvider useOutputWalkers
*/
public function testIterateWithFetchJoinWithOrderAndLimitAndOffset ( $useOutputWalkers )
{
$dql = 'SELECT u,g FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.groups g ORDER BY u.username' ;
$this -> iterateWithOrderAscWithLimitAndOffset ( $useOutputWalkers , true , $dql , " username " );
$this -> iterateWithOrderDescWithLimitAndOffset ( $useOutputWalkers , true , $dql , " username " );
2015-03-19 21:59:15 -04:00
}
/**
* @ dataProvider useOutputWalkersAndFetchJoinCollection
*/
public function testIterateWithRegularJoinWithOrderByColumnFromJoined ( $useOutputWalkers , $fetchJoinCollection )
{
$dql = 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.email e ORDER BY e.email' ;
2015-03-20 12:28:55 -04:00
$this -> iterateWithOrderAsc ( $useOutputWalkers , $fetchJoinCollection , $dql , " username " );
$this -> iterateWithOrderDesc ( $useOutputWalkers , $fetchJoinCollection , $dql , " username " );
}
/**
* @ dataProvider useOutputWalkersAndFetchJoinCollection
*/
public function testIterateWithRegularJoinWithOrderByColumnFromJoinedWithLimit ( $useOutputWalkers , $fetchJoinCollection )
{
$dql = 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.email e ORDER BY e.email' ;
$this -> iterateWithOrderAscWithLimit ( $useOutputWalkers , $fetchJoinCollection , $dql , " username " );
$this -> iterateWithOrderDescWithLimit ( $useOutputWalkers , $fetchJoinCollection , $dql , " username " );
}
/**
* @ dataProvider useOutputWalkersAndFetchJoinCollection
*/
public function testIterateWithRegularJoinWithOrderByColumnFromJoinedWithLimitAndOffset ( $useOutputWalkers , $fetchJoinCollection )
{
$dql = 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.email e ORDER BY e.email' ;
$this -> iterateWithOrderAscWithLimitAndOffset ( $useOutputWalkers , $fetchJoinCollection , $dql , " username " );
$this -> iterateWithOrderDescWithLimitAndOffset ( $useOutputWalkers , $fetchJoinCollection , $dql , " username " );
2015-03-19 21:59:15 -04:00
}
/**
* @ dataProvider fetchJoinCollection
*/
public function testIterateWithOutputWalkersWithRegularJoinWithComplexOrderByReferencingJoined ( $fetchJoinCollection )
{
// long function name is loooooooooooong
2015-03-20 12:28:55 -04:00
$dql = 'SELECT c FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.logo l ORDER BY l.image_height * l.image_width' ;
$this -> iterateWithOrderAsc ( true , $fetchJoinCollection , $dql , " name " );
$this -> iterateWithOrderDesc ( true , $fetchJoinCollection , $dql , " name " );
}
/**
* @ dataProvider fetchJoinCollection
*/
public function testIterateWithOutputWalkersWithRegularJoinWithComplexOrderByReferencingJoinedWithLimit ( $fetchJoinCollection )
{
// long function name is loooooooooooong
$dql = 'SELECT c FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.logo l ORDER BY l.image_height * l.image_width' ;
$this -> iterateWithOrderAscWithLimit ( true , $fetchJoinCollection , $dql , " name " );
$this -> iterateWithOrderDescWithLimit ( true , $fetchJoinCollection , $dql , " name " );
}
/**
* @ dataProvider fetchJoinCollection
*/
public function testIterateWithOutputWalkersWithRegularJoinWithComplexOrderByReferencingJoinedWithLimitAndOffset ( $fetchJoinCollection )
{
// long function name is loooooooooooong
$dql = 'SELECT c FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.logo l ORDER BY l.image_height * l.image_width' ;
$this -> iterateWithOrderAscWithLimitAndOffset ( true , $fetchJoinCollection , $dql , " name " );
$this -> iterateWithOrderDescWithLimitAndOffset ( true , $fetchJoinCollection , $dql , " name " );
2015-03-19 21:59:15 -04:00
}
/**
* @ dataProvider useOutputWalkers
*/
public function testIterateWithFetchJoinWithOrderByColumnFromJoined ( $useOutputWalkers )
{
2015-03-20 12:28:55 -04:00
$dql = 'SELECT u,g,e FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.groups g JOIN u.email e ORDER BY e.email' ;
$this -> iterateWithOrderAsc ( $useOutputWalkers , true , $dql , " username " );
$this -> iterateWithOrderDesc ( $useOutputWalkers , true , $dql , " username " );
}
/**
* @ dataProvider useOutputWalkers
*/
public function testIterateWithFetchJoinWithOrderByColumnFromJoinedWithLimit ( $useOutputWalkers )
{
$dql = 'SELECT u,g,e FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.groups g JOIN u.email e ORDER BY e.email' ;
$this -> iterateWithOrderAscWithLimit ( $useOutputWalkers , true , $dql , " username " );
$this -> iterateWithOrderDescWithLimit ( $useOutputWalkers , true , $dql , " username " );
}
/**
* @ dataProvider useOutputWalkers
*/
public function testIterateWithFetchJoinWithOrderByColumnFromJoinedWithLimitAndOffset ( $useOutputWalkers )
{
$dql = 'SELECT u,g,e FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.groups g JOIN u.email e ORDER BY e.email' ;
$this -> iterateWithOrderAscWithLimitAndOffset ( $useOutputWalkers , true , $dql , " username " );
$this -> iterateWithOrderDescWithLimitAndOffset ( $useOutputWalkers , true , $dql , " username " );
2015-03-19 21:59:15 -04:00
}
/**
* @ dataProvider fetchJoinCollection
*/
public function testIterateWithOutputWalkersWithFetchJoinWithComplexOrderByReferencingJoined ( $fetchJoinCollection )
{
2015-03-20 12:28:55 -04:00
$dql = 'SELECT c,l FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.logo l ORDER BY l.image_width * l.image_height' ;
$this -> iterateWithOrderAsc ( true , $fetchJoinCollection , $dql , " name " );
$this -> iterateWithOrderDesc ( true , $fetchJoinCollection , $dql , " name " );
}
/**
* @ dataProvider fetchJoinCollection
*/
public function testIterateWithOutputWalkersWithFetchJoinWithComplexOrderByReferencingJoinedWithLimit ( $fetchJoinCollection )
{
$dql = 'SELECT c,l FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.logo l ORDER BY l.image_width * l.image_height' ;
$this -> iterateWithOrderAscWithLimit ( true , $fetchJoinCollection , $dql , " name " );
$this -> iterateWithOrderDescWithLimit ( true , $fetchJoinCollection , $dql , " name " );
}
/**
* @ dataProvider fetchJoinCollection
*/
public function testIterateWithOutputWalkersWithFetchJoinWithComplexOrderByReferencingJoinedWithLimitAndOffset ( $fetchJoinCollection )
{
$dql = 'SELECT c,l FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.logo l ORDER BY l.image_width * l.image_height' ;
$this -> iterateWithOrderAscWithLimitAndOffset ( true , $fetchJoinCollection , $dql , " name " );
$this -> iterateWithOrderDescWithLimitAndOffset ( true , $fetchJoinCollection , $dql , " name " );
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
{
2015-03-20 12:28:55 -04:00
$dql = 'SELECT g, COUNT(u.id) AS userCount FROM Doctrine\Tests\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 );
2015-03-19 21:59:15 -04:00
$this -> assertCount ( 3 , $paginator -> getIterator ());
2012-03-06 16:24:44 +01:00
}
2015-03-25 22:28:32 -04:00
/**
* @ dataProvider useOutputWalkers
*/
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromBoth ( $useOutputWalkers )
{
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY c.name' ;
$dqlAsc = $dql . " ASC, d.name " ;
$dqlDesc = $dql . " DESC, d.name " ;
$this -> iterateWithOrderAsc ( $useOutputWalkers , true , $dqlAsc , " name " );
$this -> iterateWithOrderDesc ( $useOutputWalkers , true , $dqlDesc , " name " );
}
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromBothWithLimitWithOutputWalker ()
{
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY c.name' ;
$dqlAsc = $dql . " ASC, d.name " ;
$dqlDesc = $dql . " DESC, d.name " ;
$this -> iterateWithOrderAscWithLimit ( true , true , $dqlAsc , " name " );
$this -> iterateWithOrderDescWithLimit ( true , true , $dqlDesc , " name " );
}
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromBothWithLimitWithoutOutputWalker ()
{
$this -> setExpectedException ( " RuntimeException " , " Cannot select distinct identifiers from query with LIMIT and ORDER BY on a joined entity. Use output walkers. " );
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY c.name ASC' ;
// Ascending
$query = $this -> _em -> createQuery ( $dql );
// With limit
$query -> setMaxResults ( 3 );
$paginator = new Paginator ( $query , true );
$paginator -> setUseOutputWalkers ( false );
$iter = $paginator -> getIterator ();
iterator_to_array ( $iter );
}
/**
* @ dataProvider useOutputWalkers
*/
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromRoot ( $useOutputWalkers )
{
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY c.name' ;
$this -> iterateWithOrderAsc ( $useOutputWalkers , true , $dql , " name " );
$this -> iterateWithOrderDesc ( $useOutputWalkers , true , $dql , " name " );
}
/**
* @ dataProvider useOutputWalkers
*/
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromRootWithLimit ( $useOutputWalkers )
{
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY c.name' ;
$this -> iterateWithOrderAscWithLimit ( $useOutputWalkers , true , $dql , " name " );
$this -> iterateWithOrderDescWithLimit ( $useOutputWalkers , true , $dql , " name " );
}
/**
* @ dataProvider useOutputWalkers
*/
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromRootWithLimitAndOffset ( $useOutputWalkers )
{
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY c.name' ;
$this -> iterateWithOrderAscWithLimitAndOffset ( $useOutputWalkers , true , $dql , " name " );
$this -> iterateWithOrderDescWithLimitAndOffset ( $useOutputWalkers , true , $dql , " name " );
}
/**
* @ dataProvider useOutputWalkers
*/
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromJoined ( $useOutputWalkers )
{
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY d.name' ;
$this -> iterateWithOrderAsc ( $useOutputWalkers , true , $dql , " name " );
$this -> iterateWithOrderDesc ( $useOutputWalkers , true , $dql , " name " );
}
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromJoinedWithLimitWithOutputWalker ()
{
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY d.name' ;
$this -> iterateWithOrderAscWithLimit ( true , true , $dql , " name " );
$this -> iterateWithOrderDescWithLimit ( true , true , $dql , " name " );
}
public function testIterateWithFetchJoinOneToManyWithOrderByColumnFromJoinedWithLimitWithoutOutputWalker ()
{
$this -> setExpectedException ( " RuntimeException " , " Cannot select distinct identifiers from query with LIMIT and ORDER BY on a joined entity. Use output walkers. " );
$dql = 'SELECT c, d FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.departments d ORDER BY d.name ASC' ;
// Ascending
$query = $this -> _em -> createQuery ( $dql );
// With limit
$query -> setMaxResults ( 3 );
$paginator = new Paginator ( $query , true );
$paginator -> setUseOutputWalkers ( false );
$iter = $paginator -> getIterator ();
iterator_to_array ( $iter );
}
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
2015-03-20 12:28:55 -04:00
$dql = 'SELECT g, COUNT(u.id) AS userCount FROM Doctrine\Tests\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 ()
{
2015-03-20 12:28:55 -04:00
$dql = 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' ;
2012-07-07 17:27:32 +02:00
$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 ()
{
2015-03-20 12:28:55 -04:00
$dql = 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' ;
2013-07-03 11:18:19 +00:00
$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
2014-11-27 17:50:27 +01:00
public function testCountQueryStripsParametersInSelect ()
2014-11-24 15:28:37 +01:00
{
$query = $this -> _em -> createQuery (
2014-11-27 17:54:02 +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 '
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 );
2014-11-27 17:50:27 +01:00
$getCountQuery = new ReflectionMethod ( $paginator , 'getCountQuery' );
$getCountQuery -> setAccessible ( true );
2014-11-27 17:57:42 +01:00
$this -> assertCount ( 2 , $getCountQuery -> invoke ( $paginator ) -> getParameters ());
2015-03-19 21:59:15 -04:00
$this -> assertCount ( 9 , $paginator );
2014-11-24 15:28:37 +01:00
$query -> setHint ( Query :: HINT_CUSTOM_OUTPUT_WALKER , 'Doctrine\ORM\Query\SqlWalker' );
2014-11-27 17:50:27 +01:00
2014-11-24 15:28:37 +01:00
$paginator = new Paginator ( $query );
2014-11-27 17:50:27 +01:00
2014-11-27 17:57:42 +01:00
// if select part of query is replaced with count(...) paginator should remove
// parameters from query object not used in new query.
$this -> assertCount ( 1 , $getCountQuery -> invoke ( $paginator ) -> getParameters ());
2015-03-19 21:59:15 -04:00
$this -> assertCount ( 9 , $paginator );
2014-11-24 15:28:37 +01:00
}
2013-07-03 11:18:19 +00:00
2012-01-22 13:35:06 +01:00
public function populate ()
{
2015-03-20 12:28:55 -04:00
$groups = array ();
2015-03-19 21:59:15 -04:00
for ( $j = 0 ; $j < 3 ; $j ++ ) {;
$group = new CmsGroup ();
$group -> name = " group $j " ;
$groups [] = $group ;
$this -> _em -> persist ( $group );
}
for ( $i = 0 ; $i < 9 ; $i ++ ) {
2012-01-22 13:35:06 +01:00
$user = new CmsUser ();
$user -> name = " Name $i " ;
$user -> username = " username $i " ;
$user -> status = " active " ;
2015-03-19 21:59:15 -04:00
$user -> email = new CmsEmail ();
$user -> email -> user = $user ;
$user -> email -> email = " email $i " ;
for ( $j = 0 ; $j < 3 ; $j ++ ) {
$user -> addGroup ( $groups [ $j ]);
}
2012-01-22 13:35:06 +01:00
$this -> _em -> persist ( $user );
2015-03-19 21:59:15 -04:00
}
2012-01-22 13:35:06 +01:00
2015-03-19 21:59:15 -04:00
for ( $i = 0 ; $i < 9 ; $i ++ ) {
$company = new Company ();
$company -> name = " name $i " ;
$company -> logo = new Logo ();
$company -> logo -> image = " image $i " ;
$company -> logo -> image_width = 100 + $i ;
$company -> logo -> image_height = 100 + $i ;
$company -> logo -> company = $company ;
2015-03-25 22:28:32 -04:00
for ( $j = 0 ; $j < 3 ; $j ++ ) {
$department = new Department ();
$department -> name = " name $i $j " ;
$department -> company = $company ;
$company -> departments [] = $department ;
}
2015-03-19 21:59:15 -04:00
$this -> _em -> persist ( $company );
2012-01-22 13:35:06 +01:00
}
2015-03-19 21:59:15 -04:00
2012-01-22 13:35:06 +01:00
$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 ),
);
}
2015-03-19 21:59:15 -04:00
public function fetchJoinCollection ()
{
return array (
array ( true ),
array ( false ),
);
}
public function useOutputWalkersAndFetchJoinCollection ()
{
return array (
array ( true , false ),
array ( true , true ),
array ( false , false ),
array ( false , true ),
);
}
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 );
}
}