Completed
Push — master ( 3ca682...b22cda )
by Marco
09:29
created

ExtraLazyCollectionTest::testContainsManyToMany()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 45
rs 8.8571
cc 1
eloc 27
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional;
4
5
use Doctrine\ORM\Mapping\ClassMetadataInfo;
6
use Doctrine\Tests\Models\DDC2504\DDC2504ChildClass;
7
use Doctrine\Tests\Models\DDC2504\DDC2504OtherClass;
8
use Doctrine\Tests\Models\Tweet\Tweet;
9
use Doctrine\Tests\Models\Tweet\User;
10
use Doctrine\Tests\Models\Tweet\UserList;
11
use Doctrine\Tests\OrmFunctionalTestCase;
12
13
/**
14
 * Description of ExtraLazyCollectionTest
15
 *
16
 * @author beberlei
17
 */
18
class ExtraLazyCollectionTest extends OrmFunctionalTestCase
19
{
20
    private $userId;
21
    private $userId2;
22
    private $groupId;
23
    private $articleId;
24
    private $ddc2504OtherClassId;
25
    private $ddc2504ChildClassId;
26
27
    private $username;
28
    private $groupname;
29
    private $topic;
30
    private $phonenumber;
31
32
    public function setUp()
33
    {
34
        $this->useModelSet('tweet');
35
        $this->useModelSet('cms');
36
        $this->useModelSet('ddc2504');
37
        parent::setUp();
38
39
        $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
40
        $class->associationMappings['groups']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
41
        $class->associationMappings['groups']['indexBy'] = 'name';
42
        $class->associationMappings['articles']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
43
        $class->associationMappings['articles']['indexBy'] = 'topic';
44
        $class->associationMappings['phonenumbers']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
45
        $class->associationMappings['phonenumbers']['indexBy'] = 'phonenumber';
46
47
        unset($class->associationMappings['phonenumbers']['cache']);
48
        unset($class->associationMappings['articles']['cache']);
49
        unset($class->associationMappings['users']['cache']);
50
51
        $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsGroup');
52
        $class->associationMappings['users']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
53
        $class->associationMappings['users']['indexBy'] = 'username';
54
55
        $this->loadFixture();
56
    }
57
58
    public function tearDown()
59
    {
60
        parent::tearDown();
61
62
        $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
63
        $class->associationMappings['groups']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
64
        $class->associationMappings['articles']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
65
        $class->associationMappings['phonenumbers']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
66
67
        unset($class->associationMappings['groups']['indexBy']);
68
        unset($class->associationMappings['articles']['indexBy']);
69
        unset($class->associationMappings['phonenumbers']['indexBy']);
70
71
        $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsGroup');
72
        $class->associationMappings['users']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
73
74
        unset($class->associationMappings['users']['indexBy']);
75
    }
76
77
    /**
78
     * @group DDC-546
79
     * @group non-cacheable
80
     */
81
    public function testCountNotInitializesCollection()
82
    {
83
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
84
        $queryCount = $this->getCurrentQueryCount();
85
86
        $this->assertFalse($user->groups->isInitialized());
87
        $this->assertEquals(3, count($user->groups));
88
        $this->assertFalse($user->groups->isInitialized());
89
90
        foreach ($user->groups AS $group) { }
91
92
        $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration.");
93
    }
94
95
    /**
96
     * @group DDC-546
97
     */
98
    public function testCountWhenNewEntityPresent()
99
    {
100
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
101
102
        $newGroup = new \Doctrine\Tests\Models\CMS\CmsGroup();
103
        $newGroup->name = "Test4";
104
105
        $user->addGroup($newGroup);
106
        $this->_em->persist($newGroup);
107
108
        $this->assertFalse($user->groups->isInitialized());
109
        $this->assertEquals(4, count($user->groups));
110
        $this->assertFalse($user->groups->isInitialized());
111
    }
112
113
    /**
114
     * @group DDC-546
115
     * @group non-cacheable
116
     */
117
    public function testCountWhenInitialized()
118
    {
119
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
120
        $queryCount = $this->getCurrentQueryCount();
121
122
        foreach ($user->groups AS $group) { }
123
124
        $this->assertTrue($user->groups->isInitialized());
125
        $this->assertEquals(3, count($user->groups));
126
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Should only execute one query to initialize collection, no extra query for count() more.");
127
    }
128
129
    /**
130
     * @group DDC-546
131
     */
132
    public function testCountInverseCollection()
133
    {
134
        $group = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
135
        $this->assertFalse($group->users->isInitialized(), "Pre-Condition");
136
137
        $this->assertEquals(4, count($group->users));
138
        $this->assertFalse($group->users->isInitialized(), "Extra Lazy collection should not be initialized by counting the collection.");
139
    }
140
141
    /**
142
     * @group DDC-546
143
     */
144
    public function testCountOneToMany()
145
    {
146
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
147
        $this->assertFalse($user->groups->isInitialized(), "Pre-Condition");
148
149
        $this->assertEquals(2, count($user->articles));
150
    }
151
152
    /**
153
     * @group DDC-2504
154
     */
155
    public function testCountOneToManyJoinedInheritance()
156
    {
157
        $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId);
158
159
        $this->assertFalse($otherClass->childClasses->isInitialized(), "Pre-Condition");
160
        $this->assertEquals(2, count($otherClass->childClasses));
161
    }
162
163
    /**
164
     * @group DDC-546
165
     */
166
    public function testFullSlice()
167
    {
168
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
169
        $this->assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
170
171
        $someGroups = $user->groups->slice(null);
172
        $this->assertEquals(3, count($someGroups));
173
    }
174
175
    /**
176
     * @group DDC-546
177
     * @group non-cacheable
178
     */
179
    public function testSlice()
180
    {
181
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
182
        $this->assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
183
184
        $queryCount = $this->getCurrentQueryCount();
185
186
        $someGroups = $user->groups->slice(0, 2);
187
188
        $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsGroup', $someGroups);
189
        $this->assertEquals(2, count($someGroups));
190
        $this->assertFalse($user->groups->isInitialized(), "Slice should not initialize the collection if it wasn't before!");
191
192
        $otherGroup = $user->groups->slice(2, 1);
193
194
        $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsGroup', $otherGroup);
195
        $this->assertEquals(1, count($otherGroup));
196
        $this->assertFalse($user->groups->isInitialized());
197
198
        foreach ($user->groups AS $group) { }
199
200
        $this->assertTrue($user->groups->isInitialized());
201
        $this->assertEquals(3, count($user->groups));
202
203
        $this->assertEquals($queryCount + 3, $this->getCurrentQueryCount());
204
    }
205
206
    /**
207
     * @group DDC-546
208
     * @group non-cacheable
209
     */
210
    public function testSliceInitializedCollection()
211
    {
212
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
213
        $queryCount = $this->getCurrentQueryCount();
214
215
        foreach ($user->groups AS $group) { }
216
217
        $someGroups = $user->groups->slice(0, 2);
218
219
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
220
221
        $this->assertEquals(2, count($someGroups));
222
        $this->assertTrue($user->groups->contains(array_shift($someGroups)));
223
        $this->assertTrue($user->groups->contains(array_shift($someGroups)));
224
    }
225
226
    /**
227
     * @group DDC-546
228
     */
229
    public function testSliceInverseCollection()
230
    {
231
        $group = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
232
        $this->assertFalse($group->users->isInitialized(), "Pre-Condition");
233
        $queryCount = $this->getCurrentQueryCount();
234
235
        $someUsers = $group->users->slice(0, 2);
236
        $otherUsers = $group->users->slice(2, 2);
237
238
        $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsUser', $someUsers);
239
        $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsUser', $otherUsers);
240
        $this->assertEquals(2, count($someUsers));
241
        $this->assertEquals(2, count($otherUsers));
242
243
        // +2 queries executed by slice
244
        $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Slicing two parts should only execute two additional queries.");
245
    }
246
247
    /**
248
     * @group DDC-546
249
     */
250
    public function testSliceOneToMany()
251
    {
252
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
253
        $this->assertFalse($user->articles->isInitialized(), "Pre-Condition: Collection is not initialized.");
254
255
        $queryCount = $this->getCurrentQueryCount();
256
257
        $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...
258
        $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...
259
260
        $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount());
261
    }
262
263
    /**
264
     * @group DDC-546
265
     */
266
    public function testContainsOneToMany()
267
    {
268
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
269
        $this->assertFalse($user->articles->isInitialized(), "Pre-Condition: Collection is not initialized.");
270
271
        // Test One to Many existence retrieved from DB
272
        $article    = $this->_em->find('Doctrine\Tests\Models\CMS\CmsArticle', $this->articleId);
273
        $queryCount = $this->getCurrentQueryCount();
274
275
        $this->assertTrue($user->articles->contains($article));
276
        $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
277
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
278
279
        // Test One to Many existence with state new
280
        $article = new \Doctrine\Tests\Models\CMS\CmsArticle();
281
        $article->topic = "Testnew";
282
        $article->text = "blub";
283
284
        $queryCount = $this->getCurrentQueryCount();
285
        $this->assertFalse($user->articles->contains($article));
286
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed.");
287
288
        // Test One to Many existence with state clear
289
        $this->_em->persist($article);
290
        $this->_em->flush();
291
292
        $queryCount = $this->getCurrentQueryCount();
293
        $this->assertFalse($user->articles->contains($article));
294
        $this->assertEquals($queryCount+1, $this->getCurrentQueryCount(), "Checking for contains of persisted entity should cause one query to be executed.");
295
        $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
296
297
        // Test One to Many existence with state managed
298
        $article = new \Doctrine\Tests\Models\CMS\CmsArticle();
299
        $article->topic = "How to not fail anymore on tests";
300
        $article->text = "That is simple! Just write more tests!";
301
302
        $this->_em->persist($article);
303
304
        $queryCount = $this->getCurrentQueryCount();
305
306
        $this->assertFalse($user->articles->contains($article));
307
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of managed entity (but not persisted) should cause no query to be executed.");
308
        $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
309
    }
310
311
    /**
312
     * @group DDC-2504
313
     */
314
    public function testLazyOneToManyJoinedInheritanceIsLazilyInitialized()
315
    {
316
        $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId);
317
318
        $this->assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
319
    }
320
321
    /**
322
     * @group DDC-2504
323
     */
324
    public function testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWhenMatchingItemIsFound()
325
    {
326
        $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId);
327
328
        // Test One to Many existence retrieved from DB
329
        $childClass = $this->_em->find(DDC2504ChildClass::CLASSNAME, $this->ddc2504ChildClassId);
330
        $queryCount = $this->getCurrentQueryCount();
331
332
        $this->assertTrue($otherClass->childClasses->contains($childClass));
333
        $this->assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
334
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), 'Search operation was performed via SQL');
335
    }
336
337
    /**
338
     * @group DDC-2504
339
     */
340
    public function testContainsOnOneToManyJoinedInheritanceWillNotCauseQueriesWhenNonPersistentItemIsMatched()
341
    {
342
        $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId);
343
        $queryCount = $this->getCurrentQueryCount();
344
345
        $this->assertFalse($otherClass->childClasses->contains(new DDC2504ChildClass()));
346
        $this->assertEquals(
347
            $queryCount,
348
            $this->getCurrentQueryCount(),
349
            'Checking for contains of new entity should cause no query to be executed.'
350
        );
351
    }
352
353
    /**
354
     * @group DDC-2504
355
     */
356
    public function testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWithClearStateMatchingItem()
357
    {
358
        $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId);
359
        $childClass = new DDC2504ChildClass();
360
361
        // Test One to Many existence with state clear
362
        $this->_em->persist($childClass);
363
        $this->_em->flush();
364
365
        $queryCount = $this->getCurrentQueryCount();
366
        $this->assertFalse($otherClass->childClasses->contains($childClass));
367
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Checking for contains of persisted entity should cause one query to be executed.");
368
        $this->assertFalse($otherClass->childClasses->isInitialized(), "Post-Condition: Collection is not initialized.");
369
    }
370
371
    /**
372
     * @group DDC-2504
373
     */
374
    public function testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWithNewStateNotMatchingItem()
375
    {
376
        $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId);
377
        $childClass = new DDC2504ChildClass();
378
379
        $this->_em->persist($childClass);
380
381
        $queryCount = $this->getCurrentQueryCount();
382
383
        $this->assertFalse($otherClass->childClasses->contains($childClass));
384
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of managed entity (but not persisted) should cause no query to be executed.");
385
        $this->assertFalse($otherClass->childClasses->isInitialized(), "Post-Condition: Collection is not initialized.");
386
    }
387
388
    /**
389
     * @group DDC-2504
390
     */
391
    public function testCountingOnOneToManyJoinedInheritanceWillNotInitializeCollection()
392
    {
393
        $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId);
394
395
        $this->assertEquals(2, count($otherClass->childClasses));
396
397
        $this->assertFalse($otherClass->childClasses->isInitialized());
398
    }
399
400
    /**
401
     * @group DDC-546
402
     */
403
    public function testContainsManyToMany()
404
    {
405
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
406
        $this->assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
407
408
        // Test Many to Many existence retrieved from DB
409
        $group      = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
410
        $queryCount = $this->getCurrentQueryCount();
411
412
        $this->assertTrue($user->groups->contains($group));
413
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Checking for contains of managed entity should cause one query to be executed.");
414
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
415
416
        // Test Many to Many existence with state new
417
        $group = new \Doctrine\Tests\Models\CMS\CmsGroup();
418
        $group->name = "A New group!";
419
420
        $queryCount = $this->getCurrentQueryCount();
421
422
        $this->assertFalse($user->groups->contains($group));
423
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed.");
424
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
425
426
        // Test Many to Many existence with state clear
427
        $this->_em->persist($group);
428
        $this->_em->flush();
429
430
        $queryCount = $this->getCurrentQueryCount();
431
432
        $this->assertFalse($user->groups->contains($group));
433
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Checking for contains of persisted entity should cause one query to be executed.");
434
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
435
436
        // Test Many to Many existence with state managed
437
        $group = new \Doctrine\Tests\Models\CMS\CmsGroup();
438
        $group->name = "My managed group";
439
440
        $this->_em->persist($group);
441
442
        $queryCount = $this->getCurrentQueryCount();
443
444
        $this->assertFalse($user->groups->contains($group));
445
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of managed entity (but not persisted) should cause no query to be executed.");
446
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
447
    }
448
449
    /**
450
     * @group DDC-546
451
     */
452
    public function testContainsManyToManyInverse()
453
    {
454
        $group = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
455
        $this->assertFalse($group->users->isInitialized(), "Pre-Condition: Collection is not initialized.");
456
457
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
458
459
        $queryCount = $this->getCurrentQueryCount();
460
        $this->assertTrue($group->users->contains($user));
461
        $this->assertEquals($queryCount+1, $this->getCurrentQueryCount(), "Checking for contains of managed entity should cause one query to be executed.");
462
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
463
464
        $newUser = new \Doctrine\Tests\Models\CMS\CmsUser();
465
        $newUser->name = "A New group!";
466
467
        $queryCount = $this->getCurrentQueryCount();
468
        $this->assertFalse($group->users->contains($newUser));
469
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed.");
470
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
471
    }
472
473
    /**
474
     *
475
     */
476
    public function testRemoveElementOneToMany()
477
    {
478
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
479
        $this->assertFalse($user->articles->isInitialized(), "Pre-Condition: Collection is not initialized.");
480
481
        // Test One to Many removal with Entity retrieved from DB
482
        $article    = $this->_em->find('Doctrine\Tests\Models\CMS\CmsArticle', $this->articleId);
483
        $queryCount = $this->getCurrentQueryCount();
484
485
        $user->articles->removeElement($article);
486
487
        $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
488
        $this->assertEquals($queryCount, $this->getCurrentQueryCount());
489
490
        // Test One to Many removal with Entity state as new
491
        $article = new \Doctrine\Tests\Models\CMS\CmsArticle();
492
        $article->topic = "Testnew";
493
        $article->text = "blub";
494
495
        $queryCount = $this->getCurrentQueryCount();
496
497
        $user->articles->removeElement($article);
498
499
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a new entity should cause no query to be executed.");
500
501
        // Test One to Many removal with Entity state as clean
502
        $this->_em->persist($article);
503
        $this->_em->flush();
504
505
        $queryCount = $this->getCurrentQueryCount();
506
507
        $user->articles->removeElement($article);
508
509
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a persisted entity will not cause queries when the owning side doesn't actually change.");
510
        $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
511
512
        // Test One to Many removal with Entity state as managed
513
        $article = new \Doctrine\Tests\Models\CMS\CmsArticle();
514
        $article->topic = "How to not fail anymore on tests";
515
        $article->text = "That is simple! Just write more tests!";
516
517
        $this->_em->persist($article);
518
519
        $queryCount = $this->getCurrentQueryCount();
520
521
        $user->articles->removeElement($article);
522
523
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a managed entity should cause no query to be executed.");
524
    }
525
526
    /**
527
     * @group DDC-2504
528
     */
529
    public function testRemovalOfManagedElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt()
530
    {
531
        /* @var $otherClass DDC2504OtherClass */
532
        $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId);
533
        /* @var $childClass DDC2504ChildClass */
534
        $childClass = $this->_em->find(DDC2504ChildClass::CLASSNAME, $this->ddc2504ChildClassId);
535
536
        $queryCount = $this->getCurrentQueryCount();
537
538
        $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...
539
        $childClass->other = null; // updating owning side
540
541
        $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...
542
543
        $this->assertEquals(
544
            $queryCount,
545
            $this->getCurrentQueryCount(),
546
            'No queries have been executed'
547
        );
548
549
        $this->assertTrue(
550
            $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...
551
            'Collection item still not updated (needs flushing)'
552
        );
553
554
        $this->_em->flush();
555
556
        $this->assertFalse(
557
            $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...
558
            'Referenced item was removed in the transaction'
559
        );
560
561
        $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...
562
    }
563
564
    /**
565
     * @group DDC-2504
566
     */
567
    public function testRemovalOfNonManagedElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt()
568
    {
569
        /* @var $otherClass DDC2504OtherClass */
570
        $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId);
571
        $queryCount = $this->getCurrentQueryCount();
572
573
        $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...
574
575
        $this->assertEquals(
576
            $queryCount,
577
            $this->getCurrentQueryCount(),
578
            'Removing an unmanaged entity should cause no query to be executed.'
579
        );
580
    }
581
582
    /**
583
     * @group DDC-2504
584
     */
585
    public function testRemovalOfNewElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt()
586
    {
587
        /* @var $otherClass DDC2504OtherClass */
588
        $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId);
589
        $childClass = new DDC2504ChildClass();
590
591
        $this->_em->persist($childClass);
592
593
        $queryCount = $this->getCurrentQueryCount();
594
595
        $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...
596
597
        $this->assertEquals(
598
            $queryCount,
599
            $this->getCurrentQueryCount(),
600
            'Removing a new entity should cause no query to be executed.'
601
        );
602
    }
603
604
    /**
605
     * @group DDC-2504
606
     */
607
    public function testRemovalOfNewManagedElementFromOneToManyJoinedInheritanceCollectionDoesNotInitializeIt()
608
    {
609
        $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId);
610
        $childClass = new DDC2504ChildClass();
611
612
        $this->_em->persist($childClass);
613
        $this->_em->flush();
614
615
        $queryCount = $this->getCurrentQueryCount();
616
617
        $otherClass->childClasses->removeElement($childClass);
618
619
        $this->assertEquals(
620
            $queryCount,
621
            $this->getCurrentQueryCount(),
622
            'No queries are executed, as the owning side of the association is not actually updated.'
623
        );
624
        $this->assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
625
    }
626
627
    /**
628
     *
629
     */
630
    public function testRemoveElementManyToMany()
631
    {
632
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
633
        $this->assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
634
635
        // Test Many to Many removal with Entity retrieved from DB
636
        $group      = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
637
        $queryCount = $this->getCurrentQueryCount();
638
639
        $user->groups->removeElement($group);
640
641
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a persisted entity should cause one query to be executed.");
642
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
643
644
        // Test Many to Many removal with Entity state as new
645
        $group = new \Doctrine\Tests\Models\CMS\CmsGroup();
646
        $group->name = "A New group!";
647
648
        $queryCount = $this->getCurrentQueryCount();
649
650
        $user->groups->removeElement($group);
651
652
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing new entity should cause no query to be executed.");
653
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
654
655
        // Test Many to Many removal with Entity state as clean
656
        $this->_em->persist($group);
657
        $this->_em->flush();
658
659
        $queryCount = $this->getCurrentQueryCount();
660
661
        $user->groups->removeElement($group);
662
663
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a persisted entity should cause one query to be executed.");
664
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
665
666
        // Test Many to Many removal with Entity state as managed
667
        $group = new \Doctrine\Tests\Models\CMS\CmsGroup();
668
        $group->name = "A New group!";
669
670
        $this->_em->persist($group);
671
672
        $queryCount = $this->getCurrentQueryCount();
673
674
        $user->groups->removeElement($group);
675
676
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a managed entity should cause no query to be executed.");
677
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
678
    }
679
680
    /**
681
     *
682
     */
683
    public function testRemoveElementManyToManyInverse()
684
    {
685
        $group = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
686
        $this->assertFalse($group->users->isInitialized(), "Pre-Condition: Collection is not initialized.");
687
688
        $user       = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
689
        $queryCount = $this->getCurrentQueryCount();
690
691
        $group->users->removeElement($user);
692
693
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a managed entity should cause one query to be executed.");
694
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
695
696
        $newUser = new \Doctrine\Tests\Models\CMS\CmsUser();
697
        $newUser->name = "A New group!";
698
699
        $queryCount = $this->getCurrentQueryCount();
700
701
        $group->users->removeElement($newUser);
702
703
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a new entity should cause no query to be executed.");
704
        $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
705
    }
706
707
    /**
708
     * @group DDC-1399
709
     */
710
    public function testCountAfterAddThenFlush()
711
    {
712
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
713
714
        $newGroup = new \Doctrine\Tests\Models\CMS\CmsGroup();
715
        $newGroup->name = "Test4";
716
717
        $user->addGroup($newGroup);
718
        $this->_em->persist($newGroup);
719
720
        $this->assertFalse($user->groups->isInitialized());
721
        $this->assertEquals(4, count($user->groups));
722
        $this->assertFalse($user->groups->isInitialized());
723
724
        $this->_em->flush();
725
726
        $this->assertEquals(4, count($user->groups));
727
    }
728
729
    /**
730
     * @group DDC-1462
731
     * @group non-cacheable
732
     */
733
    public function testSliceOnDirtyCollection()
734
    {
735
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
736
        /* @var $user CmsUser */
737
738
        $newGroup = new \Doctrine\Tests\Models\CMS\CmsGroup();
739
        $newGroup->name = "Test4";
740
741
        $user->addGroup($newGroup);
742
        $this->_em->persist($newGroup);
743
744
        $qc = $this->getCurrentQueryCount();
745
        $groups = $user->groups->slice(0, 10);
746
747
        $this->assertEquals(4, count($groups));
748
        $this->assertEquals($qc + 1, $this->getCurrentQueryCount());
749
    }
750
751
    /**
752
     * @group DDC-1398
753
     * @group non-cacheable
754
     */
755
    public function testGetIndexByIdentifier()
756
    {
757
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
758
        /* @var $user CmsUser */
759
760
        $queryCount = $this->getCurrentQueryCount();
761
        $phonenumber = $user->phonenumbers->get($this->phonenumber);
762
763
        $this->assertFalse($user->phonenumbers->isInitialized());
764
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
765
        $this->assertSame($phonenumber, $this->_em->find('Doctrine\Tests\Models\CMS\CmsPhonenumber', $this->phonenumber));
766
767
        $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...
768
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Getting the same entity should not cause an extra query to be executed");
769
    }
770
771
    /**
772
     * @group DDC-1398
773
     */
774
    public function testGetIndexByOneToMany()
775
    {
776
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
777
        /* @var $user CmsUser */
778
779
        $queryCount = $this->getCurrentQueryCount();
780
781
        $article = $user->articles->get($this->topic);
782
783
        $this->assertFalse($user->articles->isInitialized());
784
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
785
        $this->assertSame($article, $this->_em->find('Doctrine\Tests\Models\CMS\CmsArticle', $this->articleId));
786
    }
787
788
    /**
789
     * @group DDC-1398
790
     */
791
    public function testGetIndexByManyToManyInverseSide()
792
    {
793
        $group = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
794
        /* @var $group CmsGroup */
795
796
        $queryCount = $this->getCurrentQueryCount();
797
798
        $user = $group->users->get($this->username);
799
800
        $this->assertFalse($group->users->isInitialized());
801
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
802
        $this->assertSame($user, $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId));
803
    }
804
805
    /**
806
     * @group DDC-1398
807
     */
808
    public function testGetIndexByManyToManyOwningSide()
809
    {
810
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
811
        /* @var $user CmsUser */
812
813
        $queryCount = $this->getCurrentQueryCount();
814
815
        $group = $user->groups->get($this->groupname);
816
817
        $this->assertFalse($user->groups->isInitialized());
818
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
819
        $this->assertSame($group, $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId));
820
    }
821
822
    /**
823
     * @group DDC-1398
824
     */
825
    public function testGetNonExistentIndexBy()
826
    {
827
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
828
        $this->assertNull($user->articles->get(-1));
829
        $this->assertNull($user->groups->get(-1));
830
    }
831
832
    public function testContainsKeyIndexByOneToMany()
833
    {
834
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
835
        /* @var $user CmsUser */
836
837
        $queryCount = $this->getCurrentQueryCount();
838
839
        $contains = $user->articles->containsKey($this->topic);
840
841
        $this->assertTrue($contains);
842
        $this->assertFalse($user->articles->isInitialized());
843
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
844
    }
845
846
    public function testContainsKeyIndexByOneToManyJoinedInheritance()
847
    {
848
        $class = $this->_em->getClassMetadata(DDC2504OtherClass::CLASSNAME);
849
        $class->associationMappings['childClasses']['indexBy'] = 'id';
850
851
        $otherClass = $this->_em->find(DDC2504OtherClass::CLASSNAME, $this->ddc2504OtherClassId);
852
853
        $queryCount = $this->getCurrentQueryCount();
854
855
        $contains = $otherClass->childClasses->containsKey($this->ddc2504ChildClassId);
856
857
        $this->assertTrue($contains);
858
        $this->assertFalse($otherClass->childClasses->isInitialized());
859
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
860
    }
861
862
    public function testContainsKeyIndexByManyToMany()
863
    {
864
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId2);
865
866
        $group = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
867
868
        $queryCount = $this->getCurrentQueryCount();
869
870
        $contains = $user->groups->containsKey($group->name);
871
872
        $this->assertTrue($contains, "The item is not into collection");
873
        $this->assertFalse($user->groups->isInitialized(), "The collection must not be initialized");
874
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
875
    }
876
    public function testContainsKeyIndexByManyToManyNonOwning()
877
    {
878
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId2);
879
        $group = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
880
881
        $queryCount = $this->getCurrentQueryCount();
882
883
        $contains = $group->users->containsKey($user->username);
884
885
        $this->assertTrue($contains, "The item is not into collection");
886
        $this->assertFalse($group->users->isInitialized(), "The collection must not be initialized");
887
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
888
    }
889
890
    public function testContainsKeyIndexByWithPkManyToMany()
891
    {
892
        $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
893
        $class->associationMappings['groups']['indexBy'] = 'id';
894
895
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId2);
896
897
        $queryCount = $this->getCurrentQueryCount();
898
899
        $contains = $user->groups->containsKey($this->groupId);
900
901
        $this->assertTrue($contains, "The item is not into collection");
902
        $this->assertFalse($user->groups->isInitialized(), "The collection must not be initialized");
903
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
904
    }
905
    public function testContainsKeyIndexByWithPkManyToManyNonOwning()
906
    {
907
        $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsGroup');
908
        $class->associationMappings['users']['indexBy'] = 'id';
909
910
        $group = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
911
912
        $queryCount = $this->getCurrentQueryCount();
913
914
        $contains = $group->users->containsKey($this->userId2);
915
916
        $this->assertTrue($contains, "The item is not into collection");
917
        $this->assertFalse($group->users->isInitialized(), "The collection must not be initialized");
918
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
919
    }
920
921
    public function testContainsKeyNonExistentIndexByOneToMany()
922
    {
923
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId2);
924
925
        $queryCount = $this->getCurrentQueryCount();
926
927
        $contains = $user->articles->containsKey("NonExistentTopic");
928
929
        $this->assertFalse($contains);
930
        $this->assertFalse($user->articles->isInitialized());
931
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
932
    }
933
934
    public function testContainsKeyNonExistentIndexByManyToMany()
935
    {
936
        $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId2);
937
938
939
        $queryCount = $this->getCurrentQueryCount();
940
941
        $contains = $user->groups->containsKey("NonExistentTopic");
942
943
        $this->assertFalse($contains);
944
        $this->assertFalse($user->groups->isInitialized());
945
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
946
    }
947
948
    private function loadFixture()
949
    {
950
        $user1 = new \Doctrine\Tests\Models\CMS\CmsUser();
951
        $user1->username = "beberlei";
952
        $user1->name = "Benjamin";
953
        $user1->status = "active";
954
955
        $user2 = new \Doctrine\Tests\Models\CMS\CmsUser();
956
        $user2->username = "jwage";
957
        $user2->name = "Jonathan";
958
        $user2->status = "active";
959
960
        $user3 = new \Doctrine\Tests\Models\CMS\CmsUser();
961
        $user3->username = "romanb";
962
        $user3->name = "Roman";
963
        $user3->status = "active";
964
965
        $user4 = new \Doctrine\Tests\Models\CMS\CmsUser();
966
        $user4->username = "gblanco";
967
        $user4->name = "Guilherme";
968
        $user4->status = "active";
969
970
        $this->_em->persist($user1);
971
        $this->_em->persist($user2);
972
        $this->_em->persist($user3);
973
        $this->_em->persist($user4);
974
975
        $group1 = new \Doctrine\Tests\Models\CMS\CmsGroup();
976
        $group1->name = "Test1";
977
978
        $group2 = new \Doctrine\Tests\Models\CMS\CmsGroup();
979
        $group2->name = "Test2";
980
981
        $group3 = new \Doctrine\Tests\Models\CMS\CmsGroup();
982
        $group3->name = "Test3";
983
984
        $user1->addGroup($group1);
985
        $user1->addGroup($group2);
986
        $user1->addGroup($group3);
987
988
        $user2->addGroup($group1);
989
        $user3->addGroup($group1);
990
        $user4->addGroup($group1);
991
992
        $this->_em->persist($group1);
993
        $this->_em->persist($group2);
994
        $this->_em->persist($group3);
995
996
        $article1 = new \Doctrine\Tests\Models\CMS\CmsArticle();
997
        $article1->topic = "Test1";
998
        $article1->text = "Test1";
999
        $article1->setAuthor($user1);
1000
1001
        $article2 = new \Doctrine\Tests\Models\CMS\CmsArticle();
1002
        $article2->topic = "Test2";
1003
        $article2->text = "Test2";
1004
        $article2->setAuthor($user1);
1005
1006
        $this->_em->persist($article1);
1007
        $this->_em->persist($article2);
1008
1009
        $phonenumber1 = new \Doctrine\Tests\Models\CMS\CmsPhonenumber();
1010
        $phonenumber1->phonenumber = '12345';
1011
1012
        $phonenumber2 = new \Doctrine\Tests\Models\CMS\CmsPhonenumber();
1013
        $phonenumber2->phonenumber = '67890';
1014
1015
        $this->_em->persist($phonenumber1);
1016
        $this->_em->persist($phonenumber2);
1017
1018
        $user1->addPhonenumber($phonenumber1);
1019
1020
        // DDC-2504
1021
        $otherClass = new DDC2504OtherClass();
1022
        $childClass1 = new DDC2504ChildClass();
1023
        $childClass2 = new DDC2504ChildClass();
1024
1025
        $childClass1->other = $otherClass;
0 ignored issues
show
Documentation Bug introduced by
It seems like $otherClass of type object<Doctrine\Tests\Mo...2504\DDC2504OtherClass> is incompatible with the declared type object<Doctrine\Tests\Mo...\DDC\DDC2504OtherClass> of property $other.

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

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

Loading history...
1026
        $childClass2->other = $otherClass;
1027
1028
        $otherClass->childClasses[] = $childClass1;
1029
        $otherClass->childClasses[] = $childClass2;
1030
1031
        $this->_em->persist($childClass1);
1032
        $this->_em->persist($childClass2);
1033
        $this->_em->persist($otherClass);
1034
1035
        $this->_em->flush();
1036
        $this->_em->clear();
1037
1038
        $this->articleId = $article1->id;
1039
        $this->userId = $user1->getId();
1040
        $this->userId2 = $user2->getId();
1041
        $this->groupId = $group1->id;
1042
        $this->ddc2504OtherClassId = $otherClass->id;
1043
        $this->ddc2504ChildClassId = $childClass1->id;
1044
1045
        $this->username = $user1->username;
1046
        $this->groupname = $group1->name;
1047
        $this->topic = $article1->topic;
1048
        $this->phonenumber = $phonenumber1->phonenumber;
1049
1050
    }
1051
1052
    /**
1053
     * @group DDC-3343
1054
     */
1055
    public function testRemoveManagedElementFromOneToManyExtraLazyCollectionIsNoOp()
1056
    {
1057
        list($userId, $tweetId) = $this->loadTweetFixture();
1058
1059
        /* @var $user User */
1060
        $user = $this->_em->find(User::CLASSNAME, $userId);
1061
1062
        $user->tweets->removeElement($this->_em->find(Tweet::CLASSNAME, $tweetId));
1063
1064
        $this->_em->clear();
1065
1066
        /* @var $user User */
1067
        $user = $this->_em->find(User::CLASSNAME, $userId);
1068
1069
        $this->assertCount(1, $user->tweets, 'Element was not removed - need to update the owning side first');
1070
    }
1071
1072
    /**
1073
     * @group DDC-3343
1074
     */
1075
    public function testRemoveManagedElementFromOneToManyExtraLazyCollectionWithoutDeletingTheTargetEntityEntryIsNoOp()
1076
    {
1077
        list($userId, $tweetId) = $this->loadTweetFixture();
1078
1079
        /* @var $user User */
1080
        $user  = $this->_em->find(User::CLASSNAME, $userId);
1081
        $tweet = $this->_em->find(Tweet::CLASSNAME, $tweetId);
1082
1083
        $user->tweets->removeElement($tweet);
1084
1085
        $this->_em->clear();
1086
1087
        /* @var $tweet Tweet */
1088
        $tweet = $this->_em->find(Tweet::CLASSNAME, $tweetId);
1089
        $this->assertInstanceOf(
1090
            Tweet::CLASSNAME,
1091
            $tweet,
1092
            'Even though the collection is extra lazy, the tweet should not have been deleted'
1093
        );
1094
1095
        $this->assertInstanceOf(
1096
            User::CLASSNAME,
1097
            $tweet->author,
1098
            'Tweet author link has not been removed - need to update the owning side first'
1099
        );
1100
    }
1101
1102
    /**
1103
     * @group DDC-3343
1104
     */
1105
    public function testRemovingManagedLazyProxyFromExtraLazyOneToManyDoesRemoveTheAssociationButNotTheEntity()
1106
    {
1107
        list($userId, $tweetId) = $this->loadTweetFixture();
1108
1109
        /* @var $user User */
1110
        $user  = $this->_em->find(User::CLASSNAME, $userId);
1111
        $tweet = $this->_em->getReference(Tweet::CLASSNAME, $tweetId);
1112
1113
        $user->tweets->removeElement($this->_em->getReference(Tweet::CLASSNAME, $tweetId));
1114
1115
        $this->_em->clear();
1116
1117
        /* @var $tweet Tweet */
1118
        $tweet = $this->_em->find(Tweet::CLASSNAME, $tweet->id);
1119
        $this->assertInstanceOf(
1120
            Tweet::CLASSNAME,
1121
            $tweet,
1122
            'Even though the collection is extra lazy, the tweet should not have been deleted'
1123
        );
1124
1125
        $this->assertInstanceOf(User::CLASSNAME, $tweet->author);
1126
1127
        /* @var $user User */
1128
        $user = $this->_em->find(User::CLASSNAME, $userId);
1129
1130
        $this->assertCount(1, $user->tweets, 'Element was not removed - need to update the owning side first');
1131
    }
1132
1133
    /**
1134
     * @group DDC-3343
1135
     */
1136
    public function testRemoveOrphanedManagedElementFromOneToManyExtraLazyCollection()
1137
    {
1138
        list($userId, $userListId) = $this->loadUserListFixture();
1139
1140
        /* @var $user User */
1141
        $user = $this->_em->find(User::CLASSNAME, $userId);
1142
1143
        $user->userLists->removeElement($this->_em->find(UserList::CLASSNAME, $userListId));
1144
1145
        $this->_em->clear();
1146
1147
        /* @var $user User */
1148
        $user = $this->_em->find(User::CLASSNAME, $userId);
1149
1150
        $this->assertCount(0, $user->userLists, 'Element was removed from association due to orphan removal');
1151
        $this->assertNull(
1152
            $this->_em->find(UserList::CLASSNAME, $userListId),
1153
            'Element was deleted due to orphan removal'
1154
        );
1155
    }
1156
1157
    /**
1158
     * @group DDC-3343
1159
     */
1160
    public function testRemoveOrphanedUnManagedElementFromOneToManyExtraLazyCollection()
1161
    {
1162
        list($userId, $userListId) = $this->loadUserListFixture();
1163
1164
        /* @var $user User */
1165
        $user = $this->_em->find(User::CLASSNAME, $userId);
1166
1167
        $user->userLists->removeElement(new UserList());
1168
1169
        $this->_em->clear();
1170
1171
        /* @var $userList UserList */
1172
        $userList = $this->_em->find(UserList::CLASSNAME, $userListId);
1173
        $this->assertInstanceOf(
1174
            UserList::CLASSNAME,
1175
            $userList,
1176
            'Even though the collection is extra lazy + orphan removal, the user list should not have been deleted'
1177
        );
1178
1179
        $this->assertInstanceOf(
1180
            User::CLASSNAME,
1181
            $userList->owner,
1182
            'User list to owner link has not been removed'
1183
        );
1184
    }
1185
1186
    /**
1187
     * @group DDC-3343
1188
     */
1189
    public function testRemoveOrphanedManagedLazyProxyFromExtraLazyOneToMany()
1190
    {
1191
        list($userId, $userListId) = $this->loadUserListFixture();
1192
1193
        /* @var $user User */
1194
        $user = $this->_em->find(User::CLASSNAME, $userId);
1195
1196
        $user->userLists->removeElement($this->_em->getReference(UserList::CLASSNAME, $userListId));
1197
1198
        $this->_em->clear();
1199
1200
        /* @var $user User */
1201
        $user = $this->_em->find(User::CLASSNAME, $userId);
1202
1203
        $this->assertCount(0, $user->userLists, 'Element was removed from association due to orphan removal');
1204
        $this->assertNull(
1205
            $this->_em->find(UserList::CLASSNAME, $userListId),
1206
            'Element was deleted due to orphan removal'
1207
        );
1208
    }
1209
1210
    /**
1211
     * @return int[] ordered tuple: user id and tweet id
1212
     */
1213
    private function loadTweetFixture()
1214
    {
1215
        $user  = new User();
1216
        $tweet = new Tweet();
1217
1218
        $user->name     = 'ocramius';
1219
        $tweet->content = 'The cat is on the table';
1220
1221
        $user->addTweet($tweet);
1222
1223
        $this->_em->persist($user);
1224
        $this->_em->persist($tweet);
1225
        $this->_em->flush();
1226
        $this->_em->clear();
1227
1228
        return array($user->id, $tweet->id);
1229
    }
1230
1231
    /**
1232
     * @return int[] ordered tuple: user id and user list id
1233
     */
1234
    private function loadUserListFixture()
1235
    {
1236
        $user     = new User();
1237
        $userList = new UserList();
1238
1239
        $user->name     = 'ocramius';
1240
        $userList->listName = 'PHP Developers to follow closely';
1241
1242
        $user->addUserList($userList);
1243
1244
        $this->_em->persist($user);
1245
        $this->_em->persist($userList);
1246
        $this->_em->flush();
1247
        $this->_em->clear();
1248
1249
        return array($user->id, $userList->id);
1250
    }
1251
}
1252