Completed
Push — master ( aa13e4...bbe005 )
by Marco
14s
created

DDC2602Test::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Event\LifecycleEventArgs;
7
use Doctrine\ORM\Events;
8
use Doctrine\Tests\OrmFunctionalTestCase;
9
10
/**
11
 * @group DDC-2602
12
 */
13
class DDC2602Test extends OrmFunctionalTestCase
14
{
15 View Code Duplication
    protected function setUp() : void
16
    {
17
        parent::setUp();
18
19
        $this->_schemaTool->createSchema(
20
            [
21
                $this->_em->getClassMetadata(DDC2602User::class),
22
                $this->_em->getClassMetadata(DDC2602Biography::class),
23
                $this->_em->getClassMetadata(DDC2602BiographyField::class),
24
                $this->_em->getClassMetadata(DDC2602BiographyFieldChoice::class),
25
            ]
26
        );
27
28
        $this->loadFixture();
29
    }
30
31 View Code Duplication
    protected function tearDown() : void
32
    {
33
        parent::tearDown();
34
35
        $this->_schemaTool->dropSchema(
36
            [
37
                $this->_em->getClassMetadata(DDC2602User::class),
38
                $this->_em->getClassMetadata(DDC2602Biography::class),
39
                $this->_em->getClassMetadata(DDC2602BiographyField::class),
40
                $this->_em->getClassMetadata(DDC2602BiographyFieldChoice::class),
41
            ]
42
        );
43
    }
44
45
    public function testPostLoadListenerShouldBeAbleToRunQueries() : void
46
    {
47
        $eventManager = $this->_em->getEventManager();
48
        $eventManager->addEventListener([Events::postLoad], new DDC2602PostLoadListener());
49
50
        $result = $this->_em->createQuery('SELECT u, b FROM Doctrine\Tests\ORM\Functional\Ticket\DDC2602User u JOIN u.biography b')
51
                             ->getResult();
52
53
        self::assertCount(2, $result);
54
        self::assertCount(2, $result[0]->biography->fieldList);
55
        self::assertCount(1, $result[1]->biography->fieldList);
56
    }
57
58
    private function loadFixture() : void
59
    {
60
        $user1                 = new DDC2602User();
61
        $user2                 = new DDC2602User();
62
        $biography1            = new DDC2602Biography();
63
        $biography2            = new DDC2602Biography();
64
        $biographyField1       = new DDC2602BiographyField();
65
        $biographyField2       = new DDC2602BiographyField();
66
        $biographyFieldChoice1 = new DDC2602BiographyFieldChoice();
67
        $biographyFieldChoice2 = new DDC2602BiographyFieldChoice();
68
        $biographyFieldChoice3 = new DDC2602BiographyFieldChoice();
69
        $biographyFieldChoice4 = new DDC2602BiographyFieldChoice();
70
        $biographyFieldChoice5 = new DDC2602BiographyFieldChoice();
71
        $biographyFieldChoice6 = new DDC2602BiographyFieldChoice();
72
73
        $user1->name = 'Gblanco';
74
        $user1->biography = $biography1;
75
76
        $user2->name = 'Beberlei';
77
        $user2->biography = $biography2;
78
79
        $biography1->user = $user1;
80
        $biography1->content = '[{"field": 1, "choiceList": [1,3]}, {"field": 2, "choiceList": [5]}]';
81
82
        $biography2->user = $user2;
83
        $biography2->content = '[{"field": 1, "choiceList": [1,2,3,4]}]';
84
85
        $biographyField1->alias = 'question_1';
86
        $biographyField1->label = 'Question 1';
87
        $biographyField1->choiceList->add($biographyFieldChoice1);
88
        $biographyField1->choiceList->add($biographyFieldChoice2);
89
        $biographyField1->choiceList->add($biographyFieldChoice3);
90
        $biographyField1->choiceList->add($biographyFieldChoice4);
91
92
        $biographyField2->alias = 'question_2';
93
        $biographyField2->label = 'Question 2';
94
        $biographyField2->choiceList->add($biographyFieldChoice5);
95
        $biographyField2->choiceList->add($biographyFieldChoice6);
96
97
        $biographyFieldChoice1->field = $biographyField1;
98
        $biographyFieldChoice1->label = 'Answer 1.1';
99
100
        $biographyFieldChoice2->field = $biographyField1;
101
        $biographyFieldChoice2->label = 'Answer 1.2';
102
103
        $biographyFieldChoice3->field = $biographyField1;
104
        $biographyFieldChoice3->label = 'Answer 1.3';
105
106
        $biographyFieldChoice4->field = $biographyField1;
107
        $biographyFieldChoice4->label = 'Answer 1.4';
108
109
        $biographyFieldChoice5->field = $biographyField2;
110
        $biographyFieldChoice5->label = 'Answer 2.1';
111
112
        $biographyFieldChoice6->field = $biographyField2;
113
        $biographyFieldChoice6->label = 'Answer 2.2';
114
115
        $this->_em->persist($user1);
116
        $this->_em->persist($user2);
117
118
        $this->_em->persist($biographyField1);
119
        $this->_em->persist($biographyField2);
120
121
        $this->_em->flush();
122
        $this->_em->clear();
123
    }
124
}
125
126
127
class DDC2602PostLoadListener
128
{
129
    public function postLoad(LifecycleEventArgs $event) : void
130
    {
131
        $entity = $event->getEntity();
132
133
        if ( ! ($entity instanceof DDC2602Biography)) {
134
            return;
135
        }
136
137
        $entityManager = $event->getEntityManager();
138
        $query         = $entityManager->createQuery('
139
            SELECT f, fc
140
              FROM Doctrine\Tests\ORM\Functional\Ticket\DDC2602BiographyField f INDEX BY f.id
141
              JOIN f.choiceList fc INDEX BY fc.id
142
        ');
143
144
        $result    = $query->getResult();
145
        $content   = json_decode($entity->content);
146
        $fieldList = new ArrayCollection();
147
148
        foreach ($content as $selection) {
149
            $field      = $result[$selection->field];
150
            $choiceList = $selection->choiceList;
151
            $fieldSelection     = new DDC2602FieldSelection();
152
153
            $fieldSelection->field      = $field;
154
            $fieldSelection->choiceList = $field->choiceList->filter(function ($choice) use ($choiceList) {
155
                return in_array($choice->id, $choiceList);
156
            });
157
158
            $fieldList->add($fieldSelection);
159
        }
160
161
        $entity->fieldList = $fieldList;
0 ignored issues
show
Documentation Bug introduced by
It seems like $fieldList of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type array of property $fieldList.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
162
    }
163
}
164
165
166
/**
167
 * @Entity
168
 */
169
class DDC2602User
170
{
171
    /**
172
     * @Id @GeneratedValue
173
     * @Column(type="integer")
174
     *
175
     * @var integer
176
     */
177
    public $id;
178
179
    /**
180
     * @Column(type="string", length=15)
181
     *
182
     * @var string
183
     */
184
    public $name;
185
186
    /**
187
     * @OneToOne(
188
     *     targetEntity="DDC2602Biography",
189
     *     inversedBy="user",
190
     *     cascade={"persist", "merge", "refresh", "remove"}
191
     * )
192
     * @JoinColumn(nullable=false)
193
     *
194
     * @var DDC2602Biography
195
     */
196
    public $biography;
197
}
198
199
/**
200
 * @Entity
201
 */
202
class DDC2602Biography
203
{
204
    /**
205
     * @Id @GeneratedValue
206
     * @Column(type="integer")
207
     *
208
     * @var integer
209
     */
210
    public $id;
211
212
    /**
213
     * @OneToOne(
214
     *     targetEntity="DDC2602User",
215
     *     mappedBy="biography",
216
     *     cascade={"persist", "merge", "refresh"}
217
     * )
218
     *
219
     * @var DDC2602User
220
     */
221
    public $user;
222
223
    /**
224
     * @Column(type="text", nullable=true)
225
     *
226
     * @var string
227
     */
228
    public $content;
229
230
    /**
231
     * @var array
232
     */
233
    public $fieldList = [];
234
}
235
236
/**
237
 * @Entity
238
 */
239
class DDC2602BiographyField
240
{
241
    /**
242
     * @Id @GeneratedValue
243
     * @Column(type="integer")
244
     *
245
     * @var integer
246
     */
247
    public $id;
248
249
    /**
250
     * @Column(type="string", unique=true, length=100)
251
     */
252
    public $alias;
253
254
    /**
255
     * @Column(type="string", length=100)
256
     */
257
    public $label;
258
259
    /**
260
     * @OneToMany(
261
     *     targetEntity="DDC2602BiographyFieldChoice",
262
     *     mappedBy="field",
263
     *     cascade={"persist", "merge", "refresh"}
264
     * )
265
     *
266
     * @var \Doctrine\Common\Collections\ArrayCollection
267
     */
268
    public $choiceList;
269
270
    /**
271
     * Constructor.
272
     *
273
     */
274
    public function __construct()
275
    {
276
        $this->choiceList = new ArrayCollection();
277
    }
278
}
279
280
/**
281
 * @Entity
282
 */
283
class DDC2602BiographyFieldChoice
284
{
285
    /**
286
     * @Id @GeneratedValue
287
     * @Column(type="integer")
288
     *
289
     * @var integer
290
     */
291
    public $id;
292
293
    /**
294
     * @Column(type="string", unique=true, length=100)
295
     */
296
    public $label;
297
298
    /**
299
     * @ManyToOne(
300
     *     targetEntity="DDC2602BiographyField",
301
     *     inversedBy="choiceList"
302
     * )
303
     * @JoinColumn(onDelete="CASCADE")
304
     *
305
     * @var DDC2602BiographyField
306
     */
307
    public $field;
308
}
309
310
class DDC2602FieldSelection
311
{
312
    /**
313
     * @var DDC2602BiographyField
314
     */
315
    public $field;
316
317
    /**
318
     * @var \Doctrine\Common\Collections\ArrayCollection
319
     */
320
    public $choiceList;
321
322
    /**
323
     * Constructor.
324
     *
325
     */
326
    public function __construct()
327
    {
328
        $this->choiceList = new ArrayCollection();
329
    }
330
}
331