Completed
Pull Request — 2.7 (#8058)
by Peter
08:17
created

GH5544Test::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 5
c 2
b 0
f 1
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\ORM\Query;
6
use Doctrine\Tests\OrmFunctionalTestCase;
7
8
/**
9
 * @group 5544
10
 */
11
class GH5544Test extends OrmFunctionalTestCase
12
{
13
    protected function setUp() : void
14
    {
15
        parent::setUp();
16
17
        $this->_schemaTool->createSchema([
18
            $this->_em->getClassMetadata(GH5544User::class),
19
            $this->_em->getClassMetadata(GH5544UserBrowser::class),
20
            $this->_em->getClassMetadata(GH5544BrowserGroup::class),
21
        ]);
22
    }
23
24
    public function testIssue() : void
25
    {
26
        $this->createScalarIdentifierData();
27
        $this->createEntityIdentifierData();
28
        $initialQueryCount = $this->getCurrentQueryCount();
29
30
        $query = $this->createScalarIdentifierQuery(true);
31
        self::assertSame(2, (int) $query->getSingleScalarResult());
32
        self::assertEquals($initialQueryCount + 1, $this->getCurrentQueryCount());
33
34
        $query = $this->createScalarIdentifierQuery(false);
35
        self::assertSame(2, (int) $query->getSingleScalarResult());
36
        self::assertEquals($initialQueryCount + 2, $this->getCurrentQueryCount());
37
38
        $query = $this->createEntityIdentifierQuery(true);
39
        self::assertSame(2, (int) $query->getSingleScalarResult());
40
        self::assertEquals($initialQueryCount + 3, $this->getCurrentQueryCount());
41
42
        $query = $this->createEntityIdentifierQuery(false);
43
        self::assertSame(2, (int) $query->getSingleScalarResult());
44
        self::assertEquals($initialQueryCount + 4, $this->getCurrentQueryCount());
45
    }
46
47
    private function createScalarIdentifierQuery(bool $distinct) : Query
48
    {
49
        return $this->_em
50
            ->createQueryBuilder()
51
            ->select(\sprintf(
52
                'COUNT(%s CONCAT(bg.os, :concat_separator, bg.browser)) cnt',
53
                $distinct ? 'DISTINCT' : ''
54
            ))
55
            ->from(GH5544BrowserGroup::class, 'bg')
56
            ->setParameter('concat_separator', '|')
57
            ->getQuery();
58
    }
59
60
    private function createScalarIdentifierData() : void
61
    {
62
        $this->_em->persist(new GH5544BrowserGroup('Windows', 'Google Chrome', '80.0.3987.122'));
63
        $this->_em->persist(new GH5544BrowserGroup('Ubuntu', 'Mozilla FireFox', '73.0.1'));
64
        $this->_em->flush();
65
        $this->_em->clear();
66
    }
67
68
    private function createEntityIdentifierQuery(bool $distinct) : Query
69
    {
70
        return $this->_em
71
            ->createQueryBuilder()
72
            ->select(\sprintf(
73
                'COUNT(%s CONCAT(ub.user.id, :concat_separator, ub.browser)) cnt',
74
                $distinct ? 'DISTINCT' : ''
75
            ))
76
            ->from(GH5544UserBrowser::class, 'ub')
77
            ->setParameter('concat_separator', '|')
78
            ->getQuery();
79
    }
80
81
    private function createEntityIdentifierData() : void
82
    {
83
        $user = new GH5544User(12345);
84
        $this->_em->persist($user);
85
        $this->_em->persist(new GH5544UserBrowser($user, 'Google Chrome'));
86
        $this->_em->persist(new GH5544UserBrowser($user, 'Mozilla FireFox'));
87
        $this->_em->flush();
88
        $this->_em->clear();
89
    }
90
}
91
92
/**
93
 * @Entity
94
 * @Table(name="GH5544_user")
95
 */
96
class GH5544User
97
{
98
    /**
99
     * @Id
100
     * @GeneratedValue("NONE")
101
     * @Column(type="integer")
102
     */
103
    public $id;
104
105
    public function __construct(int $id)
106
    {
107
        $this->id = $id;
108
    }
109
110
}
111
112
/**
113
 * @Entity
114
 * @Table(name="GH5544_user_browser")
115
 */
116
class GH5544UserBrowser
117
{
118
    /**
119
     * @ORM\Id()
120
     * @ORM\GeneratedValue("NONE")
121
     * @ORM\ManyToOne(targetEntity="User")
122
     * @ORM\JoinColumn(referencedColumnName="id", nullable=false)
123
     */
124
    public $user;
125
126
    /**
127
     * @Id
128
     * @GeneratedValue("NONE")
129
     * @Column(type="string", length=64)
130
     */
131
    public $browser;
132
133
    public function __construct(GH5544User $user, string $browser)
134
    {
135
        $this->user = $user;
136
        $this->browser = $browser;
137
    }
138
}
139
140
/**
141
 * @Entity
142
 * @Table(name="GH5544_browser_group")
143
 */
144
class GH5544BrowserGroup
145
{
146
    /**
147
     * @ORM\Id()
148
     * @ORM\GeneratedValue("NONE")
149
     * @Column(type="string", length=64)
150
     */
151
    public $os;
152
153
    /**
154
     * @Id
155
     * @GeneratedValue("NONE")
156
     * @Column(type="string", length=64)
157
     */
158
    public $browser;
159
160
    /**
161
     * @Column(type="string", length=64)
162
     */
163
    public $version;
164
165
    public function __construct(string $os, string $browser, string $version)
166
    {
167
        $this->os = $os;
168
        $this->browser = $browser;
169
        $this->version = $version;
170
    }
171
}
172