Passed
Pull Request — 2.8.x (#8073)
by
unknown
06:47 queued 12s
created

testHavingLeftJoinsAreKept()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 18
rs 9.7998
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Tools\Pagination;
6
7
use Doctrine\ORM\Query;
8
use Doctrine\ORM\Tools\Pagination\RemoveUselessLeftJoinsWalker;
9
10
/**
11
 * @group DDC-1613
12
 */
13
class RemoveUselessLeftJoinWalkerTest extends PaginationTestCase
14
{
15
    public function testUselessLeftJoinsAreRemoved()
16
    {
17
        $dql        = <<<DQL
18
SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p 
19
LEFT JOIN p.category c 
20
LEFT JOIN p.author a
21
WHERE p.id IN (SELECT DISTINCT(p2.id) FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p2 
22
LEFT JOIN p2.category c2 
23
LEFT JOIN p2.author a2)
24
DQL;
25
        $query      = $this->entityManager->createQuery($dql);
26
        $limitQuery = clone $query;
27
28
        $limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [RemoveUselessLeftJoinsWalker::class]);
29
30
        $this->assertEquals(
31
            '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_)',
32
            $limitQuery->getSQL()
33
        );
34
    }
35
36
    public function testOrderByLeftJoinsAreKept()
37
    {
38
        $dql        = <<<DQL
39
SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p 
40
LEFT JOIN p.category c 
41
LEFT JOIN p.author a
42
WHERE p.id IN (SELECT DISTINCT(p2.id) FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p2 
43
LEFT JOIN p2.category c2 
44
LEFT JOIN p2.author a2 ORDER BY a2.id DESC)
45
DQL;
46
        $query      = $this->entityManager->createQuery($dql);
47
        $limitQuery = clone $query;
48
49
        $limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [RemoveUselessLeftJoinsWalker::class]);
50
51
        $this->assertEquals(
52
            '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)',
53
            $limitQuery->getSQL()
54
        );
55
    }
56
57
    public function testWhereLeftJoinsAreKept()
58
    {
59
        $dql        = <<<DQL
60
SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p 
61
LEFT JOIN p.category c 
62
LEFT JOIN p.author a
63
WHERE p.id IN (SELECT DISTINCT(p2.id) FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p2 
64
LEFT JOIN p2.category c2 
65
LEFT JOIN p2.author a2 WHERE 2 = a2.id)
66
DQL;
67
        $query      = $this->entityManager->createQuery($dql);
68
        $limitQuery = clone $query;
69
70
        $limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [RemoveUselessLeftJoinsWalker::class]);
71
72
        $this->assertEquals(
73
            '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)',
74
            $limitQuery->getSQL()
75
        );
76
    }
77
78
    public function testMultipleWhereLeftJoinsAreKept()
79
    {
80
        $dql        = <<<DQL
81
SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p 
82
LEFT JOIN p.category c 
83
LEFT JOIN p.author a
84
WHERE p.id IN (SELECT DISTINCT(p2.id) FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p2 
85
LEFT JOIN p2.category c2 
86
LEFT JOIN p2.author a2 WHERE 2 = a2.id AND c2.id = 1)
87
DQL;
88
        $query      = $this->entityManager->createQuery($dql);
89
        $limitQuery = clone $query;
90
91
        $limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [RemoveUselessLeftJoinsWalker::class]);
92
93
        $this->assertEquals(
94
            '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)',
95
            $limitQuery->getSQL()
96
        );
97
    }
98
99
    public function testHavingLeftJoinsAreKept()
100
    {
101
        $dql        = <<<DQL
102
SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p 
103
LEFT JOIN p.category c 
104
LEFT JOIN p.author a
105
WHERE p.id IN (SELECT DISTINCT(p2.id) FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p2 
106
LEFT JOIN p2.category c2 
107
LEFT JOIN p2.author a2 HAVING a2.id > 2)
108
DQL;
109
        $query      = $this->entityManager->createQuery($dql);
110
        $limitQuery = clone $query;
111
112
        $limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [RemoveUselessLeftJoinsWalker::class]);
113
114
        $this->assertEquals(
115
            '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 HAVING a4_.id > 2)',
116
            $limitQuery->getSQL()
117
        );
118
    }
119
}
120