2012-01-22 16:35:06 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Tools\Pagination;
|
|
|
|
|
|
|
|
use Doctrine\ORM\Query;
|
|
|
|
use Doctrine\ORM\Tools\Pagination\CountWalker;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-1613
|
|
|
|
*/
|
|
|
|
class CountWalkerTest extends PaginationTestCase
|
|
|
|
{
|
|
|
|
public function testCountQuery()
|
|
|
|
{
|
|
|
|
$query = $this->entityManager->createQuery(
|
2012-03-07 11:57:51 +04:00
|
|
|
'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p JOIN p.category c JOIN p.author a');
|
2012-01-22 16:35:06 +04:00
|
|
|
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\CountWalker'));
|
|
|
|
$query->setHint(CountWalker::HINT_DISTINCT, true);
|
|
|
|
$query->setFirstResult(null)->setMaxResults(null);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2012-03-07 11:57:51 +04:00
|
|
|
"SELECT count(DISTINCT b0_.id) AS sclr0 FROM BlogPost b0_ INNER JOIN Category c1_ ON b0_.category_id = c1_.id INNER JOIN Author a2_ ON b0_.author_id = a2_.id", $query->getSql()
|
2012-01-22 16:35:06 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCountQuery_MixedResultsWithName()
|
|
|
|
{
|
|
|
|
$query = $this->entityManager->createQuery(
|
2012-03-07 11:57:51 +04:00
|
|
|
'SELECT a, sum(a.name) as foo FROM Doctrine\Tests\ORM\Tools\Pagination\Author a');
|
2012-01-22 16:35:06 +04:00
|
|
|
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\CountWalker'));
|
|
|
|
$query->setHint(CountWalker::HINT_DISTINCT, true);
|
|
|
|
$query->setFirstResult(null)->setMaxResults(null);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2012-03-07 11:57:51 +04:00
|
|
|
"SELECT count(DISTINCT a0_.id) AS sclr0 FROM Author a0_", $query->getSql()
|
2012-01-22 16:35:06 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCountQuery_KeepsGroupBy()
|
|
|
|
{
|
|
|
|
$query = $this->entityManager->createQuery(
|
2012-03-07 11:57:51 +04:00
|
|
|
'SELECT b FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost b GROUP BY b.id');
|
2012-01-22 16:35:06 +04:00
|
|
|
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\CountWalker'));
|
|
|
|
$query->setHint(CountWalker::HINT_DISTINCT, true);
|
|
|
|
$query->setFirstResult(null)->setMaxResults(null);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2012-03-07 11:57:51 +04:00
|
|
|
"SELECT count(DISTINCT b0_.id) AS sclr0 FROM BlogPost b0_ GROUP BY b0_.id", $query->getSql()
|
2012-01-22 16:35:06 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCountQuery_RemovesOrderBy()
|
|
|
|
{
|
|
|
|
$query = $this->entityManager->createQuery(
|
2012-03-07 11:57:51 +04:00
|
|
|
'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p JOIN p.category c JOIN p.author a ORDER BY a.name');
|
2012-01-22 16:35:06 +04:00
|
|
|
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\CountWalker'));
|
|
|
|
$query->setHint(CountWalker::HINT_DISTINCT, true);
|
|
|
|
$query->setFirstResult(null)->setMaxResults(null);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2012-03-07 11:57:51 +04:00
|
|
|
"SELECT count(DISTINCT b0_.id) AS sclr0 FROM BlogPost b0_ INNER JOIN Category c1_ ON b0_.category_id = c1_.id INNER JOIN Author a2_ ON b0_.author_id = a2_.id", $query->getSql()
|
2012-01-22 16:35:06 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCountQuery_RemovesLimits()
|
|
|
|
{
|
|
|
|
$query = $this->entityManager->createQuery(
|
2012-03-07 11:57:51 +04:00
|
|
|
'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p JOIN p.category c JOIN p.author a');
|
2012-01-22 16:35:06 +04:00
|
|
|
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\CountWalker'));
|
|
|
|
$query->setHint(CountWalker::HINT_DISTINCT, true);
|
|
|
|
$query->setFirstResult(null)->setMaxResults(null);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2012-03-07 11:57:51 +04:00
|
|
|
"SELECT count(DISTINCT b0_.id) AS sclr0 FROM BlogPost b0_ INNER JOIN Category c1_ ON b0_.category_id = c1_.id INNER JOIN Author a2_ ON b0_.author_id = a2_.id", $query->getSql()
|
2012-01-22 16:35:06 +04:00
|
|
|
);
|
|
|
|
}
|
2012-03-07 11:42:09 +04:00
|
|
|
|
|
|
|
public function testCountQuery_HavingException()
|
|
|
|
{
|
|
|
|
$query = $this->entityManager->createQuery(
|
|
|
|
"SELECT g, COUNT(u.id) AS userCount FROM Doctrine\Tests\Models\CMS\CmsGroup g LEFT JOIN g.users u GROUP BY g.id HAVING userCount > 0"
|
|
|
|
);
|
|
|
|
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\CountWalker'));
|
|
|
|
$query->setFirstResult(null)->setMaxResults(null);
|
|
|
|
|
|
|
|
$this->setExpectedException(
|
|
|
|
'RuntimeException',
|
2012-03-12 11:33:35 +04:00
|
|
|
'Cannot count query that uses a HAVING clause. Use the output walkers for pagination'
|
2012-03-07 11:42:09 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
$query->getSql();
|
|
|
|
}
|
2012-01-22 16:35:06 +04:00
|
|
|
}
|
|
|
|
|