Failed Conditions
Pull Request — 2.8.x (#8073)
by
unknown
61:56
created

testUselessLeftJoinsAreRemoved()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 18
rs 9.7998
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Tools\Pagination;
4
5
use Doctrine\ORM\Query;
6
use Doctrine\ORM\Tools\Pagination\LimitSubqueryWalker;
7
use Doctrine\ORM\Tools\Pagination\RemoveUselessLeftJoinsWalker;
8
9
/**
10
 * @group DDC-1613
11
 */
12
class RemoveUselessLeftJoinWalkerTest extends PaginationTestCase
13
{
14
    public function testUselessLeftJoinsAreRemoved()
15
    {
16
        $dql        = <<<DQL
17
SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p 
18
LEFT JOIN p.category c 
19
LEFT JOIN p.author a
20
WHERE p.id IN (SELECT DISTINCT(p2.id) FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p2 
21
LEFT JOIN p2.category c2 
22
LEFT JOIN p2.author a2)
23
DQL;
24
        $query      = $this->entityManager->createQuery($dql);
25
        $limitQuery = clone $query;
26
27
        $limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [RemoveUselessLeftJoinsWalker::class]);
28
29
        $this->assertEquals(
30
            "SELECT m0_.id AS id_0, m0_.title AS title_1, c1_.id AS id_2, a2_.id AS id_3, a2_.name AS name_4, m0_.author_id AS author_id_5, m0_.category_id AS category_id_6 FROM MyBlogPost m0_ WHERE m0_.id IN (SELECT DISTINCT (m3_.id) FROM MyBlogPost m3_)",
31
            $limitQuery->getSQL()
32
        );
33
    }
34
35
    public function testOrderByLeftJoinsAreKept()
36
    {
37
        $dql        = <<<DQL
38
SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p 
39
LEFT JOIN p.category c 
40
LEFT JOIN p.author a
41
WHERE p.id IN (SELECT DISTINCT(p2.id) FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p2 
42
LEFT JOIN p2.category c2 
43
LEFT JOIN p2.author a2 ORDER BY a2.id DESC)
44
DQL;
45
        $query      = $this->entityManager->createQuery($dql);
46
        $limitQuery = clone $query;
47
48
        $limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [RemoveUselessLeftJoinsWalker::class]);
49
50
        $this->assertEquals(
51
            "SELECT m0_.id AS id_0, m0_.title AS title_1, c1_.id AS id_2, a2_.id AS id_3, a2_.name AS name_4, m0_.author_id AS author_id_5, m0_.category_id AS category_id_6 FROM MyBlogPost m0_ WHERE m0_.id IN (SELECT DISTINCT (m3_.id) FROM MyBlogPost m3_ LEFT JOIN Author a4_ ON m3_.author_id = a4_.id ORDER BY a4_.id DESC)",
52
            $limitQuery->getSQL()
53
        );
54
    }
55
56
    public function testWhereLeftJoinsAreKept()
57
    {
58
        $dql        = <<<DQL
59
SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p 
60
LEFT JOIN p.category c 
61
LEFT JOIN p.author a
62
WHERE p.id IN (SELECT DISTINCT(p2.id) FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p2 
63
LEFT JOIN p2.category c2 
64
LEFT JOIN p2.author a2 WHERE 2 = a2.id)
65
DQL;
66
        $query      = $this->entityManager->createQuery($dql);
67
        $limitQuery = clone $query;
68
69
        $limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [RemoveUselessLeftJoinsWalker::class]);
70
71
        $this->assertEquals(
72
            "SELECT m0_.id AS id_0, m0_.title AS title_1, c1_.id AS id_2, a2_.id AS id_3, a2_.name AS name_4, m0_.author_id AS author_id_5, m0_.category_id AS category_id_6 FROM MyBlogPost m0_ WHERE m0_.id IN (SELECT DISTINCT (m3_.id) FROM MyBlogPost m3_ LEFT JOIN Author a4_ ON m3_.author_id = a4_.id WHERE 2 = a4_.id)",
73
            $limitQuery->getSQL()
74
        );
75
    }
76
77
    public function testMultipleWhereLeftJoinsAreKept()
78
    {
79
        $dql        = <<<DQL
80
SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p 
81
LEFT JOIN p.category c 
82
LEFT JOIN p.author a
83
WHERE p.id IN (SELECT DISTINCT(p2.id) FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p2 
84
LEFT JOIN p2.category c2 
85
LEFT JOIN p2.author a2 WHERE 2 = a2.id AND c2.id = 1)
86
DQL;
87
        $query      = $this->entityManager->createQuery($dql);
88
        $limitQuery = clone $query;
89
90
        $limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [RemoveUselessLeftJoinsWalker::class]);
91
92
        $this->assertEquals(
93
            "SELECT m0_.id AS id_0, m0_.title AS title_1, c1_.id AS id_2, a2_.id AS id_3, a2_.name AS name_4, m0_.author_id AS author_id_5, m0_.category_id AS category_id_6 FROM MyBlogPost m0_ WHERE m0_.id IN (SELECT DISTINCT (m3_.id) FROM MyBlogPost m3_ LEFT JOIN Category c4_ ON m3_.category_id = c4_.id LEFT JOIN Author a5_ ON m3_.author_id = a5_.id WHERE 2 = a5_.id AND c4_.id = 1)",
94
            $limitQuery->getSQL()
95
        );
96
    }
97
}
98