From 164352562bac6e061f33f6696adc4ad2b0fb7062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Bere=C5=88?= Date: Thu, 31 Jul 2014 08:31:39 +0200 Subject: [PATCH] Added Tests for Arbitrary Join --- .../ORM/Tools/Pagination/CountWalkerTest.php | 16 ++++++++++ .../Pagination/LimitSubqueryWalkerTest.php | 31 +++++++++++++++++++ .../Tools/Pagination/WhereInWalkerTest.php | 29 +++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php index 4ef4d1630..7f36e7eae 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php @@ -90,5 +90,21 @@ class CountWalkerTest extends PaginationTestCase $query->getSql(); } + + /** + * Arbitrary Join + */ + public function testCountQueryWithArbitraryJoin() + { + $query = $this->entityManager->createQuery( + 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p LEFT JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH b.category = c'); + $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( + "SELECT count(DISTINCT b0_.id) AS sclr_0 FROM BlogPost b0_ LEFT JOIN Category c1_ ON b0_.category_id = c1_.id", $query->getSql() + ); + } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php index 1bb2b6463..2e8d9160c 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryWalkerTest.php @@ -67,5 +67,36 @@ class LimitSubqueryWalkerTest extends PaginationTestCase $limitQuery->getSql() ); } + + /** + * Arbitrary Join + */ + public function testLimitSubqueryWithArbitraryJoin() + { + $dql = 'SELECT p, c FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH b.category = c'; + $query = $this->entityManager->createQuery($dql); + $limitQuery = clone $query; + + $limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\LimitSubqueryWalker')); + + $this->assertEquals( + "SELECT DISTINCT m0_.id AS id_0 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id", + $limitQuery->getSql() + ); + } + + public function testLimitSubqueryWithSortWithArbitraryJoin() + { + $dql = 'SELECT p, c FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH b.category = c ORDER BY p.title'; + $query = $this->entityManager->createQuery($dql); + $limitQuery = clone $query; + + $limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\LimitSubqueryWalker')); + + $this->assertEquals( + "SELECT DISTINCT m0_.id AS id_0, m0_.title AS title_1 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id ORDER BY m0_.title ASC", + $limitQuery->getSql() + ); + } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php index 7291c2786..ba7305ba9 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/WhereInWalkerTest.php @@ -121,5 +121,34 @@ class WhereInWalkerTest extends PaginationTestCase "SELECT u0_.id AS id_0, g1_.id AS id_1 FROM User u0_ INNER JOIN user_group u2_ ON u0_.id = u2_.user_id INNER JOIN groups g1_ ON g1_.id = u2_.group_id WHERE (NOT 1 = 2) AND u0_.id IN (?)", $whereInQuery->getSql() ); } + + /** + * Arbitrary Join + */ + public function testWhereInQueryWithArbitraryJoin_NoWhere() + { + $whereInQuery = $this->entityManager->createQuery( + 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH b.category = c' + ); + $whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\WhereInWalker')); + $whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); + + $this->assertEquals( + "SELECT b0_.id AS id_0 FROM BlogPost b0_ INNER JOIN Category c1_ ON b0_.category_id = c1_.id WHERE b0_.id IN (?)", $whereInQuery->getSql() + ); + } + + public function testWhereInQueryWithArbitraryJoin_SingleWhere() + { + $whereInQuery = $this->entityManager->createQuery( + 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p JOIN Doctrine\Tests\ORM\Tools\Pagination\Category c WITH b.category = c WHERE 1 = 1' + ); + $whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\WhereInWalker')); + $whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, 10); + + $this->assertEquals( + "SELECT b0_.id AS id_0 FROM BlogPost b0_ INNER JOIN Category c1_ ON b0_.category_id = c1_.id WHERE 1 = 1 AND b0_.id IN (?)", $whereInQuery->getSql() + ); + } }