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

GH5544UserBrowser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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