diff --git a/tests/Doctrine/Tests/Models/Pagination/Company.php b/tests/Doctrine/Tests/Models/Pagination/Company.php new file mode 100644 index 000000000..2ff740d39 --- /dev/null +++ b/tests/Doctrine/Tests/Models/Pagination/Company.php @@ -0,0 +1,35 @@ +useModelSet('cms'); + $this->useModelSet('pagination'); parent::setUp(); $this->populate(); } @@ -35,7 +34,7 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase $paginator = new Paginator($query); $paginator->setUseOutputWalkers($useOutputWalkers); - $this->assertCount(3, $paginator); + $this->assertCount(9, $paginator); } /** @@ -48,7 +47,7 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase $paginator = new Paginator($query); $paginator->setUseOutputWalkers($useOutputWalkers); - $this->assertCount(3, $paginator); + $this->assertCount(9, $paginator); } public function testCountComplexWithOutputWalker() @@ -58,33 +57,144 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase $paginator = new Paginator($query); $paginator->setUseOutputWalkers(true); + $this->assertCount(3, $paginator); + } + + /** + * @expectedException + */ + public function testCountComplexWithoutOutputWalker() + { + $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"; + $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); + } + + /** + * @dataProvider useOutputWalkers + */ + public function testCountWithComplexScalarOrderBy($useOutputWalkers) + { + $dql = 'SELECT l FROM Doctrine\Tests\Models\Pagination\Logo l ORDER BY l.image_width * l.image_height DESC'; + $query = $this->_em->createQuery($dql); + + $paginator = new Paginator($query); + $paginator->setUseOutputWalkers($useOutputWalkers); $this->assertCount(9, $paginator); } /** - * @dataProvider useOutputWalkers + * @dataProvider useOutputWalkersAndFetchJoinCollection */ - public function testIterateSimpleWithoutJoinFetchJoinHandlingOff($useOutputWalkers) + public function testIterateSimpleWithoutJoin($useOutputWalkers, $fetchJoinCollection) { $dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u"; $query = $this->_em->createQuery($dql); - $paginator = new Paginator($query, false); + $paginator = new Paginator($query, $fetchJoinCollection); + $paginator->setUseOutputWalkers($useOutputWalkers); + $this->assertCount(9, $paginator->getIterator()); + + // Test with limit + $query->setMaxResults(3); + $paginator = new Paginator($query, $fetchJoinCollection); + $paginator->setUseOutputWalkers($useOutputWalkers); + $this->assertCount(3, $paginator->getIterator()); + + // Test with limit and offset + $query->setMaxResults(3)->setFirstResult(4); + $paginator = new Paginator($query, $fetchJoinCollection); $paginator->setUseOutputWalkers($useOutputWalkers); $this->assertCount(3, $paginator->getIterator()); } - /** - * @dataProvider useOutputWalkers - */ - public function testIterateSimpleWithoutJoinFetchJoinHandlingOn($useOutputWalkers) + private function iterateWithOrder($useOutputWalkers, $fetchJoinCollection, $baseDql, $checkField) { - $dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u"; + // Ascending + $dql = "$baseDql ASC"; $query = $this->_em->createQuery($dql); - $paginator = new Paginator($query, true); + $paginator = new Paginator($query, $fetchJoinCollection); $paginator->setUseOutputWalkers($useOutputWalkers); - $this->assertCount(3, $paginator->getIterator()); + $iter = $paginator->getIterator(); + $this->assertCount(9, $iter); + $result = iterator_to_array($iter); + $this->assertEquals($checkField . "0", $result[0]->$checkField); + + // 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); + + // 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); + + // Descending + $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); + + // 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); + + // 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 + $dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username"; + $this->iterateWithOrder($useOutputWalkers, $fetchJoinCollection, $dql, "username"); + } + + /** + * @dataProvider fetchJoinCollection + */ + public function testIterateSimpleWithOutputWalkerWithoutJoinWithComplexOrder($fetchJoinCollection) + { + // Ascending + $dql = "SELECT l FROM Doctrine\Tests\Models\Pagination\Logo l ORDER BY l.image_width * l.image_height"; + $this->iterateWithOrder(true, $fetchJoinCollection, $dql, "image"); } /** @@ -97,7 +207,54 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase $paginator = new Paginator($query, true); $paginator->setUseOutputWalkers($useOutputWalkers); - $this->assertCount(3, $paginator->getIterator()); + $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'; + $this->iterateWithOrder($useOutputWalkers, true, $dql, "username"); + } + + /** + * @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'; + $this->iterateWithOrder($useOutputWalkers, $fetchJoinCollection, $dql, "username"); + } + + /** + * @dataProvider fetchJoinCollection + */ + public function testIterateWithOutputWalkersWithRegularJoinWithComplexOrderByReferencingJoined($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->iterateWithOrder(true, $fetchJoinCollection, $dql, "name"); + } + + /** + * @dataProvider useOutputWalkers + */ + public function testIterateWithFetchJoinWithOrderByColumnFromJoined($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->iterateWithOrder($useOutputWalkers, true, $dql, "username"); + } + + /** + * @dataProvider fetchJoinCollection + */ + public function testIterateWithOutputWalkersWithFetchJoinWithComplexOrderByReferencingJoined($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->iterateWithOrder(true, $fetchJoinCollection, $dql, "name"); } public function testIterateComplexWithOutputWalker() @@ -107,7 +264,7 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase $paginator = new Paginator($query); $paginator->setUseOutputWalkers(true); - $this->assertCount(9, $paginator->getIterator()); + $this->assertCount(3, $paginator->getIterator()); } public function testDetectOutputWalker() @@ -170,7 +327,7 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase $getCountQuery->setAccessible(true); $this->assertCount(2, $getCountQuery->invoke($paginator)->getParameters()); - $this->assertCount(3, $paginator); + $this->assertCount(9, $paginator); $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Query\SqlWalker'); @@ -179,25 +336,44 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase // 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()); - $this->assertCount(3, $paginator); + $this->assertCount(9, $paginator); } public function populate() { - for ($i = 0; $i < 3; $i++) { + $groups = []; + 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++) { $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); + $user->email = new CmsEmail(); + $user->email->user = $user; + $user->email->email = "email$i"; + for ($j = 0; $j < 3; $j++) { + $user->addGroup($groups[$j]); } + $this->_em->persist($user); } + + 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; + $this->_em->persist($company); + } + $this->_em->flush(); } @@ -208,6 +384,24 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase array(false), ); } + + 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), + ); + } } class CustomPaginationTestTreeWalker extends Query\TreeWalkerAdapter diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index 6feb8ab97..c03a4d04a 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -267,6 +267,10 @@ abstract class OrmFunctionalTestCase extends OrmTestCase 'Doctrine\Tests\Models\CustomType\CustomIdObjectTypeParent', 'Doctrine\Tests\Models\CustomType\CustomIdObjectTypeChild', ), + 'pagination' => array( + 'Doctrine\Tests\Models\Pagination\Company', + 'Doctrine\Tests\Models\Pagination\Logo', + ), ); /** @@ -515,6 +519,11 @@ abstract class OrmFunctionalTestCase extends OrmTestCase $conn->executeUpdate('DELETE FROM custom_id_type_parent'); } + if (isset($this->_usedModelSets['pagination'])) { + $conn->executeUpdate('DELETE FROM pagination_logo'); + $conn->executeUpdate('DELETE FROM pagination_company'); + } + $this->_em->clear(); }