Failed Conditions
Push — master ( 2ade86...13f838 )
by Jonathan
18s
created

ExtraLazyCollectionTest   D

Complexity

Total Complexity 57

Size/Duplication

Total Lines 1236
Duplicated Lines 33.98 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 0
Metric Value
wmc 57
lcom 1
cbo 13
dl 420
loc 1236
rs 4.7133
c 0
b 0
f 0

53 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 25 1
A tearDown() 0 18 1
A testCountNotInitializesCollection() 0 13 2
A testCountWhenNewEntityPresent() 0 14 1
A testCountWhenInitialized() 0 11 2
A testCountInverseCollection() 0 8 1
A testCountOneToManyJoinedInheritance() 0 7 1
B testSlice() 0 26 2
A testSliceInitializedCollection() 0 15 2
A testSliceInverseCollection() 0 17 1
A testSliceOneToMany() 0 12 1
A testLazyOneToManyJoinedInheritanceIsLazilyInitialized() 0 6 1
A testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWithClearStateMatchingItem() 0 14 1
A testCountingOnOneToManyJoinedInheritanceWillNotInitializeCollection() 0 8 1
B testContainsManyToMany() 0 45 1
A testRemoveElementOneToMany() 0 49 1
B testRemovalOfManagedElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt() 0 34 1
A testRemovalOfNewElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt() 0 18 1
A testCountOneToMany() 7 7 1
A testFullSlice() 8 8 1
A testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWhenMatchingItemIsFound() 12 12 1
A testContainsOnOneToManyJoinedInheritanceWillNotCauseQueriesWhenNonPersistentItemIsMatched() 12 12 1
A testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWithNewStateNotMatchingItem() 13 13 1
A testContainsManyToManyInverse() 20 20 1
A testRemovalOfNonManagedElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt() 14 14 1
A testRemovalOfNewManagedElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt() 19 19 1
B testContainsOneToMany() 44 44 1
A testRemoveElementManyToManyInverse() 23 23 1
A testCountAfterAddThenFlush() 0 18 1
A testSliceOnDirtyCollection() 0 17 1
A testGetIndexByIdentifier() 0 15 1
A testGetIndexByOneToMany() 0 13 1
A testGetIndexByManyToManyInverseSide() 0 13 1
A testGetIndexByManyToManyOwningSide() 13 13 1
A testGetNonExistentIndexBy() 0 6 1
A testContainsKeyIndexByOneToMany() 13 13 1
A testContainsKeyIndexByOneToManyJoinedInheritance() 15 15 1
A testContainsKeyIndexByManyToMany() 14 14 1
A testContainsKeyIndexByManyToManyNonOwning() 13 13 1
A testContainsKeyIndexByWithPkManyToMany() 15 15 1
A testContainsKeyIndexByWithPkManyToManyNonOwning() 15 15 1
A testContainsKeyNonExistentIndexByOneToMany() 12 12 1
A testContainsKeyNonExistentIndexByManyToMany() 13 13 1
B loadFixture() 0 103 1
A testRemoveManagedElementFromOneToManyExtraLazyCollectionIsNoOp() 0 16 1
B testRemoveManagedElementFromOneToManyExtraLazyCollectionWithoutDeletingTheTargetEntityEntryIsNoOp() 0 26 1
B testRemovingManagedLazyProxyFromExtraLazyOneToManyDoesRemoveTheAssociationButNotTheEntity() 0 27 1
A testRemoveOrphanedManagedElementFromOneToManyExtraLazyCollection() 20 20 1
B testRemoveOrphanedUnManagedElementFromOneToManyExtraLazyCollection() 0 25 1
A testRemoveOrphanedManagedLazyProxyFromExtraLazyOneToMany() 20 20 1
A loadTweetFixture() 17 17 1
A loadUserListFixture() 17 17 1
A testRemoveElementManyToMany() 51 51 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ExtraLazyCollectionTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ExtraLazyCollectionTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional;
4
5
use Doctrine\ORM\Mapping\ClassMetadataInfo;
6
use Doctrine\Tests\Models\CMS\CmsArticle;
7
use Doctrine\Tests\Models\CMS\CmsGroup;
8
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
9
use Doctrine\Tests\Models\CMS\CmsUser;
10
use Doctrine\Tests\Models\DDC2504\DDC2504ChildClass;
11
use Doctrine\Tests\Models\DDC2504\DDC2504OtherClass;
12
use Doctrine\Tests\Models\Tweet\Tweet;
13
use Doctrine\Tests\Models\Tweet\User;
14
use Doctrine\Tests\Models\Tweet\UserList;
15
use Doctrine\Tests\OrmFunctionalTestCase;
16
17
/**
18
 * Description of ExtraLazyCollectionTest
19
 *
20
 * @author beberlei
21
 */
22
class ExtraLazyCollectionTest extends OrmFunctionalTestCase
23
{
24
    private $userId;
25
    private $userId2;
26
    private $groupId;
27
    private $articleId;
28
    private $ddc2504OtherClassId;
29
    private $ddc2504ChildClassId;
30
31
    private $username;
32
    private $groupname;
33
    private $topic;
34
    private $phonenumber;
35
36
    public function setUp()
37
    {
38
        $this->useModelSet('tweet');
39
        $this->useModelSet('cms');
40
        $this->useModelSet('ddc2504');
41
        parent::setUp();
42
43
        $class = $this->_em->getClassMetadata(CmsUser::class);
44
        $class->associationMappings['groups']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
45
        $class->associationMappings['groups']['indexBy'] = 'name';
46
        $class->associationMappings['articles']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
47
        $class->associationMappings['articles']['indexBy'] = 'topic';
48
        $class->associationMappings['phonenumbers']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
49
        $class->associationMappings['phonenumbers']['indexBy'] = 'phonenumber';
50
51
        unset($class->associationMappings['phonenumbers']['cache']);
52
        unset($class->associationMappings['articles']['cache']);
53
        unset($class->associationMappings['users']['cache']);
54
55
        $class = $this->_em->getClassMetadata(CmsGroup::class);
56
        $class->associationMappings['users']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
57
        $class->associationMappings['users']['indexBy'] = 'username';
58
59
        $this->loadFixture();
60
    }
61
62
    public function tearDown()
63
    {
64
        parent::tearDown();
65
66
        $class = $this->_em->getClassMetadata(CmsUser::class);
67
        $class->associationMappings['groups']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
68
        $class->associationMappings['articles']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
69
        $class->associationMappings['phonenumbers']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
70
71
        unset($class->associationMappings['groups']['indexBy']);
72
        unset($class->associationMappings['articles']['indexBy']);
73
        unset($class->associationMappings['phonenumbers']['indexBy']);
74
75
        $class = $this->_em->getClassMetadata(CmsGroup::class);
76
        $class->associationMappings['users']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
77
78
        unset($class->associationMappings['users']['indexBy']);
79
    }
80
81
    /**
82
     * @group DDC-546
83
     * @group non-cacheable
84
     */
85
    public function testCountNotInitializesCollection()
86
    {
87
        $user = $this->_em->find(CmsUser::class, $this->userId);
88
        $queryCount = $this->getCurrentQueryCount();
89
90
        $this->assertFalse($user->groups->isInitialized());
91
        $this->assertEquals(3, count($user->groups));
92
        $this->assertFalse($user->groups->isInitialized());
93
94
        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...
95
96
        $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration.");
97
    }
98
99
    /**
100
     * @group DDC-546
101
     */
102
    public function testCountWhenNewEntityPresent()
103
    {
104
        $user = $this->_em->find(CmsUser::class, $this->userId);
105
106
        $newGroup = new CmsGroup();
107
        $newGroup->name = "Test4";
108
109
        $user->addGroup($newGroup);
110
        $this->_em->persist($newGroup);
111
112
        $this->assertFalse($user->groups->isInitialized());
113
        $this->assertEquals(4, count($user->groups));
114
        $this->assertFalse($user->groups->isInitialized());
115
    }
116
117
    /**
118
     * @group DDC-546
119
     * @group non-cacheable
120
     */
121
    public function testCountWhenInitialized()
122
    {
123
        $user = $this->_em->find(CmsUser::class, $this->userId);
124
        $queryCount = $this->getCurrentQueryCount();
125
126
        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...
127
128
        $this->assertTrue($user->groups->isInitialized());
129
        $this->assertEquals(3, count($user->groups));
130
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Should only execute one query to initialize collection, no extra query for count() more.");
131
    }
132
133
    /**
134
     * @group DDC-546
135
     */
136
    public function testCountInverseCollection()
137
    {
138
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
139
        $this->assertFalse($group->users->isInitialized(), "Pre-Condition");
140
141
        $this->assertEquals(4, count($group->users));
142
        $this->assertFalse($group->users->isInitialized(), "Extra Lazy collection should not be initialized by counting the collection.");
143
    }
144
145
    /**
146
     * @group DDC-546
147
     */
148 View Code Duplication
    public function testCountOneToMany()
149
    {
150
        $user = $this->_em->find(CmsUser::class, $this->userId);
151
        $this->assertFalse($user->groups->isInitialized(), "Pre-Condition");
152
153
        $this->assertEquals(2, count($user->articles));
154
    }
155
156
    /**
157
     * @group DDC-2504
158
     */
159
    public function testCountOneToManyJoinedInheritance()
160
    {
161
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
162
163
        $this->assertFalse($otherClass->childClasses->isInitialized(), "Pre-Condition");
164
        $this->assertEquals(2, count($otherClass->childClasses));
165
    }
166
167
    /**
168
     * @group DDC-546
169
     */
170 View Code Duplication
    public function testFullSlice()
171
    {
172
        $user = $this->_em->find(CmsUser::class, $this->userId);
173
        $this->assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
174
175
        $someGroups = $user->groups->slice(null);
176
        $this->assertEquals(3, count($someGroups));
177
    }
178
179
    /**
180
     * @group DDC-546
181
     * @group non-cacheable
182
     */
183
    public function testSlice()
184
    {
185
        $user = $this->_em->find(CmsUser::class, $this->userId);
186
        $this->assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
187
188
        $queryCount = $this->getCurrentQueryCount();
189
190
        $someGroups = $user->groups->slice(0, 2);
191
192
        $this->assertContainsOnly(CmsGroup::class, $someGroups);
193
        $this->assertEquals(2, count($someGroups));
194
        $this->assertFalse($user->groups->isInitialized(), "Slice should not initialize the collection if it wasn't before!");
195
196
        $otherGroup = $user->groups->slice(2, 1);
197
198
        $this->assertContainsOnly(CmsGroup::class, $otherGroup);
199
        $this->assertEquals(1, count($otherGroup));
200
        $this->assertFalse($user->groups->isInitialized());
201
202
        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...
203
204
        $this->assertTrue($user->groups->isInitialized());
205
        $this->assertEquals(3, count($user->groups));
206
207
        $this->assertEquals($queryCount + 3, $this->getCurrentQueryCount());
208
    }
209
210
    /**
211
     * @group DDC-546
212
     * @group non-cacheable
213
     */
214
    public function testSliceInitializedCollection()
215
    {
216
        $user = $this->_em->find(CmsUser::class, $this->userId);
217
        $queryCount = $this->getCurrentQueryCount();
218
219
        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...
220
221
        $someGroups = $user->groups->slice(0, 2);
222
223
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
224
225
        $this->assertEquals(2, count($someGroups));
226
        $this->assertTrue($user->groups->contains(array_shift($someGroups)));
227
        $this->assertTrue($user->groups->contains(array_shift($someGroups)));
228
    }
229
230
    /**
231
     * @group DDC-546
232
     */
233
    public function testSliceInverseCollection()
234
    {
235
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
236
        $this->assertFalse($group->users->isInitialized(), "Pre-Condition");
237
        $queryCount = $this->getCurrentQueryCount();
238
239
        $someUsers = $group->users->slice(0, 2);
240
        $otherUsers = $group->users->slice(2, 2);
241
242
        $this->assertContainsOnly(CmsUser::class, $someUsers);
243
        $this->assertContainsOnly(CmsUser::class, $otherUsers);
244
        $this->assertEquals(2, count($someUsers));
245
        $this->assertEquals(2, count($otherUsers));
246
247
        // +2 queries executed by slice
248
        $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Slicing two parts should only execute two additional queries.");
249
    }
250
251
    /**
252
     * @group DDC-546
253
     */
254
    public function testSliceOneToMany()
255
    {
256
        $user = $this->_em->find(CmsUser::class, $this->userId);
257
        $this->assertFalse($user->articles->isInitialized(), "Pre-Condition: Collection is not initialized.");
258
259
        $queryCount = $this->getCurrentQueryCount();
260
261
        $someArticle = $user->articles->slice(0, 1);
0 ignored issues
show
Unused Code introduced by
$someArticle is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
262
        $otherArticle = $user->articles->slice(1, 1);
0 ignored issues
show
Unused Code introduced by
$otherArticle is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
263
264
        $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount());
265
    }
266
267
    /**
268
     * @group DDC-546
269
     */
270 View Code Duplication
    public function testContainsOneToMany()
271
    {
272
        $user = $this->_em->find(CmsUser::class, $this->userId);
273
        $this->assertFalse($user->articles->isInitialized(), "Pre-Condition: Collection is not initialized.");
274
275
        // Test One to Many existence retrieved from DB
276
        $article    = $this->_em->find(CmsArticle::class, $this->articleId);
277
        $queryCount = $this->getCurrentQueryCount();
278
279
        $this->assertTrue($user->articles->contains($article));
280
        $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
281
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
282
283
        // Test One to Many existence with state new
284
        $article = new CmsArticle();
285
        $article->topic = "Testnew";
286
        $article->text = "blub";
287
288
        $queryCount = $this->getCurrentQueryCount();
289
        $this->assertFalse($user->articles->contains($article));
290
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed.");
291
292
        // Test One to Many existence with state clear
293
        $this->_em->persist($article);
294
        $this->_em->flush();
295
296
        $queryCount = $this->getCurrentQueryCount();
297
        $this->assertFalse($user->articles->contains($article));
298
        $this->assertEquals($queryCount+1, $this->getCurrentQueryCount(), "Checking for contains of persisted entity should cause one query to be executed.");
299
        $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
300
301
        // Test One to Many existence with state managed
302
        $article = new CmsArticle();
303
        $article->topic = "How to not fail anymore on tests";
304
        $article->text = "That is simple! Just write more tests!";
305
306
        $this->_em->persist($article);
307
308
        $queryCount = $this->getCurrentQueryCount();
309
310
        $this->assertFalse($user->articles->contains($article));
311
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of managed entity (but not persisted) should cause no query to be executed.");
312
        $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
313
    }
314
315
    /**
316
     * @group DDC-2504
317
     */
318
    public function testLazyOneToManyJoinedInheritanceIsLazilyInitialized()
319
    {
320
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
321
322
        $this->assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
323
    }
324
325
    /**
326
     * @group DDC-2504
327
     */
328 View Code Duplication
    public function testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWhenMatchingItemIsFound()
329
    {
330
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
331
332
        // Test One to Many existence retrieved from DB
333
        $childClass = $this->_em->find(DDC2504ChildClass::class, $this->ddc2504ChildClassId);
334
        $queryCount = $this->getCurrentQueryCount();
335
336
        $this->assertTrue($otherClass->childClasses->contains($childClass));
337
        $this->assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
338
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), 'Search operation was performed via SQL');
339
    }
340
341
    /**
342
     * @group DDC-2504
343
     */
344 View Code Duplication
    public function testContainsOnOneToManyJoinedInheritanceWillNotCauseQueriesWhenNonPersistentItemIsMatched()
345
    {
346
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
347
        $queryCount = $this->getCurrentQueryCount();
348
349
        $this->assertFalse($otherClass->childClasses->contains(new DDC2504ChildClass()));
350
        $this->assertEquals(
351
            $queryCount,
352
            $this->getCurrentQueryCount(),
353
            'Checking for contains of new entity should cause no query to be executed.'
354
        );
355
    }
356
357
    /**
358
     * @group DDC-2504
359
     */
360
    public function testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWithClearStateMatchingItem()
361
    {
362
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
363
        $childClass = new DDC2504ChildClass();
364
365
        // Test One to Many existence with state clear
366
        $this->_em->persist($childClass);
367
        $this->_em->flush();
368
369
        $queryCount = $this->getCurrentQueryCount();
370
        $this->assertFalse($otherClass->childClasses->contains($childClass));
371
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Checking for contains of persisted entity should cause one query to be executed.");
372
        $this->assertFalse($otherClass->childClasses->isInitialized(), "Post-Condition: Collection is not initialized.");
373
    }
374
375
    /**
376
     * @group DDC-2504
377
     */
378 View Code Duplication
    public function testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWithNewStateNotMatchingItem()
379
    {
380
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
381
        $childClass = new DDC2504ChildClass();
382
383
        $this->_em->persist($childClass);
384
385
        $queryCount = $this->getCurrentQueryCount();
386
387
        $this->assertFalse($otherClass->childClasses->contains($childClass));
388
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of managed entity (but not persisted) should cause no query to be executed.");
389
        $this->assertFalse($otherClass->childClasses->isInitialized(), "Post-Condition: Collection is not initialized.");
390
    }
391
392
    /**
393
     * @group DDC-2504
394
     */
395
    public function testCountingOnOneToManyJoinedInheritanceWillNotInitializeCollection()
396
    {
397
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
398
399
        $this->assertEquals(2, count($otherClass->childClasses));
400
401
        $this->assertFalse($otherClass->childClasses->isInitialized());
402
    }
403
404
    /**
405
     * @group DDC-546
406
     */
407
    public function testContainsManyToMany()
408
    {
409
        $user = $this->_em->find(CmsUser::class, $this->userId);
410
        $this->assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
411
412
        // Test Many to Many existence retrieved from DB
413
        $group      = $this->_em->find(CmsGroup::class, $this->groupId);
414
        $queryCount = $this->getCurrentQueryCount();
415
416
        $this->assertTrue($user->groups->contains($group));
417
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Checking for contains of managed entity should cause one query to be executed.");
418
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
419
420
        // Test Many to Many existence with state new
421
        $group = new CmsGroup();
422
        $group->name = "A New group!";
423
424
        $queryCount = $this->getCurrentQueryCount();
425
426
        $this->assertFalse($user->groups->contains($group));
427
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed.");
428
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
429
430
        // Test Many to Many existence with state clear
431
        $this->_em->persist($group);
432
        $this->_em->flush();
433
434
        $queryCount = $this->getCurrentQueryCount();
435
436
        $this->assertFalse($user->groups->contains($group));
437
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Checking for contains of persisted entity should cause one query to be executed.");
438
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
439
440
        // Test Many to Many existence with state managed
441
        $group = new CmsGroup();
442
        $group->name = "My managed group";
443
444
        $this->_em->persist($group);
445
446
        $queryCount = $this->getCurrentQueryCount();
447
448
        $this->assertFalse($user->groups->contains($group));
449
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of managed entity (but not persisted) should cause no query to be executed.");
450
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
451
    }
452
453
    /**
454
     * @group DDC-546
455
     */
456 View Code Duplication
    public function testContainsManyToManyInverse()
457
    {
458
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
459
        $this->assertFalse($group->users->isInitialized(), "Pre-Condition: Collection is not initialized.");
460
461
        $user = $this->_em->find(CmsUser::class, $this->userId);
462
463
        $queryCount = $this->getCurrentQueryCount();
464
        $this->assertTrue($group->users->contains($user));
465
        $this->assertEquals($queryCount+1, $this->getCurrentQueryCount(), "Checking for contains of managed entity should cause one query to be executed.");
466
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
467
468
        $newUser = new CmsUser();
469
        $newUser->name = "A New group!";
470
471
        $queryCount = $this->getCurrentQueryCount();
472
        $this->assertFalse($group->users->contains($newUser));
473
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed.");
474
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
475
    }
476
477
    /**
478
     *
479
     */
480
    public function testRemoveElementOneToMany()
481
    {
482
        $user = $this->_em->find(CmsUser::class, $this->userId);
483
        $this->assertFalse($user->articles->isInitialized(), "Pre-Condition: Collection is not initialized.");
484
485
        // Test One to Many removal with Entity retrieved from DB
486
        $article    = $this->_em->find(CmsArticle::class, $this->articleId);
487
        $queryCount = $this->getCurrentQueryCount();
488
489
        $user->articles->removeElement($article);
490
491
        $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
492
        $this->assertEquals($queryCount, $this->getCurrentQueryCount());
493
494
        // Test One to Many removal with Entity state as new
495
        $article = new CmsArticle();
496
        $article->topic = "Testnew";
497
        $article->text = "blub";
498
499
        $queryCount = $this->getCurrentQueryCount();
500
501
        $user->articles->removeElement($article);
502
503
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a new entity should cause no query to be executed.");
504
505
        // Test One to Many removal with Entity state as clean
506
        $this->_em->persist($article);
507
        $this->_em->flush();
508
509
        $queryCount = $this->getCurrentQueryCount();
510
511
        $user->articles->removeElement($article);
512
513
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a persisted entity will not cause queries when the owning side doesn't actually change.");
514
        $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
515
516
        // Test One to Many removal with Entity state as managed
517
        $article = new CmsArticle();
518
        $article->topic = "How to not fail anymore on tests";
519
        $article->text = "That is simple! Just write more tests!";
520
521
        $this->_em->persist($article);
522
523
        $queryCount = $this->getCurrentQueryCount();
524
525
        $user->articles->removeElement($article);
526
527
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a managed entity should cause no query to be executed.");
528
    }
529
530
    /**
531
     * @group DDC-2504
532
     */
533
    public function testRemovalOfManagedElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt()
534
    {
535
        /* @var $otherClass DDC2504OtherClass */
536
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
537
        /* @var $childClass DDC2504ChildClass */
538
        $childClass = $this->_em->find(DDC2504ChildClass::class, $this->ddc2504ChildClassId);
539
540
        $queryCount = $this->getCurrentQueryCount();
541
542
        $otherClass->childClasses->removeElement($childClass);
0 ignored issues
show
Bug introduced by
The method removeElement() does not seem to exist on object<Doctrine\Tests\Mo...2504\DDC2504ChildClass>.

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...
543
        $childClass->other = null; // updating owning side
544
545
        $this->assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
0 ignored issues
show
Bug introduced by
The method isInitialized() does not seem to exist on object<Doctrine\Tests\Mo...2504\DDC2504ChildClass>.

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...
546
547
        $this->assertEquals(
548
            $queryCount,
549
            $this->getCurrentQueryCount(),
550
            'No queries have been executed'
551
        );
552
553
        $this->assertTrue(
554
            $otherClass->childClasses->contains($childClass),
0 ignored issues
show
Bug introduced by
The method contains() does not seem to exist on object<Doctrine\Tests\Mo...2504\DDC2504ChildClass>.

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...
555
            'Collection item still not updated (needs flushing)'
556
        );
557
558
        $this->_em->flush();
559
560
        $this->assertFalse(
561
            $otherClass->childClasses->contains($childClass),
0 ignored issues
show
Bug introduced by
The method contains() does not seem to exist on object<Doctrine\Tests\Mo...2504\DDC2504ChildClass>.

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...
562
            'Referenced item was removed in the transaction'
563
        );
564
565
        $this->assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
0 ignored issues
show
Bug introduced by
The method isInitialized() does not seem to exist on object<Doctrine\Tests\Mo...2504\DDC2504ChildClass>.

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...
566
    }
567
568
    /**
569
     * @group DDC-2504
570
     */
571 View Code Duplication
    public function testRemovalOfNonManagedElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt()
572
    {
573
        /* @var $otherClass DDC2504OtherClass */
574
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
575
        $queryCount = $this->getCurrentQueryCount();
576
577
        $otherClass->childClasses->removeElement(new DDC2504ChildClass());
0 ignored issues
show
Bug introduced by
The method removeElement() does not seem to exist on object<Doctrine\Tests\Mo...2504\DDC2504ChildClass>.

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...
578
579
        $this->assertEquals(
580
            $queryCount,
581
            $this->getCurrentQueryCount(),
582
            'Removing an unmanaged entity should cause no query to be executed.'
583
        );
584
    }
585
586
    /**
587
     * @group DDC-2504
588
     */
589
    public function testRemovalOfNewElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt()
590
    {
591
        /* @var $otherClass DDC2504OtherClass */
592
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
593
        $childClass = new DDC2504ChildClass();
594
595
        $this->_em->persist($childClass);
596
597
        $queryCount = $this->getCurrentQueryCount();
598
599
        $otherClass->childClasses->removeElement($childClass);
0 ignored issues
show
Bug introduced by
The method removeElement() does not seem to exist on object<Doctrine\Tests\Mo...2504\DDC2504ChildClass>.

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...
600
601
        $this->assertEquals(
602
            $queryCount,
603
            $this->getCurrentQueryCount(),
604
            'Removing a new entity should cause no query to be executed.'
605
        );
606
    }
607
608
    /**
609
     * @group DDC-2504
610
     */
611 View Code Duplication
    public function testRemovalOfNewManagedElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt()
612
    {
613
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
614
        $childClass = new DDC2504ChildClass();
615
616
        $this->_em->persist($childClass);
617
        $this->_em->flush();
618
619
        $queryCount = $this->getCurrentQueryCount();
620
621
        $otherClass->childClasses->removeElement($childClass);
622
623
        $this->assertEquals(
624
            $queryCount,
625
            $this->getCurrentQueryCount(),
626
            'No queries are executed, as the owning side of the association is not actually updated.'
627
        );
628
        $this->assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
629
    }
630
631
    /**
632
     *
633
     */
634 View Code Duplication
    public function testRemoveElementManyToMany()
635
    {
636
        $user = $this->_em->find(CmsUser::class, $this->userId);
637
        $this->assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
638
639
        // Test Many to Many removal with Entity retrieved from DB
640
        $group      = $this->_em->find(CmsGroup::class, $this->groupId);
641
        $queryCount = $this->getCurrentQueryCount();
642
643
        $this->assertTrue($user->groups->removeElement($group));
644
645
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a persisted entity should cause one query to be executed.");
646
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
647
648
        $this->assertFalse($user->groups->removeElement($group), "Removing an already removed element returns false");
649
650
        // Test Many to Many removal with Entity state as new
651
        $group = new CmsGroup();
652
        $group->name = "A New group!";
653
654
        $queryCount = $this->getCurrentQueryCount();
655
656
        $user->groups->removeElement($group);
657
658
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing new entity should cause no query to be executed.");
659
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
660
661
        // Test Many to Many removal with Entity state as clean
662
        $this->_em->persist($group);
663
        $this->_em->flush();
664
665
        $queryCount = $this->getCurrentQueryCount();
666
667
        $user->groups->removeElement($group);
668
669
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a persisted entity should cause one query to be executed.");
670
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
671
672
        // Test Many to Many removal with Entity state as managed
673
        $group = new CmsGroup();
674
        $group->name = "A New group!";
675
676
        $this->_em->persist($group);
677
678
        $queryCount = $this->getCurrentQueryCount();
679
680
        $user->groups->removeElement($group);
681
682
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a managed entity should cause no query to be executed.");
683
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
684
    }
685
686
    /**
687
     *
688
     */
689 View Code Duplication
    public function testRemoveElementManyToManyInverse()
690
    {
691
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
692
        $this->assertFalse($group->users->isInitialized(), "Pre-Condition: Collection is not initialized.");
693
694
        $user       = $this->_em->find(CmsUser::class, $this->userId);
695
        $queryCount = $this->getCurrentQueryCount();
696
697
        $group->users->removeElement($user);
698
699
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a managed entity should cause one query to be executed.");
700
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
701
702
        $newUser = new CmsUser();
703
        $newUser->name = "A New group!";
704
705
        $queryCount = $this->getCurrentQueryCount();
706
707
        $group->users->removeElement($newUser);
708
709
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a new entity should cause no query to be executed.");
710
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
711
    }
712
713
    /**
714
     * @group DDC-1399
715
     */
716
    public function testCountAfterAddThenFlush()
717
    {
718
        $user = $this->_em->find(CmsUser::class, $this->userId);
719
720
        $newGroup = new CmsGroup();
721
        $newGroup->name = "Test4";
722
723
        $user->addGroup($newGroup);
724
        $this->_em->persist($newGroup);
725
726
        $this->assertFalse($user->groups->isInitialized());
727
        $this->assertEquals(4, count($user->groups));
728
        $this->assertFalse($user->groups->isInitialized());
729
730
        $this->_em->flush();
731
732
        $this->assertEquals(4, count($user->groups));
733
    }
734
735
    /**
736
     * @group DDC-1462
737
     * @group non-cacheable
738
     */
739
    public function testSliceOnDirtyCollection()
740
    {
741
        $user = $this->_em->find(CmsUser::class, $this->userId);
742
        /* @var $user CmsUser */
743
744
        $newGroup = new CmsGroup();
745
        $newGroup->name = "Test4";
746
747
        $user->addGroup($newGroup);
748
        $this->_em->persist($newGroup);
749
750
        $qc = $this->getCurrentQueryCount();
751
        $groups = $user->groups->slice(0, 10);
752
753
        $this->assertEquals(4, count($groups));
754
        $this->assertEquals($qc + 1, $this->getCurrentQueryCount());
755
    }
756
757
    /**
758
     * @group DDC-1398
759
     * @group non-cacheable
760
     */
761
    public function testGetIndexByIdentifier()
762
    {
763
        $user = $this->_em->find(CmsUser::class, $this->userId);
764
        /* @var $user CmsUser */
765
766
        $queryCount = $this->getCurrentQueryCount();
767
        $phonenumber = $user->phonenumbers->get($this->phonenumber);
768
769
        $this->assertFalse($user->phonenumbers->isInitialized());
0 ignored issues
show
Bug introduced by
The method isInitialized() does not seem to exist on object<Doctrine\Common\C...ctions\ArrayCollection>.

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...
770
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
771
        $this->assertSame($phonenumber, $this->_em->find(CmsPhonenumber::class, $this->phonenumber));
772
773
        $article = $user->phonenumbers->get($this->phonenumber);
0 ignored issues
show
Unused Code introduced by
$article is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
774
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Getting the same entity should not cause an extra query to be executed");
775
    }
776
777
    /**
778
     * @group DDC-1398
779
     */
780
    public function testGetIndexByOneToMany()
781
    {
782
        $user = $this->_em->find(CmsUser::class, $this->userId);
783
        /* @var $user CmsUser */
784
785
        $queryCount = $this->getCurrentQueryCount();
786
787
        $article = $user->articles->get($this->topic);
788
789
        $this->assertFalse($user->articles->isInitialized());
0 ignored issues
show
Bug introduced by
The method isInitialized() does not seem to exist on object<Doctrine\Common\C...ctions\ArrayCollection>.

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...
790
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
791
        $this->assertSame($article, $this->_em->find(CmsArticle::class, $this->articleId));
792
    }
793
794
    /**
795
     * @group DDC-1398
796
     */
797
    public function testGetIndexByManyToManyInverseSide()
798
    {
799
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
800
        /* @var $group CmsGroup */
801
802
        $queryCount = $this->getCurrentQueryCount();
803
804
        $user = $group->users->get($this->username);
805
806
        $this->assertFalse($group->users->isInitialized());
0 ignored issues
show
Bug introduced by
The method isInitialized() does not seem to exist on object<Doctrine\Common\C...ctions\ArrayCollection>.

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...
807
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
808
        $this->assertSame($user, $this->_em->find(CmsUser::class, $this->userId));
809
    }
810
811
    /**
812
     * @group DDC-1398
813
     */
814 View Code Duplication
    public function testGetIndexByManyToManyOwningSide()
815
    {
816
        $user = $this->_em->find(CmsUser::class, $this->userId);
817
        /* @var $user CmsUser */
818
819
        $queryCount = $this->getCurrentQueryCount();
820
821
        $group = $user->groups->get($this->groupname);
822
823
        $this->assertFalse($user->groups->isInitialized());
0 ignored issues
show
Bug introduced by
The method isInitialized() does not seem to exist on object<Doctrine\Common\C...ctions\ArrayCollection>.

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...
824
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
825
        $this->assertSame($group, $this->_em->find(CmsGroup::class, $this->groupId));
826
    }
827
828
    /**
829
     * @group DDC-1398
830
     */
831
    public function testGetNonExistentIndexBy()
832
    {
833
        $user = $this->_em->find(CmsUser::class, $this->userId);
834
        $this->assertNull($user->articles->get(-1));
835
        $this->assertNull($user->groups->get(-1));
836
    }
837
838 View Code Duplication
    public function testContainsKeyIndexByOneToMany()
839
    {
840
        $user = $this->_em->find(CmsUser::class, $this->userId);
841
        /* @var $user CmsUser */
842
843
        $queryCount = $this->getCurrentQueryCount();
844
845
        $contains = $user->articles->containsKey($this->topic);
846
847
        $this->assertTrue($contains);
848
        $this->assertFalse($user->articles->isInitialized());
0 ignored issues
show
Bug introduced by
The method isInitialized() does not seem to exist on object<Doctrine\Common\C...ctions\ArrayCollection>.

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...
849
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
850
    }
851
852 View Code Duplication
    public function testContainsKeyIndexByOneToManyJoinedInheritance()
853
    {
854
        $class = $this->_em->getClassMetadata(DDC2504OtherClass::class);
855
        $class->associationMappings['childClasses']['indexBy'] = 'id';
856
857
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
858
859
        $queryCount = $this->getCurrentQueryCount();
860
861
        $contains = $otherClass->childClasses->containsKey($this->ddc2504ChildClassId);
862
863
        $this->assertTrue($contains);
864
        $this->assertFalse($otherClass->childClasses->isInitialized());
865
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
866
    }
867
868 View Code Duplication
    public function testContainsKeyIndexByManyToMany()
869
    {
870
        $user = $this->_em->find(CmsUser::class, $this->userId2);
871
872
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
873
874
        $queryCount = $this->getCurrentQueryCount();
875
876
        $contains = $user->groups->containsKey($group->name);
877
878
        $this->assertTrue($contains, "The item is not into collection");
879
        $this->assertFalse($user->groups->isInitialized(), "The collection must not be initialized");
880
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
881
    }
882 View Code Duplication
    public function testContainsKeyIndexByManyToManyNonOwning()
883
    {
884
        $user = $this->_em->find(CmsUser::class, $this->userId2);
885
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
886
887
        $queryCount = $this->getCurrentQueryCount();
888
889
        $contains = $group->users->containsKey($user->username);
890
891
        $this->assertTrue($contains, "The item is not into collection");
892
        $this->assertFalse($group->users->isInitialized(), "The collection must not be initialized");
893
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
894
    }
895
896 View Code Duplication
    public function testContainsKeyIndexByWithPkManyToMany()
897
    {
898
        $class = $this->_em->getClassMetadata(CmsUser::class);
899
        $class->associationMappings['groups']['indexBy'] = 'id';
900
901
        $user = $this->_em->find(CmsUser::class, $this->userId2);
902
903
        $queryCount = $this->getCurrentQueryCount();
904
905
        $contains = $user->groups->containsKey($this->groupId);
906
907
        $this->assertTrue($contains, "The item is not into collection");
908
        $this->assertFalse($user->groups->isInitialized(), "The collection must not be initialized");
909
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
910
    }
911 View Code Duplication
    public function testContainsKeyIndexByWithPkManyToManyNonOwning()
912
    {
913
        $class = $this->_em->getClassMetadata(CmsGroup::class);
914
        $class->associationMappings['users']['indexBy'] = 'id';
915
916
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
917
918
        $queryCount = $this->getCurrentQueryCount();
919
920
        $contains = $group->users->containsKey($this->userId2);
921
922
        $this->assertTrue($contains, "The item is not into collection");
923
        $this->assertFalse($group->users->isInitialized(), "The collection must not be initialized");
924
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
925
    }
926
927 View Code Duplication
    public function testContainsKeyNonExistentIndexByOneToMany()
928
    {
929
        $user = $this->_em->find(CmsUser::class, $this->userId2);
930
931
        $queryCount = $this->getCurrentQueryCount();
932
933
        $contains = $user->articles->containsKey("NonExistentTopic");
934
935
        $this->assertFalse($contains);
936
        $this->assertFalse($user->articles->isInitialized());
937
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
938
    }
939
940 View Code Duplication
    public function testContainsKeyNonExistentIndexByManyToMany()
941
    {
942
        $user = $this->_em->find(CmsUser::class, $this->userId2);
943
944
945
        $queryCount = $this->getCurrentQueryCount();
946
947
        $contains = $user->groups->containsKey("NonExistentTopic");
948
949
        $this->assertFalse($contains);
950
        $this->assertFalse($user->groups->isInitialized());
951
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
952
    }
953
954
    private function loadFixture()
955
    {
956
        $user1 = new CmsUser();
957
        $user1->username = "beberlei";
958
        $user1->name = "Benjamin";
959
        $user1->status = "active";
960
961
        $user2 = new CmsUser();
962
        $user2->username = "jwage";
963
        $user2->name = "Jonathan";
964
        $user2->status = "active";
965
966
        $user3 = new CmsUser();
967
        $user3->username = "romanb";
968
        $user3->name = "Roman";
969
        $user3->status = "active";
970
971
        $user4 = new CmsUser();
972
        $user4->username = "gblanco";
973
        $user4->name = "Guilherme";
974
        $user4->status = "active";
975
976
        $this->_em->persist($user1);
977
        $this->_em->persist($user2);
978
        $this->_em->persist($user3);
979
        $this->_em->persist($user4);
980
981
        $group1 = new CmsGroup();
982
        $group1->name = "Test1";
983
984
        $group2 = new CmsGroup();
985
        $group2->name = "Test2";
986
987
        $group3 = new CmsGroup();
988
        $group3->name = "Test3";
989
990
        $user1->addGroup($group1);
991
        $user1->addGroup($group2);
992
        $user1->addGroup($group3);
993
994
        $user2->addGroup($group1);
995
        $user3->addGroup($group1);
996
        $user4->addGroup($group1);
997
998
        $this->_em->persist($group1);
999
        $this->_em->persist($group2);
1000
        $this->_em->persist($group3);
1001
1002
        $article1 = new CmsArticle();
1003
        $article1->topic = "Test1";
1004
        $article1->text = "Test1";
1005
        $article1->setAuthor($user1);
1006
1007
        $article2 = new CmsArticle();
1008
        $article2->topic = "Test2";
1009
        $article2->text = "Test2";
1010
        $article2->setAuthor($user1);
1011
1012
        $this->_em->persist($article1);
1013
        $this->_em->persist($article2);
1014
1015
        $phonenumber1 = new CmsPhonenumber();
1016
        $phonenumber1->phonenumber = '12345';
1017
1018
        $phonenumber2 = new CmsPhonenumber();
1019
        $phonenumber2->phonenumber = '67890';
1020
1021
        $this->_em->persist($phonenumber1);
1022
        $this->_em->persist($phonenumber2);
1023
1024
        $user1->addPhonenumber($phonenumber1);
1025
1026
        // DDC-2504
1027
        $otherClass = new DDC2504OtherClass();
1028
        $childClass1 = new DDC2504ChildClass();
1029
        $childClass2 = new DDC2504ChildClass();
1030
1031
        $childClass1->other = $otherClass;
1032
        $childClass2->other = $otherClass;
1033
1034
        $otherClass->childClasses[] = $childClass1;
1035
        $otherClass->childClasses[] = $childClass2;
1036
1037
        $this->_em->persist($childClass1);
1038
        $this->_em->persist($childClass2);
1039
        $this->_em->persist($otherClass);
1040
1041
        $this->_em->flush();
1042
        $this->_em->clear();
1043
1044
        $this->articleId = $article1->id;
1045
        $this->userId = $user1->getId();
1046
        $this->userId2 = $user2->getId();
1047
        $this->groupId = $group1->id;
1048
        $this->ddc2504OtherClassId = $otherClass->id;
1049
        $this->ddc2504ChildClassId = $childClass1->id;
1050
1051
        $this->username = $user1->username;
1052
        $this->groupname = $group1->name;
1053
        $this->topic = $article1->topic;
1054
        $this->phonenumber = $phonenumber1->phonenumber;
1055
1056
    }
1057
1058
    /**
1059
     * @group DDC-3343
1060
     */
1061
    public function testRemoveManagedElementFromOneToManyExtraLazyCollectionIsNoOp()
1062
    {
1063
        list($userId, $tweetId) = $this->loadTweetFixture();
1064
1065
        /* @var $user User */
1066
        $user = $this->_em->find(User::class, $userId);
1067
1068
        $user->tweets->removeElement($this->_em->find(Tweet::class, $tweetId));
1069
1070
        $this->_em->clear();
1071
1072
        /* @var $user User */
1073
        $user = $this->_em->find(User::class, $userId);
1074
1075
        $this->assertCount(1, $user->tweets, 'Element was not removed - need to update the owning side first');
1076
    }
1077
1078
    /**
1079
     * @group DDC-3343
1080
     */
1081
    public function testRemoveManagedElementFromOneToManyExtraLazyCollectionWithoutDeletingTheTargetEntityEntryIsNoOp()
1082
    {
1083
        list($userId, $tweetId) = $this->loadTweetFixture();
1084
1085
        /* @var $user User */
1086
        $user  = $this->_em->find(User::class, $userId);
1087
        $tweet = $this->_em->find(Tweet::class, $tweetId);
1088
1089
        $user->tweets->removeElement($tweet);
1090
1091
        $this->_em->clear();
1092
1093
        /* @var $tweet Tweet */
1094
        $tweet = $this->_em->find(Tweet::class, $tweetId);
1095
        $this->assertInstanceOf(
1096
            Tweet::class,
1097
            $tweet,
1098
            'Even though the collection is extra lazy, the tweet should not have been deleted'
1099
        );
1100
1101
        $this->assertInstanceOf(
1102
            User::class,
1103
            $tweet->author,
1104
            'Tweet author link has not been removed - need to update the owning side first'
1105
        );
1106
    }
1107
1108
    /**
1109
     * @group DDC-3343
1110
     */
1111
    public function testRemovingManagedLazyProxyFromExtraLazyOneToManyDoesRemoveTheAssociationButNotTheEntity()
1112
    {
1113
        list($userId, $tweetId) = $this->loadTweetFixture();
1114
1115
        /* @var $user User */
1116
        $user  = $this->_em->find(User::class, $userId);
1117
        $tweet = $this->_em->getReference(Tweet::class, $tweetId);
1118
1119
        $user->tweets->removeElement($this->_em->getReference(Tweet::class, $tweetId));
1120
1121
        $this->_em->clear();
1122
1123
        /* @var $tweet Tweet */
1124
        $tweet = $this->_em->find(Tweet::class, $tweet->id);
1125
        $this->assertInstanceOf(
1126
            Tweet::class,
1127
            $tweet,
1128
            'Even though the collection is extra lazy, the tweet should not have been deleted'
1129
        );
1130
1131
        $this->assertInstanceOf(User::class, $tweet->author);
1132
1133
        /* @var $user User */
1134
        $user = $this->_em->find(User::class, $userId);
1135
1136
        $this->assertCount(1, $user->tweets, 'Element was not removed - need to update the owning side first');
1137
    }
1138
1139
    /**
1140
     * @group DDC-3343
1141
     */
1142 View Code Duplication
    public function testRemoveOrphanedManagedElementFromOneToManyExtraLazyCollection()
1143
    {
1144
        list($userId, $userListId) = $this->loadUserListFixture();
1145
1146
        /* @var $user User */
1147
        $user = $this->_em->find(User::class, $userId);
1148
1149
        $user->userLists->removeElement($this->_em->find(UserList::class, $userListId));
1150
1151
        $this->_em->clear();
1152
1153
        /* @var $user User */
1154
        $user = $this->_em->find(User::class, $userId);
1155
1156
        $this->assertCount(0, $user->userLists, 'Element was removed from association due to orphan removal');
1157
        $this->assertNull(
1158
            $this->_em->find(UserList::class, $userListId),
1159
            'Element was deleted due to orphan removal'
1160
        );
1161
    }
1162
1163
    /**
1164
     * @group DDC-3343
1165
     */
1166
    public function testRemoveOrphanedUnManagedElementFromOneToManyExtraLazyCollection()
1167
    {
1168
        list($userId, $userListId) = $this->loadUserListFixture();
1169
1170
        /* @var $user User */
1171
        $user = $this->_em->find(User::class, $userId);
1172
1173
        $user->userLists->removeElement(new UserList());
1174
1175
        $this->_em->clear();
1176
1177
        /* @var $userList UserList */
1178
        $userList = $this->_em->find(UserList::class, $userListId);
1179
        $this->assertInstanceOf(
1180
            UserList::class,
1181
            $userList,
1182
            'Even though the collection is extra lazy + orphan removal, the user list should not have been deleted'
1183
        );
1184
1185
        $this->assertInstanceOf(
1186
            User::class,
1187
            $userList->owner,
1188
            'User list to owner link has not been removed'
1189
        );
1190
    }
1191
1192
    /**
1193
     * @group DDC-3343
1194
     */
1195 View Code Duplication
    public function testRemoveOrphanedManagedLazyProxyFromExtraLazyOneToMany()
1196
    {
1197
        list($userId, $userListId) = $this->loadUserListFixture();
1198
1199
        /* @var $user User */
1200
        $user = $this->_em->find(User::class, $userId);
1201
1202
        $user->userLists->removeElement($this->_em->getReference(UserList::class, $userListId));
1203
1204
        $this->_em->clear();
1205
1206
        /* @var $user User */
1207
        $user = $this->_em->find(User::class, $userId);
1208
1209
        $this->assertCount(0, $user->userLists, 'Element was removed from association due to orphan removal');
1210
        $this->assertNull(
1211
            $this->_em->find(UserList::class, $userListId),
1212
            'Element was deleted due to orphan removal'
1213
        );
1214
    }
1215
1216
    /**
1217
     * @return int[] ordered tuple: user id and tweet id
1218
     */
1219 View Code Duplication
    private function loadTweetFixture()
1220
    {
1221
        $user  = new User();
1222
        $tweet = new Tweet();
1223
1224
        $user->name     = 'ocramius';
1225
        $tweet->content = 'The cat is on the table';
1226
1227
        $user->addTweet($tweet);
1228
1229
        $this->_em->persist($user);
1230
        $this->_em->persist($tweet);
1231
        $this->_em->flush();
1232
        $this->_em->clear();
1233
1234
        return [$user->id, $tweet->id];
1235
    }
1236
1237
    /**
1238
     * @return int[] ordered tuple: user id and user list id
1239
     */
1240 View Code Duplication
    private function loadUserListFixture()
1241
    {
1242
        $user     = new User();
1243
        $userList = new UserList();
1244
1245
        $user->name     = 'ocramius';
1246
        $userList->listName = 'PHP Developers to follow closely';
1247
1248
        $user->addUserList($userList);
1249
1250
        $this->_em->persist($user);
1251
        $this->_em->persist($userList);
1252
        $this->_em->flush();
1253
        $this->_em->clear();
1254
1255
        return [$user->id, $userList->id];
1256
    }
1257
}
1258