Failed Conditions
Pull Request — develop (#6873)
by
unknown
112:44 queued 47:41
created

testGetNonExistentIndexBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional;
6
7
use Doctrine\ORM\Mapping\ClassMetadata;
8
use Doctrine\ORM\Mapping\FetchMode;
9
use Doctrine\Tests\Models\CMS\CmsArticle;
10
use Doctrine\Tests\Models\CMS\CmsGroup;
11
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
12
use Doctrine\Tests\Models\CMS\CmsUser;
13
use Doctrine\Tests\Models\DDC2504\DDC2504ChildClass;
14
use Doctrine\Tests\Models\DDC2504\DDC2504OtherClass;
15
use Doctrine\Tests\Models\Tweet\Tweet;
16
use Doctrine\Tests\Models\Tweet\User;
17
use Doctrine\Tests\Models\Tweet\UserList;
18
use Doctrine\Tests\OrmFunctionalTestCase;
19
20
/**
21
 * Description of ExtraLazyCollectionTest
22
 *
23
 * @author beberlei
24
 */
25
class ExtraLazyCollectionTest extends OrmFunctionalTestCase
26
{
27
    private $userId;
28
    private $userId2;
29
    private $groupId;
30
    private $articleId;
31
    private $ddc2504OtherClassId;
32
    private $ddc2504ChildClassId;
33
34
    private $username;
35
    private $groupname;
36
    private $topic;
37
    private $phonenumber;
38
39
    public function setUp()
40
    {
41
        $this->useModelSet('tweet');
42
        $this->useModelSet('cms');
43
        $this->useModelSet('ddc2504');
44
        parent::setUp();
45
46
        $class       = $this->em->getClassMetadata(CmsUser::class);
47
48
        $class->getProperty('groups')->setFetchMode(FetchMode::EXTRA_LAZY);
0 ignored issues
show
Bug introduced by
The method getProperty() does not exist on Doctrine\Common\Persistence\Mapping\ClassMetadata. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        $class->/** @scrutinizer ignore-call */ 
49
                getProperty('groups')->setFetchMode(FetchMode::EXTRA_LAZY);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
        $class->getProperty('articles')->setFetchMode(FetchMode::EXTRA_LAZY);
50
        $class->getProperty('phonenumbers')->setFetchMode(FetchMode::EXTRA_LAZY);
51
52
        $class->getProperty('groups')->setIndexedBy('name');
53
        $class->getProperty('articles')->setIndexedBy('topic');
54
        $class->getProperty('phonenumbers')->setIndexedBy('phonenumber');
55
56
        $class->getProperty('groups')->setCache(null);
57
        $class->getProperty('articles')->setCache(null);
58
        $class->getProperty('phonenumbers')->setCache(null);
59
60
        $class = $this->em->getClassMetadata(CmsGroup::class);
61
62
        $class->getProperty('users')->setFetchMode(FetchMode::EXTRA_LAZY);
63
64
        $class->getProperty('users')->setIndexedBy('username');
65
66
        $this->loadFixture();
67
    }
68
69
    public function tearDown()
70
    {
71
        parent::tearDown();
72
73
        $class = $this->em->getClassMetadata(CmsUser::class);
74
75
        $class->getProperty('groups')->setFetchMode(FetchMode::LAZY);
76
        $class->getProperty('articles')->setFetchMode(FetchMode::LAZY);
77
        $class->getProperty('phonenumbers')->setFetchMode(FetchMode::LAZY);
78
79
        $class->getProperty('groups')->setIndexedBy(null);
80
        $class->getProperty('articles')->setIndexedBy(null);
81
        $class->getProperty('phonenumbers')->setIndexedBy(null);
82
83
        $class = $this->em->getClassMetadata(CmsGroup::class);
84
85
        $class->getProperty('users')->setFetchMode(FetchMode::LAZY);
86
87
        $class->getProperty('users')->setIndexedBy(null);
88
    }
89
90
    /**
91
     * @group DDC-546
92
     * @group non-cacheable
93
     */
94 View Code Duplication
    public function testCountNotInitializesCollection()
95
    {
96
        $user = $this->em->find(CmsUser::class, $this->userId);
97
        $queryCount = $this->getCurrentQueryCount();
98
99
        self::assertFalse($user->groups->isInitialized());
0 ignored issues
show
Bug introduced by
The method isInitialized() does not exist on Doctrine\Common\Collections\ArrayCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        self::assertFalse($user->groups->/** @scrutinizer ignore-call */ isInitialized());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
        self::assertCount(3, $user->groups);
101
        self::assertFalse($user->groups->isInitialized());
0 ignored issues
show
Bug introduced by
The method isInitialized() does not exist on Countable. It seems like you code against a sub-type of Countable such as Doctrine\Common\Collections\AbstractLazyCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
        self::assertFalse($user->groups->/** @scrutinizer ignore-call */ isInitialized());
Loading history...
Bug introduced by
The method isInitialized() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as Doctrine\Common\Collections\AbstractLazyCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
        self::assertFalse($user->groups->/** @scrutinizer ignore-call */ isInitialized());
Loading history...
102
103
        foreach ($user->groups AS $group) { }
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
104
105
        self::assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration.");
106
    }
107
108
    /**
109
     * @group DDC-546
110
     */
111
    public function testCountWhenNewEntityPresent()
112
    {
113
        $user = $this->em->find(CmsUser::class, $this->userId);
114
115
        $newGroup = new CmsGroup();
116
        $newGroup->name = "Test4";
117
118
        $user->addGroup($newGroup);
119
        $this->em->persist($newGroup);
120
121
        self::assertFalse($user->groups->isInitialized());
122
        self::assertCount(4, $user->groups);
123
        self::assertFalse($user->groups->isInitialized());
124
    }
125
126
    /**
127
     * @group DDC-546
128
     * @group non-cacheable
129
     */
130
    public function testCountWhenInitialized()
131
    {
132
        $user = $this->em->find(CmsUser::class, $this->userId);
133
        $queryCount = $this->getCurrentQueryCount();
134
135
        foreach ($user->groups AS $group) { }
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
136
137
        self::assertTrue($user->groups->isInitialized());
138
        self::assertCount(3, $user->groups);
139
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Should only execute one query to initialize collection, no extra query for count() more.");
140
    }
141
142
    /**
143
     * @group DDC-546
144
     */
145
    public function testCountInverseCollection()
146
    {
147
        $group = $this->em->find(CmsGroup::class, $this->groupId);
148
        self::assertFalse($group->users->isInitialized(), "Pre-Condition");
149
150
        self::assertCount(4, $group->users);
151
        self::assertFalse($group->users->isInitialized(), "Extra Lazy collection should not be initialized by counting the collection.");
152
    }
153
154
    /**
155
     * @group DDC-546
156
     */
157 View Code Duplication
    public function testCountOneToMany()
158
    {
159
        $user = $this->em->find(CmsUser::class, $this->userId);
160
        self::assertFalse($user->groups->isInitialized(), "Pre-Condition");
161
162
        self::assertCount(2, $user->articles);
163
    }
164
165
    /**
166
     * @group DDC-2504
167
     */
168 View Code Duplication
    public function testCountOneToManyJoinedInheritance()
169
    {
170
        $otherClass = $this->em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
171
172
        self::assertFalse($otherClass->childClasses->isInitialized(), "Pre-Condition");
173
        self::assertCount(2, $otherClass->childClasses);
174
    }
175
176
    /**
177
     * @group DDC-546
178
     */
179 View Code Duplication
    public function testFullSlice()
180
    {
181
        $user = $this->em->find(CmsUser::class, $this->userId);
182
        self::assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
183
184
        $someGroups = $user->groups->slice(null);
185
        self::assertCount(3, $someGroups);
186
    }
187
188
    /**
189
     * @group DDC-546
190
     * @group non-cacheable
191
     */
192
    public function testSlice()
193
    {
194
        $user = $this->em->find(CmsUser::class, $this->userId);
195
        self::assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
196
197
        $queryCount = $this->getCurrentQueryCount();
198
199
        $someGroups = $user->groups->slice(0, 2);
200
201
        self::assertContainsOnly(CmsGroup::class, $someGroups);
202
        self::assertCount(2, $someGroups);
203
        self::assertFalse($user->groups->isInitialized(), "Slice should not initialize the collection if it wasn't before!");
204
205
        $otherGroup = $user->groups->slice(2, 1);
206
207
        self::assertContainsOnly(CmsGroup::class, $otherGroup);
208
        self::assertCount(1, $otherGroup);
209
        self::assertFalse($user->groups->isInitialized());
210
211
        foreach ($user->groups AS $group) { }
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
212
213
        self::assertTrue($user->groups->isInitialized());
214
        self::assertCount(3, $user->groups);
215
216
        self::assertEquals($queryCount + 3, $this->getCurrentQueryCount());
217
    }
218
219
    /**
220
     * @group DDC-546
221
     * @group non-cacheable
222
     */
223
    public function testSliceInitializedCollection()
224
    {
225
        $user = $this->em->find(CmsUser::class, $this->userId);
226
        $queryCount = $this->getCurrentQueryCount();
227
228
        foreach ($user->groups AS $group) { }
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
229
230
        $someGroups = $user->groups->slice(0, 2);
231
232
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount());
233
234
        self::assertCount(2, $someGroups);
235
        self::assertTrue($user->groups->contains(array_shift($someGroups)));
236
        self::assertTrue($user->groups->contains(array_shift($someGroups)));
237
    }
238
239
    /**
240
     * @group DDC-546
241
     */
242
    public function testSliceInverseCollection()
243
    {
244
        $group = $this->em->find(CmsGroup::class, $this->groupId);
245
        self::assertFalse($group->users->isInitialized(), "Pre-Condition");
246
        $queryCount = $this->getCurrentQueryCount();
247
248
        $someUsers = $group->users->slice(0, 2);
249
        $otherUsers = $group->users->slice(2, 2);
250
251
        self::assertContainsOnly(CmsUser::class, $someUsers);
252
        self::assertContainsOnly(CmsUser::class, $otherUsers);
253
        self::assertCount(2, $someUsers);
254
        self::assertCount(2, $otherUsers);
255
256
        // +2 queries executed by slice
257
        self::assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Slicing two parts should only execute two additional queries.");
258
    }
259
260
    /**
261
     * @group DDC-546
262
     */
263
    public function testSliceOneToMany()
264
    {
265
        $user = $this->em->find(CmsUser::class, $this->userId);
266
        self::assertFalse($user->articles->isInitialized(), "Pre-Condition: Collection is not initialized.");
267
268
        $queryCount = $this->getCurrentQueryCount();
269
270
        $someArticle = $user->articles->slice(0, 1);
0 ignored issues
show
Unused Code introduced by
The assignment to $someArticle is dead and can be removed.
Loading history...
271
        $otherArticle = $user->articles->slice(1, 1);
0 ignored issues
show
Unused Code introduced by
The assignment to $otherArticle is dead and can be removed.
Loading history...
272
273
        self::assertEquals($queryCount + 2, $this->getCurrentQueryCount());
274
    }
275
276
    /**
277
     * @group DDC-546
278
     */
279 View Code Duplication
    public function testContainsOneToMany()
280
    {
281
        $user = $this->em->find(CmsUser::class, $this->userId);
282
        self::assertFalse($user->articles->isInitialized(), "Pre-Condition: Collection is not initialized.");
283
284
        // Test One to Many existence retrieved from DB
285
        $article    = $this->em->find(CmsArticle::class, $this->articleId);
286
        $queryCount = $this->getCurrentQueryCount();
287
288
        self::assertTrue($user->articles->contains($article));
289
        self::assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
290
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount());
291
292
        // Test One to Many existence with state new
293
        $article = new CmsArticle();
294
        $article->topic = "Testnew";
295
        $article->text = "blub";
296
297
        $queryCount = $this->getCurrentQueryCount();
298
        self::assertFalse($user->articles->contains($article));
299
        self::assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed.");
300
301
        // Test One to Many existence with state clear
302
        $this->em->persist($article);
303
        $this->em->flush();
304
305
        $queryCount = $this->getCurrentQueryCount();
306
        self::assertFalse($user->articles->contains($article));
307
        self::assertEquals($queryCount+1, $this->getCurrentQueryCount(), "Checking for contains of persisted entity should cause one query to be executed.");
308
        self::assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
309
310
        // Test One to Many existence with state managed
311
        $article = new CmsArticle();
312
        $article->topic = "How to not fail anymore on tests";
313
        $article->text = "That is simple! Just write more tests!";
314
315
        $this->em->persist($article);
316
317
        $queryCount = $this->getCurrentQueryCount();
318
319
        self::assertFalse($user->articles->contains($article));
320
        self::assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of managed entity (but not persisted) should cause no query to be executed.");
321
        self::assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
322
    }
323
324
    /**
325
     * @group DDC-2504
326
     */
327
    public function testLazyOneToManyJoinedInheritanceIsLazilyInitialized()
328
    {
329
        $otherClass = $this->em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
330
331
        self::assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
332
    }
333
334
    /**
335
     * @group DDC-2504
336
     */
337
    public function testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWhenMatchingItemIsFound()
338
    {
339
        $otherClass = $this->em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
340
341
        // Test One to Many existence retrieved from DB
342
        $childClass = $this->em->find(DDC2504ChildClass::class, $this->ddc2504ChildClassId);
343
        $queryCount = $this->getCurrentQueryCount();
344
345
        self::assertTrue($otherClass->childClasses->contains($childClass));
346
        self::assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
347
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount(), 'Search operation was performed via SQL');
348
    }
349
350
    /**
351
     * @group DDC-2504
352
     */
353 View Code Duplication
    public function testContainsOnOneToManyJoinedInheritanceWillNotCauseQueriesWhenNonPersistentItemIsMatched()
354
    {
355
        $otherClass = $this->em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
356
        $queryCount = $this->getCurrentQueryCount();
357
358
        self::assertFalse($otherClass->childClasses->contains(new DDC2504ChildClass()));
359
        self::assertEquals(
360
            $queryCount,
361
            $this->getCurrentQueryCount(),
362
            'Checking for contains of new entity should cause no query to be executed.'
363
        );
364
    }
365
366
    /**
367
     * @group DDC-2504
368
     */
369
    public function testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWithClearStateMatchingItem()
370
    {
371
        $otherClass = $this->em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
372
        $childClass = new DDC2504ChildClass();
373
374
        // Test One to Many existence with state clear
375
        $this->em->persist($childClass);
376
        $this->em->flush();
377
378
        $queryCount = $this->getCurrentQueryCount();
379
        self::assertFalse($otherClass->childClasses->contains($childClass));
380
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Checking for contains of persisted entity should cause one query to be executed.");
381
        self::assertFalse($otherClass->childClasses->isInitialized(), "Post-Condition: Collection is not initialized.");
382
    }
383
384
    /**
385
     * @group DDC-2504
386
     */
387 View Code Duplication
    public function testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWithNewStateNotMatchingItem()
388
    {
389
        $otherClass = $this->em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
390
        $childClass = new DDC2504ChildClass();
391
392
        $this->em->persist($childClass);
393
394
        $queryCount = $this->getCurrentQueryCount();
395
396
        self::assertFalse($otherClass->childClasses->contains($childClass));
397
        self::assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of managed entity (but not persisted) should cause no query to be executed.");
398
        self::assertFalse($otherClass->childClasses->isInitialized(), "Post-Condition: Collection is not initialized.");
399
    }
400
401
    /**
402
     * @group DDC-2504
403
     */
404 View Code Duplication
    public function testCountingOnOneToManyJoinedInheritanceWillNotInitializeCollection()
405
    {
406
        $otherClass = $this->em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
407
408
        self::assertCount(2, $otherClass->childClasses);
409
410
        self::assertFalse($otherClass->childClasses->isInitialized());
411
    }
412
413
    /**
414
     * @group DDC-546
415
     */
416 View Code Duplication
    public function testContainsManyToMany()
417
    {
418
        $user = $this->em->find(CmsUser::class, $this->userId);
419
        self::assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
420
421
        // Test Many to Many existence retrieved from DB
422
        $group      = $this->em->find(CmsGroup::class, $this->groupId);
423
        $queryCount = $this->getCurrentQueryCount();
424
425
        self::assertTrue($user->groups->contains($group));
426
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Checking for contains of managed entity should cause one query to be executed.");
427
        self::assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
428
429
        // Test Many to Many existence with state new
430
        $group = new CmsGroup();
431
        $group->name = "A New group!";
432
433
        $queryCount = $this->getCurrentQueryCount();
434
435
        self::assertFalse($user->groups->contains($group));
436
        self::assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed.");
437
        self::assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
438
439
        // Test Many to Many existence with state clear
440
        $this->em->persist($group);
441
        $this->em->flush();
442
443
        $queryCount = $this->getCurrentQueryCount();
444
445
        self::assertFalse($user->groups->contains($group));
446
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Checking for contains of persisted entity should cause one query to be executed.");
447
        self::assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
448
449
        // Test Many to Many existence with state managed
450
        $group = new CmsGroup();
451
        $group->name = "My managed group";
452
453
        $this->em->persist($group);
454
455
        $queryCount = $this->getCurrentQueryCount();
456
457
        self::assertFalse($user->groups->contains($group));
458
        self::assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of managed entity (but not persisted) should cause no query to be executed.");
459
        self::assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
460
    }
461
462
    /**
463
     * @group DDC-546
464
     */
465 View Code Duplication
    public function testContainsManyToManyInverse()
466
    {
467
        $group = $this->em->find(CmsGroup::class, $this->groupId);
468
        self::assertFalse($group->users->isInitialized(), "Pre-Condition: Collection is not initialized.");
469
470
        $user = $this->em->find(CmsUser::class, $this->userId);
471
472
        $queryCount = $this->getCurrentQueryCount();
473
        self::assertTrue($group->users->contains($user));
474
        self::assertEquals($queryCount+1, $this->getCurrentQueryCount(), "Checking for contains of managed entity should cause one query to be executed.");
475
        self::assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
476
477
        $newUser = new CmsUser();
478
        $newUser->name = "A New group!";
479
480
        $queryCount = $this->getCurrentQueryCount();
481
        self::assertFalse($group->users->contains($newUser));
482
        self::assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed.");
483
        self::assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
484
    }
485
486
    /**
487
     *
488
     */
489
    public function testRemoveElementOneToMany()
490
    {
491
        $user = $this->em->find(CmsUser::class, $this->userId);
492
        self::assertFalse($user->articles->isInitialized(), "Pre-Condition: Collection is not initialized.");
493
494
        // Test One to Many removal with Entity retrieved from DB
495
        $article    = $this->em->find(CmsArticle::class, $this->articleId);
496
        $queryCount = $this->getCurrentQueryCount();
497
498
        $user->articles->removeElement($article);
499
500
        self::assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
501
        self::assertEquals($queryCount, $this->getCurrentQueryCount());
502
503
        // Test One to Many removal with Entity state as new
504
        $article = new CmsArticle();
505
        $article->topic = "Testnew";
506
        $article->text = "blub";
507
508
        $queryCount = $this->getCurrentQueryCount();
509
510
        $user->articles->removeElement($article);
511
512
        self::assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a new entity should cause no query to be executed.");
513
514
        // Test One to Many removal with Entity state as clean
515
        $this->em->persist($article);
516
        $this->em->flush();
517
518
        $queryCount = $this->getCurrentQueryCount();
519
520
        $user->articles->removeElement($article);
521
522
        self::assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a persisted entity will not cause queries when the owning side doesn't actually change.");
523
        self::assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
524
525
        // Test One to Many removal with Entity state as managed
526
        $article = new CmsArticle();
527
        $article->topic = "How to not fail anymore on tests";
528
        $article->text = "That is simple! Just write more tests!";
529
530
        $this->em->persist($article);
531
532
        $queryCount = $this->getCurrentQueryCount();
533
534
        $user->articles->removeElement($article);
535
536
        self::assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a managed entity should cause no query to be executed.");
537
    }
538
539
    /**
540
     * @group DDC-2504
541
     */
542
    public function testRemovalOfManagedElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt()
543
    {
544
        /* @var $otherClass DDC2504OtherClass */
545
        $otherClass = $this->em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
546
        /* @var $childClass DDC2504ChildClass */
547
        $childClass = $this->em->find(DDC2504ChildClass::class, $this->ddc2504ChildClassId);
548
549
        $queryCount = $this->getCurrentQueryCount();
550
551
        $otherClass->childClasses->removeElement($childClass);
552
        $childClass->other = null; // updating owning side
553
554
        self::assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
555
556
        self::assertEquals(
557
            $queryCount,
558
            $this->getCurrentQueryCount(),
559
            'No queries have been executed'
560
        );
561
562
        self::assertTrue(
563
            $otherClass->childClasses->contains($childClass),
564
            'Collection item still not updated (needs flushing)'
565
        );
566
567
        $this->em->flush();
568
569
        self::assertFalse(
570
            $otherClass->childClasses->contains($childClass),
571
            'Referenced item was removed in the transaction'
572
        );
573
574
        self::assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
575
    }
576
577
    /**
578
     * @group DDC-2504
579
     */
580 View Code Duplication
    public function testRemovalOfNonManagedElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt()
581
    {
582
        /* @var $otherClass DDC2504OtherClass */
583
        $otherClass = $this->em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
584
        $queryCount = $this->getCurrentQueryCount();
585
586
        $otherClass->childClasses->removeElement(new DDC2504ChildClass());
587
588
        self::assertEquals(
589
            $queryCount,
590
            $this->getCurrentQueryCount(),
591
            'Removing an unmanaged entity should cause no query to be executed.'
592
        );
593
    }
594
595
    /**
596
     * @group DDC-2504
597
     */
598
    public function testRemovalOfNewElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt()
599
    {
600
        /* @var $otherClass DDC2504OtherClass */
601
        $otherClass = $this->em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
602
        $childClass = new DDC2504ChildClass();
603
604
        $this->em->persist($childClass);
605
606
        $queryCount = $this->getCurrentQueryCount();
607
608
        $otherClass->childClasses->removeElement($childClass);
609
610
        self::assertEquals(
611
            $queryCount,
612
            $this->getCurrentQueryCount(),
613
            'Removing a new entity should cause no query to be executed.'
614
        );
615
    }
616
617
    /**
618
     * @group DDC-2504
619
     */
620
    public function testRemovalOfNewManagedElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt()
621
    {
622
        $otherClass = $this->em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
623
        $childClass = new DDC2504ChildClass();
624
625
        $this->em->persist($childClass);
626
        $this->em->flush();
627
628
        $queryCount = $this->getCurrentQueryCount();
629
630
        $otherClass->childClasses->removeElement($childClass);
631
632
        self::assertEquals(
633
            $queryCount,
634
            $this->getCurrentQueryCount(),
635
            'No queries are executed, as the owning side of the association is not actually updated.'
636
        );
637
        self::assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
638
    }
639
640
    /**
641
     *
642
     */
643
    public function testRemoveElementManyToMany()
644
    {
645
        $user = $this->em->find(CmsUser::class, $this->userId);
646
        self::assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
647
648
        // Test Many to Many removal with Entity retrieved from DB
649
        $group      = $this->em->find(CmsGroup::class, $this->groupId);
650
        $queryCount = $this->getCurrentQueryCount();
651
652
        self::assertTrue($user->groups->removeElement($group));
653
654
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a persisted entity should cause one query to be executed.");
655
        self::assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
656
657
        self::assertFalse($user->groups->removeElement($group), "Removing an already removed element returns false");
658
659
        // Test Many to Many removal with Entity state as new
660
        $group = new CmsGroup();
661
        $group->name = "A New group!";
662
663
        $queryCount = $this->getCurrentQueryCount();
664
665
        $user->groups->removeElement($group);
666
667
        self::assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing new entity should cause no query to be executed.");
668
        self::assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
669
670
        // Test Many to Many removal with Entity state as clean
671
        $this->em->persist($group);
672
        $this->em->flush();
673
674
        $queryCount = $this->getCurrentQueryCount();
675
676
        $user->groups->removeElement($group);
677
678
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a persisted entity should cause one query to be executed.");
679
        self::assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
680
681
        // Test Many to Many removal with Entity state as managed
682
        $group = new CmsGroup();
683
        $group->name = "A New group!";
684
685
        $this->em->persist($group);
686
687
        $queryCount = $this->getCurrentQueryCount();
688
689
        $user->groups->removeElement($group);
690
691
        self::assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a managed entity should cause no query to be executed.");
692
        self::assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
693
    }
694
695
    /**
696
     *
697
     */
698 View Code Duplication
    public function testRemoveElementManyToManyInverse()
699
    {
700
        $group = $this->em->find(CmsGroup::class, $this->groupId);
701
        self::assertFalse($group->users->isInitialized(), "Pre-Condition: Collection is not initialized.");
702
703
        $user       = $this->em->find(CmsUser::class, $this->userId);
704
        $queryCount = $this->getCurrentQueryCount();
705
706
        $group->users->removeElement($user);
707
708
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a managed entity should cause one query to be executed.");
709
        self::assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
710
711
        $newUser = new CmsUser();
712
        $newUser->name = "A New group!";
713
714
        $queryCount = $this->getCurrentQueryCount();
715
716
        $group->users->removeElement($newUser);
717
718
        self::assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a new entity should cause no query to be executed.");
719
        self::assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
720
    }
721
722
    /**
723
     * @group DDC-1399
724
     */
725
    public function testCountAfterAddThenFlush()
726
    {
727
        $user = $this->em->find(CmsUser::class, $this->userId);
728
729
        $newGroup = new CmsGroup();
730
        $newGroup->name = "Test4";
731
732
        $user->addGroup($newGroup);
733
        $this->em->persist($newGroup);
734
735
        self::assertFalse($user->groups->isInitialized());
736
        self::assertCount(4, $user->groups);
737
        self::assertFalse($user->groups->isInitialized());
738
739
        $this->em->flush();
740
741
        self::assertCount(4, $user->groups);
742
    }
743
744
    /**
745
     * @group DDC-1462
746
     * @group non-cacheable
747
     */
748
    public function testSliceOnDirtyCollection()
749
    {
750
        $user = $this->em->find(CmsUser::class, $this->userId);
751
        /* @var $user CmsUser */
752
753
        $newGroup = new CmsGroup();
754
        $newGroup->name = "Test4";
755
756
        $user->addGroup($newGroup);
757
        $this->em->persist($newGroup);
758
759
        $qc = $this->getCurrentQueryCount();
760
        $groups = $user->groups->slice(0, 10);
761
762
        self::assertCount(4, $groups);
763
        self::assertEquals($qc + 1, $this->getCurrentQueryCount());
764
    }
765
766
    /**
767
     * @group DDC-1398
768
     * @group non-cacheable
769
     */
770
    public function testGetIndexByIdentifier()
771
    {
772
        $user = $this->em->find(CmsUser::class, $this->userId);
773
        /* @var $user CmsUser */
774
775
        $queryCount = $this->getCurrentQueryCount();
776
        $phonenumber = $user->phonenumbers->get($this->phonenumber);
777
778
        self::assertFalse($user->phonenumbers->isInitialized());
779
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount());
780
        self::assertSame($phonenumber, $this->em->find(CmsPhonenumber::class, $this->phonenumber));
781
782
        $article = $user->phonenumbers->get($this->phonenumber);
0 ignored issues
show
Unused Code introduced by
The assignment to $article is dead and can be removed.
Loading history...
783
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Getting the same entity should not cause an extra query to be executed");
784
    }
785
786
    /**
787
     * @group DDC-1398
788
     */
789
    public function testGetIndexByOneToMany()
790
    {
791
        $user = $this->em->find(CmsUser::class, $this->userId);
792
        /* @var $user CmsUser */
793
794
        $queryCount = $this->getCurrentQueryCount();
795
796
        $article = $user->articles->get($this->topic);
797
798
        self::assertFalse($user->articles->isInitialized());
799
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount());
800
        self::assertSame($article, $this->em->find(CmsArticle::class, $this->articleId));
801
    }
802
803
    /**
804
     * @group DDC-1398
805
     */
806
    public function testGetIndexByManyToManyInverseSide()
807
    {
808
        $group = $this->em->find(CmsGroup::class, $this->groupId);
809
        /* @var $group CmsGroup */
810
811
        $queryCount = $this->getCurrentQueryCount();
812
813
        $user = $group->users->get($this->username);
814
815
        self::assertFalse($group->users->isInitialized());
816
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount());
817
        self::assertSame($user, $this->em->find(CmsUser::class, $this->userId));
818
    }
819
820
    /**
821
     * @group DDC-1398
822
     */
823 View Code Duplication
    public function testGetIndexByManyToManyOwningSide()
824
    {
825
        $user = $this->em->find(CmsUser::class, $this->userId);
826
        /* @var $user CmsUser */
827
828
        $queryCount = $this->getCurrentQueryCount();
829
830
        $group = $user->groups->get($this->groupname);
831
832
        self::assertFalse($user->groups->isInitialized());
833
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount());
834
        self::assertSame($group, $this->em->find(CmsGroup::class, $this->groupId));
835
    }
836
837
    /**
838
     * @group DDC-1398
839
     */
840
    public function testGetNonExistentIndexBy()
841
    {
842
        $user = $this->em->find(CmsUser::class, $this->userId);
843
        self::assertNull($user->articles->get(-1));
844
        self::assertNull($user->groups->get(-1));
845
    }
846
847 View Code Duplication
    public function testContainsKeyIndexByOneToMany()
848
    {
849
        $user = $this->em->find(CmsUser::class, $this->userId);
850
        /* @var $user CmsUser */
851
852
        $queryCount = $this->getCurrentQueryCount();
853
854
        $contains = $user->articles->containsKey($this->topic);
855
856
        self::assertTrue($contains);
857
        self::assertFalse($user->articles->isInitialized());
858
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount());
859
    }
860
861 View Code Duplication
    public function testContainsKeyIndexByOneToManyJoinedInheritance()
862
    {
863
        $class = $this->em->getClassMetadata(DDC2504OtherClass::class);
864
        $class->getProperty('childClasses')->setIndexedBy('id');
865
866
        $otherClass = $this->em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
867
868
        $queryCount = $this->getCurrentQueryCount();
869
870
        $contains = $otherClass->childClasses->containsKey($this->ddc2504ChildClassId);
871
872
        self::assertTrue($contains);
873
        self::assertFalse($otherClass->childClasses->isInitialized());
874
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount());
875
    }
876
877 View Code Duplication
    public function testContainsKeyIndexByManyToMany()
878
    {
879
        $user = $this->em->find(CmsUser::class, $this->userId2);
880
881
        $group = $this->em->find(CmsGroup::class, $this->groupId);
882
883
        $queryCount = $this->getCurrentQueryCount();
884
885
        $contains = $user->groups->containsKey($group->name);
886
887
        self::assertTrue($contains, "The item is not into collection");
888
        self::assertFalse($user->groups->isInitialized(), "The collection must not be initialized");
889
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount());
890
    }
891 View Code Duplication
    public function testContainsKeyIndexByManyToManyNonOwning()
892
    {
893
        $user = $this->em->find(CmsUser::class, $this->userId2);
894
        $group = $this->em->find(CmsGroup::class, $this->groupId);
895
896
        $queryCount = $this->getCurrentQueryCount();
897
898
        $contains = $group->users->containsKey($user->username);
899
900
        self::assertTrue($contains, "The item is not into collection");
901
        self::assertFalse($group->users->isInitialized(), "The collection must not be initialized");
902
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount());
903
    }
904
905 View Code Duplication
    public function testContainsKeyIndexByWithPkManyToMany()
906
    {
907
        $class = $this->em->getClassMetadata(CmsUser::class);
908
        $class->getProperty('groups')->setIndexedBy('id');
909
910
        $user = $this->em->find(CmsUser::class, $this->userId2);
911
912
        $queryCount = $this->getCurrentQueryCount();
913
914
        $contains = $user->groups->containsKey($this->groupId);
915
916
        self::assertTrue($contains, "The item is not into collection");
917
        self::assertFalse($user->groups->isInitialized(), "The collection must not be initialized");
918
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount());
919
    }
920 View Code Duplication
    public function testContainsKeyIndexByWithPkManyToManyNonOwning()
921
    {
922
        $class = $this->em->getClassMetadata(CmsGroup::class);
923
        $class->getProperty('users')->setIndexedBy('id');
924
925
        $group = $this->em->find(CmsGroup::class, $this->groupId);
926
927
        $queryCount = $this->getCurrentQueryCount();
928
929
        $contains = $group->users->containsKey($this->userId2);
930
931
        self::assertTrue($contains, "The item is not into collection");
932
        self::assertFalse($group->users->isInitialized(), "The collection must not be initialized");
933
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount());
934
    }
935
936 View Code Duplication
    public function testContainsKeyNonExistentIndexByOneToMany()
937
    {
938
        $user = $this->em->find(CmsUser::class, $this->userId2);
939
940
        $queryCount = $this->getCurrentQueryCount();
941
942
        $contains = $user->articles->containsKey("NonExistentTopic");
943
944
        self::assertFalse($contains);
945
        self::assertFalse($user->articles->isInitialized());
946
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount());
947
    }
948
949 View Code Duplication
    public function testContainsKeyNonExistentIndexByManyToMany()
950
    {
951
        $user = $this->em->find(CmsUser::class, $this->userId2);
952
953
954
        $queryCount = $this->getCurrentQueryCount();
955
956
        $contains = $user->groups->containsKey("NonExistentTopic");
957
958
        self::assertFalse($contains);
959
        self::assertFalse($user->groups->isInitialized());
960
        self::assertEquals($queryCount + 1, $this->getCurrentQueryCount());
961
    }
962
963
    private function loadFixture()
964
    {
965
        $user1 = new CmsUser();
966
        $user1->username = "beberlei";
967
        $user1->name = "Benjamin";
968
        $user1->status = "active";
969
970
        $user2 = new CmsUser();
971
        $user2->username = "jwage";
972
        $user2->name = "Jonathan";
973
        $user2->status = "active";
974
975
        $user3 = new CmsUser();
976
        $user3->username = "romanb";
977
        $user3->name = "Roman";
978
        $user3->status = "active";
979
980
        $user4 = new CmsUser();
981
        $user4->username = "gblanco";
982
        $user4->name = "Guilherme";
983
        $user4->status = "active";
984
985
        $this->em->persist($user1);
986
        $this->em->persist($user2);
987
        $this->em->persist($user3);
988
        $this->em->persist($user4);
989
990
        $group1 = new CmsGroup();
991
        $group1->name = "Test1";
992
993
        $group2 = new CmsGroup();
994
        $group2->name = "Test2";
995
996
        $group3 = new CmsGroup();
997
        $group3->name = "Test3";
998
999
        $user1->addGroup($group1);
1000
        $user1->addGroup($group2);
1001
        $user1->addGroup($group3);
1002
1003
        $user2->addGroup($group1);
1004
        $user3->addGroup($group1);
1005
        $user4->addGroup($group1);
1006
1007
        $this->em->persist($group1);
1008
        $this->em->persist($group2);
1009
        $this->em->persist($group3);
1010
1011
        $article1 = new CmsArticle();
1012
        $article1->topic = "Test1";
1013
        $article1->text = "Test1";
1014
        $article1->setAuthor($user1);
1015
1016
        $article2 = new CmsArticle();
1017
        $article2->topic = "Test2";
1018
        $article2->text = "Test2";
1019
        $article2->setAuthor($user1);
1020
1021
        $this->em->persist($article1);
1022
        $this->em->persist($article2);
1023
1024
        $phonenumber1 = new CmsPhonenumber();
1025
        $phonenumber1->phonenumber = '12345';
1026
1027
        $phonenumber2 = new CmsPhonenumber();
1028
        $phonenumber2->phonenumber = '67890';
1029
1030
        $this->em->persist($phonenumber1);
1031
        $this->em->persist($phonenumber2);
1032
1033
        $user1->addPhonenumber($phonenumber1);
1034
1035
        // DDC-2504
1036
        $otherClass = new DDC2504OtherClass();
1037
        $childClass1 = new DDC2504ChildClass();
1038
        $childClass2 = new DDC2504ChildClass();
1039
1040
        $childClass1->other = $otherClass;
1041
        $childClass2->other = $otherClass;
1042
1043
        $otherClass->childClasses[] = $childClass1;
1044
        $otherClass->childClasses[] = $childClass2;
1045
1046
        $this->em->persist($childClass1);
1047
        $this->em->persist($childClass2);
1048
        $this->em->persist($otherClass);
1049
1050
        $this->em->flush();
1051
        $this->em->clear();
1052
1053
        $this->articleId = $article1->id;
1054
        $this->userId = $user1->getId();
1055
        $this->userId2 = $user2->getId();
1056
        $this->groupId = $group1->id;
1057
        $this->ddc2504OtherClassId = $otherClass->id;
1058
        $this->ddc2504ChildClassId = $childClass1->id;
1059
1060
        $this->username = $user1->username;
1061
        $this->groupname = $group1->name;
1062
        $this->topic = $article1->topic;
1063
        $this->phonenumber = $phonenumber1->phonenumber;
1064
1065
    }
1066
1067
    /**
1068
     * @group DDC-3343
1069
     */
1070
    public function testRemoveManagedElementFromOneToManyExtraLazyCollectionIsNoOp()
1071
    {
1072
        list($userId, $tweetId) = $this->loadTweetFixture();
1073
1074
        /* @var $user User */
1075
        $user = $this->em->find(User::class, $userId);
1076
1077
        $user->tweets->removeElement($this->em->find(Tweet::class, $tweetId));
1078
1079
        $this->em->clear();
1080
1081
        /* @var $user User */
1082
        $user = $this->em->find(User::class, $userId);
1083
1084
        self::assertCount(1, $user->tweets, 'Element was not removed - need to update the owning side first');
1085
    }
1086
1087
    /**
1088
     * @group DDC-3343
1089
     */
1090
    public function testRemoveManagedElementFromOneToManyExtraLazyCollectionWithoutDeletingTheTargetEntityEntryIsNoOp()
1091
    {
1092
        list($userId, $tweetId) = $this->loadTweetFixture();
1093
1094
        /* @var $user User */
1095
        $user  = $this->em->find(User::class, $userId);
1096
        $tweet = $this->em->find(Tweet::class, $tweetId);
1097
1098
        $user->tweets->removeElement($tweet);
1099
1100
        $this->em->clear();
1101
1102
        /* @var $tweet Tweet */
1103
        $tweet = $this->em->find(Tweet::class, $tweetId);
1104
        self::assertInstanceOf(
1105
            Tweet::class,
1106
            $tweet,
1107
            'Even though the collection is extra lazy, the tweet should not have been deleted'
1108
        );
1109
1110
        self::assertInstanceOf(
1111
            User::class,
1112
            $tweet->author,
1113
            'Tweet author link has not been removed - need to update the owning side first'
1114
        );
1115
    }
1116
1117
    /**
1118
     * @group DDC-3343
1119
     */
1120
    public function testRemovingManagedLazyProxyFromExtraLazyOneToManyDoesRemoveTheAssociationButNotTheEntity()
1121
    {
1122
        list($userId, $tweetId) = $this->loadTweetFixture();
1123
1124
        /* @var $user User */
1125
        $user  = $this->em->find(User::class, $userId);
1126
        $tweet = $this->em->getReference(Tweet::class, $tweetId);
1127
1128
        $user->tweets->removeElement($this->em->getReference(Tweet::class, $tweetId));
1129
1130
        $this->em->clear();
1131
1132
        /* @var $tweet Tweet */
1133
        $tweet = $this->em->find(Tweet::class, $tweet->id);
1134
        self::assertInstanceOf(
1135
            Tweet::class,
1136
            $tweet,
1137
            'Even though the collection is extra lazy, the tweet should not have been deleted'
1138
        );
1139
1140
        self::assertInstanceOf(User::class, $tweet->author);
1141
1142
        /* @var $user User */
1143
        $user = $this->em->find(User::class, $userId);
1144
1145
        self::assertCount(1, $user->tweets, 'Element was not removed - need to update the owning side first');
1146
    }
1147
1148
    /**
1149
     * @group DDC-3343
1150
     */
1151 View Code Duplication
    public function testRemoveOrphanedManagedElementFromOneToManyExtraLazyCollection()
1152
    {
1153
        list($userId, $userListId) = $this->loadUserListFixture();
1154
1155
        /* @var $user User */
1156
        $user = $this->em->find(User::class, $userId);
1157
1158
        $user->userLists->removeElement($this->em->find(UserList::class, $userListId));
1159
1160
        $this->em->clear();
1161
1162
        /* @var $user User */
1163
        $user = $this->em->find(User::class, $userId);
1164
1165
        self::assertCount(0, $user->userLists, 'Element was removed from association due to orphan removal');
1166
        self::assertNull(
1167
            $this->em->find(UserList::class, $userListId),
1168
            'Element was deleted due to orphan removal'
1169
        );
1170
    }
1171
1172
    /**
1173
     * @group DDC-3343
1174
     */
1175
    public function testRemoveOrphanedUnManagedElementFromOneToManyExtraLazyCollection()
1176
    {
1177
        list($userId, $userListId) = $this->loadUserListFixture();
1178
1179
        /* @var $user User */
1180
        $user = $this->em->find(User::class, $userId);
1181
1182
        $user->userLists->removeElement(new UserList());
1183
1184
        $this->em->clear();
1185
1186
        /* @var $userList UserList */
1187
        $userList = $this->em->find(UserList::class, $userListId);
1188
        self::assertInstanceOf(
1189
            UserList::class,
1190
            $userList,
1191
            'Even though the collection is extra lazy + orphan removal, the user list should not have been deleted'
1192
        );
1193
1194
        self::assertInstanceOf(
1195
            User::class,
1196
            $userList->owner,
1197
            'User list to owner link has not been removed'
1198
        );
1199
    }
1200
1201
    /**
1202
     * @group DDC-3343
1203
     */
1204 View Code Duplication
    public function testRemoveOrphanedManagedLazyProxyFromExtraLazyOneToMany()
1205
    {
1206
        list($userId, $userListId) = $this->loadUserListFixture();
1207
1208
        /* @var $user User */
1209
        $user = $this->em->find(User::class, $userId);
1210
1211
        $user->userLists->removeElement($this->em->getReference(UserList::class, $userListId));
1212
1213
        $this->em->clear();
1214
1215
        /* @var $user User */
1216
        $user = $this->em->find(User::class, $userId);
1217
1218
        self::assertCount(0, $user->userLists, 'Element was removed from association due to orphan removal');
1219
        self::assertNull(
1220
            $this->em->find(UserList::class, $userListId),
1221
            'Element was deleted due to orphan removal'
1222
        );
1223
    }
1224
1225
    /**
1226
     * @return int[] ordered tuple: user id and tweet id
1227
     */
1228 View Code Duplication
    private function loadTweetFixture()
1229
    {
1230
        $user  = new User();
1231
        $tweet = new Tweet();
1232
1233
        $user->name     = 'ocramius';
1234
        $tweet->content = 'The cat is on the table';
1235
1236
        $user->addTweet($tweet);
1237
1238
        $this->em->persist($user);
1239
        $this->em->persist($tweet);
1240
        $this->em->flush();
1241
        $this->em->clear();
1242
1243
        return [$user->id, $tweet->id];
1244
    }
1245
1246
    /**
1247
     * @return int[] ordered tuple: user id and user list id
1248
     */
1249 View Code Duplication
    private function loadUserListFixture()
1250
    {
1251
        $user     = new User();
1252
        $userList = new UserList();
1253
1254
        $user->name     = 'ocramius';
1255
        $userList->listName = 'PHP Developers to follow closely';
1256
1257
        $user->addUserList($userList);
1258
1259
        $this->em->persist($user);
1260
        $this->em->persist($userList);
1261
        $this->em->flush();
1262
        $this->em->clear();
1263
1264
        return [$user->id, $userList->id];
1265
    }
1266
}
1267