Failed Conditions
Push — 2.8.x ( 0ee171...60c486 )
by Benjamin
22:42 queued 16:29
created

testCountWhenNewEntityPresent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional;
6
7
use Doctrine\ORM\Mapping\ClassMetadataInfo;
8
use Doctrine\Tests\Models\CMS\CmsArticle;
9
use Doctrine\Tests\Models\CMS\CmsGroup;
10
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
11
use Doctrine\Tests\Models\CMS\CmsUser;
12
use Doctrine\Tests\Models\DDC2504\DDC2504ChildClass;
13
use Doctrine\Tests\Models\DDC2504\DDC2504OtherClass;
14
use Doctrine\Tests\Models\Tweet\Tweet;
15
use Doctrine\Tests\Models\Tweet\User;
16
use Doctrine\Tests\Models\Tweet\UserList;
17
use Doctrine\Tests\OrmFunctionalTestCase;
18
use function array_shift;
19
use function count;
20
21
/**
22
 * Description of ExtraLazyCollectionTest
23
 */
24
class ExtraLazyCollectionTest extends OrmFunctionalTestCase
25
{
26
    private $userId;
27
    private $userId2;
28
    private $groupId;
29
    private $articleId;
30
    private $ddc2504OtherClassId;
31
    private $ddc2504ChildClassId;
32
33
    private $username;
34
    private $groupname;
35
    private $topic;
36
    private $phonenumber;
37
38
    public function setUp()
39
    {
40
        $this->useModelSet('tweet');
41
        $this->useModelSet('cms');
42
        $this->useModelSet('ddc2504');
43
        parent::setUp();
44
45
        $class                                                 = $this->_em->getClassMetadata(CmsUser::class);
46
        $class->associationMappings['groups']['fetch']         = ClassMetadataInfo::FETCH_EXTRA_LAZY;
47
        $class->associationMappings['groups']['indexBy']       = 'name';
48
        $class->associationMappings['articles']['fetch']       = ClassMetadataInfo::FETCH_EXTRA_LAZY;
49
        $class->associationMappings['articles']['indexBy']     = 'topic';
50
        $class->associationMappings['phonenumbers']['fetch']   = ClassMetadataInfo::FETCH_EXTRA_LAZY;
51
        $class->associationMappings['phonenumbers']['indexBy'] = 'phonenumber';
52
53
        unset($class->associationMappings['phonenumbers']['cache']);
54
        unset($class->associationMappings['articles']['cache']);
55
        unset($class->associationMappings['users']['cache']);
56
57
        $class                                          = $this->_em->getClassMetadata(CmsGroup::class);
58
        $class->associationMappings['users']['fetch']   = ClassMetadataInfo::FETCH_EXTRA_LAZY;
59
        $class->associationMappings['users']['indexBy'] = 'username';
60
61
        $this->loadFixture();
62
    }
63
64
    public function tearDown()
65
    {
66
        parent::tearDown();
67
68
        $class                                               = $this->_em->getClassMetadata(CmsUser::class);
69
        $class->associationMappings['groups']['fetch']       = ClassMetadataInfo::FETCH_LAZY;
70
        $class->associationMappings['articles']['fetch']     = ClassMetadataInfo::FETCH_LAZY;
71
        $class->associationMappings['phonenumbers']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
72
73
        unset($class->associationMappings['groups']['indexBy']);
74
        unset($class->associationMappings['articles']['indexBy']);
75
        unset($class->associationMappings['phonenumbers']['indexBy']);
76
77
        $class                                        = $this->_em->getClassMetadata(CmsGroup::class);
78
        $class->associationMappings['users']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
79
80
        unset($class->associationMappings['users']['indexBy']);
81
    }
82
83
    /**
84
     * @group DDC-546
85
     * @group non-cacheable
86
     */
87
    public function testCountNotInitializesCollection()
88
    {
89
        $user       = $this->_em->find(CmsUser::class, $this->userId);
90
        $queryCount = $this->getCurrentQueryCount();
91
92
        $this->assertFalse($user->groups->isInitialized());
0 ignored issues
show
Bug introduced by
The method isInitialized() does not exist on Doctrine\Common\Collections\ArrayCollection. ( Ignorable by Annotation )

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

92
        $this->assertFalse($user->groups->/** @scrutinizer ignore-call */ isInitialized());

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

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

Loading history...
93
        $this->assertEquals(3, count($user->groups));
94
        $this->assertFalse($user->groups->isInitialized());
95
96
        foreach ($user->groups as $group) {
97
        }
98
99
        $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), 'Expecting two queries to be fired for count, then iteration.');
100
    }
101
102
    /**
103
     * @group DDC-546
104
     */
105
    public function testCountWhenNewEntityPresent()
106
    {
107
        $user = $this->_em->find(CmsUser::class, $this->userId);
108
109
        $newGroup       = new CmsGroup();
110
        $newGroup->name = 'Test4';
111
112
        $user->addGroup($newGroup);
113
        $this->_em->persist($newGroup);
114
115
        $this->assertFalse($user->groups->isInitialized());
116
        $this->assertEquals(4, count($user->groups));
117
        $this->assertFalse($user->groups->isInitialized());
118
    }
119
120
    /**
121
     * @group DDC-546
122
     * @group non-cacheable
123
     */
124
    public function testCountWhenInitialized()
125
    {
126
        $user       = $this->_em->find(CmsUser::class, $this->userId);
127
        $queryCount = $this->getCurrentQueryCount();
128
129
        foreach ($user->groups as $group) {
130
        }
131
132
        $this->assertTrue($user->groups->isInitialized());
133
        $this->assertEquals(3, count($user->groups));
134
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), 'Should only execute one query to initialize collection, no extra query for count() more.');
135
    }
136
137
    /**
138
     * @group DDC-546
139
     */
140
    public function testCountInverseCollection()
141
    {
142
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
143
        $this->assertFalse($group->users->isInitialized(), 'Pre-Condition');
144
145
        $this->assertEquals(4, count($group->users));
146
        $this->assertFalse($group->users->isInitialized(), 'Extra Lazy collection should not be initialized by counting the collection.');
147
    }
148
149
    /**
150
     * @group DDC-546
151
     */
152
    public function testCountOneToMany()
153
    {
154
        $user = $this->_em->find(CmsUser::class, $this->userId);
155
        $this->assertFalse($user->groups->isInitialized(), 'Pre-Condition');
156
157
        $this->assertEquals(2, count($user->articles));
158
    }
159
160
    /**
161
     * @group DDC-2504
162
     */
163
    public function testCountOneToManyJoinedInheritance()
164
    {
165
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
166
167
        $this->assertFalse($otherClass->childClasses->isInitialized(), 'Pre-Condition');
0 ignored issues
show
Bug introduced by
The method isInitialized() does not exist on Doctrine\Tests\Models\DDC2504\DDC2504ChildClass. ( Ignorable by Annotation )

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

167
        $this->assertFalse($otherClass->childClasses->/** @scrutinizer ignore-call */ isInitialized(), 'Pre-Condition');

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...
168
        $this->assertEquals(2, count($otherClass->childClasses));
0 ignored issues
show
Bug introduced by
$otherClass->childClasses of type Doctrine\Tests\Models\DDC2504\DDC2504ChildClass is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

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

168
        $this->assertEquals(2, count(/** @scrutinizer ignore-type */ $otherClass->childClasses));
Loading history...
169
    }
170
171
    /**
172
     * @group DDC-546
173
     */
174
    public function testFullSlice()
175
    {
176
        $user = $this->_em->find(CmsUser::class, $this->userId);
177
        $this->assertFalse($user->groups->isInitialized(), 'Pre-Condition: Collection is not initialized.');
178
179
        $someGroups = $user->groups->slice(null);
180
        $this->assertEquals(3, count($someGroups));
181
    }
182
183
    /**
184
     * @group DDC-546
185
     * @group non-cacheable
186
     */
187
    public function testSlice()
188
    {
189
        $user = $this->_em->find(CmsUser::class, $this->userId);
190
        $this->assertFalse($user->groups->isInitialized(), 'Pre-Condition: Collection is not initialized.');
191
192
        $queryCount = $this->getCurrentQueryCount();
193
194
        $someGroups = $user->groups->slice(0, 2);
195
196
        $this->assertContainsOnly(CmsGroup::class, $someGroups);
197
        $this->assertEquals(2, count($someGroups));
198
        $this->assertFalse($user->groups->isInitialized(), "Slice should not initialize the collection if it wasn't before!");
199
200
        $otherGroup = $user->groups->slice(2, 1);
201
202
        $this->assertContainsOnly(CmsGroup::class, $otherGroup);
203
        $this->assertEquals(1, count($otherGroup));
204
        $this->assertFalse($user->groups->isInitialized());
205
206
        foreach ($user->groups as $group) {
207
        }
208
209
        $this->assertTrue($user->groups->isInitialized());
210
        $this->assertEquals(3, count($user->groups));
211
212
        $this->assertEquals($queryCount + 3, $this->getCurrentQueryCount());
213
    }
214
215
    /**
216
     * @group DDC-546
217
     * @group non-cacheable
218
     */
219
    public function testSliceInitializedCollection()
220
    {
221
        $user       = $this->_em->find(CmsUser::class, $this->userId);
222
        $queryCount = $this->getCurrentQueryCount();
223
224
        foreach ($user->groups as $group) {
225
        }
226
227
        $someGroups = $user->groups->slice(0, 2);
228
229
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
230
231
        $this->assertEquals(2, count($someGroups));
232
        $this->assertTrue($user->groups->contains(array_shift($someGroups)));
233
        $this->assertTrue($user->groups->contains(array_shift($someGroups)));
234
    }
235
236
    /**
237
     * @group DDC-546
238
     */
239
    public function testSliceInverseCollection()
240
    {
241
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
242
        $this->assertFalse($group->users->isInitialized(), 'Pre-Condition');
243
        $queryCount = $this->getCurrentQueryCount();
244
245
        $someUsers  = $group->users->slice(0, 2);
246
        $otherUsers = $group->users->slice(2, 2);
247
248
        $this->assertContainsOnly(CmsUser::class, $someUsers);
249
        $this->assertContainsOnly(CmsUser::class, $otherUsers);
250
        $this->assertEquals(2, count($someUsers));
251
        $this->assertEquals(2, count($otherUsers));
252
253
        // +2 queries executed by slice
254
        $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), 'Slicing two parts should only execute two additional queries.');
255
    }
256
257
    /**
258
     * @group DDC-546
259
     */
260
    public function testSliceOneToMany()
261
    {
262
        $user = $this->_em->find(CmsUser::class, $this->userId);
263
        $this->assertFalse($user->articles->isInitialized(), 'Pre-Condition: Collection is not initialized.');
264
265
        $queryCount = $this->getCurrentQueryCount();
266
267
        $someArticle  = $user->articles->slice(0, 1);
0 ignored issues
show
Unused Code introduced by
The assignment to $someArticle is dead and can be removed.
Loading history...
268
        $otherArticle = $user->articles->slice(1, 1);
0 ignored issues
show
Unused Code introduced by
The assignment to $otherArticle is dead and can be removed.
Loading history...
269
270
        $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount());
271
    }
272
273
    /**
274
     * @group DDC-546
275
     */
276
    public function testContainsOneToMany()
277
    {
278
        $user = $this->_em->find(CmsUser::class, $this->userId);
279
        $this->assertFalse($user->articles->isInitialized(), 'Pre-Condition: Collection is not initialized.');
280
281
        // Test One to Many existence retrieved from DB
282
        $article    = $this->_em->find(CmsArticle::class, $this->articleId);
283
        $queryCount = $this->getCurrentQueryCount();
284
285
        $this->assertTrue($user->articles->contains($article));
286
        $this->assertFalse($user->articles->isInitialized(), 'Post-Condition: Collection is not initialized.');
287
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
288
289
        // Test One to Many existence with state new
290
        $article        = new CmsArticle();
291
        $article->topic = 'Testnew';
292
        $article->text  = 'blub';
293
294
        $queryCount = $this->getCurrentQueryCount();
295
        $this->assertFalse($user->articles->contains($article));
296
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), 'Checking for contains of new entity should cause no query to be executed.');
297
298
        // Test One to Many existence with state clear
299
        $this->_em->persist($article);
300
        $this->_em->flush();
301
302
        $queryCount = $this->getCurrentQueryCount();
303
        $this->assertFalse($user->articles->contains($article));
304
        $this->assertEquals($queryCount+1, $this->getCurrentQueryCount(), 'Checking for contains of persisted entity should cause one query to be executed.');
305
        $this->assertFalse($user->articles->isInitialized(), 'Post-Condition: Collection is not initialized.');
306
307
        // Test One to Many existence with state managed
308
        $article        = new CmsArticle();
309
        $article->topic = 'How to not fail anymore on tests';
310
        $article->text  = 'That is simple! Just write more tests!';
311
312
        $this->_em->persist($article);
313
314
        $queryCount = $this->getCurrentQueryCount();
315
316
        $this->assertFalse($user->articles->contains($article));
317
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), 'Checking for contains of managed entity (but not persisted) should cause no query to be executed.');
318
        $this->assertFalse($user->articles->isInitialized(), 'Post-Condition: Collection is not initialized.');
319
    }
320
321
    /**
322
     * @group DDC-2504
323
     */
324
    public function testLazyOneToManyJoinedInheritanceIsLazilyInitialized()
325
    {
326
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
327
328
        $this->assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
329
    }
330
331
    /**
332
     * @group DDC-2504
333
     */
334
    public function testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWhenMatchingItemIsFound()
335
    {
336
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
337
338
        // Test One to Many existence retrieved from DB
339
        $childClass = $this->_em->find(DDC2504ChildClass::class, $this->ddc2504ChildClassId);
340
        $queryCount = $this->getCurrentQueryCount();
341
342
        $this->assertTrue($otherClass->childClasses->contains($childClass));
0 ignored issues
show
Bug introduced by
The method contains() does not exist on Doctrine\Tests\Models\DDC2504\DDC2504ChildClass. ( Ignorable by Annotation )

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

342
        $this->assertTrue($otherClass->childClasses->/** @scrutinizer ignore-call */ contains($childClass));

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...
343
        $this->assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
344
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), 'Search operation was performed via SQL');
345
    }
346
347
    /**
348
     * @group DDC-2504
349
     */
350
    public function testContainsOnOneToManyJoinedInheritanceWillNotCauseQueriesWhenNonPersistentItemIsMatched()
351
    {
352
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
353
        $queryCount = $this->getCurrentQueryCount();
354
355
        $this->assertFalse($otherClass->childClasses->contains(new DDC2504ChildClass()));
356
        $this->assertEquals(
357
            $queryCount,
358
            $this->getCurrentQueryCount(),
359
            'Checking for contains of new entity should cause no query to be executed.'
360
        );
361
    }
362
363
    /**
364
     * @group DDC-2504
365
     */
366
    public function testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWithClearStateMatchingItem()
367
    {
368
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
369
        $childClass = new DDC2504ChildClass();
370
371
        // Test One to Many existence with state clear
372
        $this->_em->persist($childClass);
373
        $this->_em->flush();
374
375
        $queryCount = $this->getCurrentQueryCount();
376
        $this->assertFalse($otherClass->childClasses->contains($childClass));
377
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), 'Checking for contains of persisted entity should cause one query to be executed.');
378
        $this->assertFalse($otherClass->childClasses->isInitialized(), 'Post-Condition: Collection is not initialized.');
379
    }
380
381
    /**
382
     * @group DDC-2504
383
     */
384
    public function testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWithNewStateNotMatchingItem()
385
    {
386
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
387
        $childClass = new DDC2504ChildClass();
388
389
        $this->_em->persist($childClass);
390
391
        $queryCount = $this->getCurrentQueryCount();
392
393
        $this->assertFalse($otherClass->childClasses->contains($childClass));
394
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), 'Checking for contains of managed entity (but not persisted) should cause no query to be executed.');
395
        $this->assertFalse($otherClass->childClasses->isInitialized(), 'Post-Condition: Collection is not initialized.');
396
    }
397
398
    /**
399
     * @group DDC-2504
400
     */
401
    public function testCountingOnOneToManyJoinedInheritanceWillNotInitializeCollection()
402
    {
403
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
404
405
        $this->assertEquals(2, count($otherClass->childClasses));
0 ignored issues
show
Bug introduced by
$otherClass->childClasses of type Doctrine\Tests\Models\DDC2504\DDC2504ChildClass is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

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

405
        $this->assertEquals(2, count(/** @scrutinizer ignore-type */ $otherClass->childClasses));
Loading history...
406
407
        $this->assertFalse($otherClass->childClasses->isInitialized());
408
    }
409
410
    /**
411
     * @group DDC-546
412
     */
413
    public function testContainsManyToMany()
414
    {
415
        $user = $this->_em->find(CmsUser::class, $this->userId);
416
        $this->assertFalse($user->groups->isInitialized(), 'Pre-Condition: Collection is not initialized.');
417
418
        // Test Many to Many existence retrieved from DB
419
        $group      = $this->_em->find(CmsGroup::class, $this->groupId);
420
        $queryCount = $this->getCurrentQueryCount();
421
422
        $this->assertTrue($user->groups->contains($group));
423
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), 'Checking for contains of managed entity should cause one 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 new
427
        $group       = new CmsGroup();
428
        $group->name = 'A New group!';
429
430
        $queryCount = $this->getCurrentQueryCount();
431
432
        $this->assertFalse($user->groups->contains($group));
433
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), 'Checking for contains of new entity should cause no 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 clear
437
        $this->_em->persist($group);
438
        $this->_em->flush();
439
440
        $queryCount = $this->getCurrentQueryCount();
441
442
        $this->assertFalse($user->groups->contains($group));
443
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), 'Checking for contains of persisted entity should cause one query to be executed.');
444
        $this->assertFalse($user->groups->isInitialized(), 'Post-Condition: Collection is not initialized.');
445
446
        // Test Many to Many existence with state managed
447
        $group       = new CmsGroup();
448
        $group->name = 'My managed group';
449
450
        $this->_em->persist($group);
451
452
        $queryCount = $this->getCurrentQueryCount();
453
454
        $this->assertFalse($user->groups->contains($group));
455
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), 'Checking for contains of managed entity (but not persisted) should cause no query to be executed.');
456
        $this->assertFalse($user->groups->isInitialized(), 'Post-Condition: Collection is not initialized.');
457
    }
458
459
    /**
460
     * @group DDC-546
461
     */
462
    public function testContainsManyToManyInverse()
463
    {
464
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
465
        $this->assertFalse($group->users->isInitialized(), 'Pre-Condition: Collection is not initialized.');
466
467
        $user = $this->_em->find(CmsUser::class, $this->userId);
468
469
        $queryCount = $this->getCurrentQueryCount();
470
        $this->assertTrue($group->users->contains($user));
471
        $this->assertEquals($queryCount+1, $this->getCurrentQueryCount(), 'Checking for contains of managed entity should cause one query to be executed.');
472
        $this->assertFalse($user->groups->isInitialized(), 'Post-Condition: Collection is not initialized.');
473
474
        $newUser       = new CmsUser();
475
        $newUser->name = 'A New group!';
476
477
        $queryCount = $this->getCurrentQueryCount();
478
        $this->assertFalse($group->users->contains($newUser));
479
        $this->assertEquals($queryCount, $this->getCurrentQueryCount(), 'Checking for contains of new entity should cause no query to be executed.');
480
        $this->assertFalse($user->groups->isInitialized(), 'Post-Condition: Collection is not initialized.');
481
    }
482
483
    /**
484
     * @group DDC-1399
485
     */
486
    public function testCountAfterAddThenFlush()
487
    {
488
        $user = $this->_em->find(CmsUser::class, $this->userId);
489
490
        $newGroup       = new CmsGroup();
491
        $newGroup->name = 'Test4';
492
493
        $user->addGroup($newGroup);
494
        $this->_em->persist($newGroup);
495
496
        $this->assertFalse($user->groups->isInitialized());
497
        $this->assertEquals(4, count($user->groups));
498
        $this->assertFalse($user->groups->isInitialized());
499
500
        $this->_em->flush();
501
502
        $this->assertEquals(4, count($user->groups));
503
    }
504
505
    /**
506
     * @group DDC-1462
507
     * @group non-cacheable
508
     */
509
    public function testSliceOnDirtyCollection()
510
    {
511
        $user = $this->_em->find(CmsUser::class, $this->userId);
512
        /** @var CmsUser $user */
513
514
        $newGroup       = new CmsGroup();
515
        $newGroup->name = 'Test4';
516
517
        $user->addGroup($newGroup);
518
        $this->_em->persist($newGroup);
519
520
        $qc     = $this->getCurrentQueryCount();
521
        $groups = $user->groups->slice(0, 10);
522
523
        $this->assertEquals(4, count($groups));
524
        $this->assertEquals($qc + 1, $this->getCurrentQueryCount());
525
    }
526
527
    /**
528
     * @group DDC-1398
529
     * @group non-cacheable
530
     */
531
    public function testGetIndexByIdentifier()
532
    {
533
        $user = $this->_em->find(CmsUser::class, $this->userId);
534
        /** @var CmsUser $user */
535
536
        $queryCount  = $this->getCurrentQueryCount();
537
        $phonenumber = $user->phonenumbers->get($this->phonenumber);
538
539
        $this->assertFalse($user->phonenumbers->isInitialized());
540
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
541
        $this->assertSame($phonenumber, $this->_em->find(CmsPhonenumber::class, $this->phonenumber));
542
543
        $article = $user->phonenumbers->get($this->phonenumber);
0 ignored issues
show
Unused Code introduced by
The assignment to $article is dead and can be removed.
Loading history...
544
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), 'Getting the same entity should not cause an extra query to be executed');
545
    }
546
547
    /**
548
     * @group DDC-1398
549
     */
550
    public function testGetIndexByOneToMany()
551
    {
552
        $user = $this->_em->find(CmsUser::class, $this->userId);
553
        /** @var CmsUser $user */
554
555
        $queryCount = $this->getCurrentQueryCount();
556
557
        $article = $user->articles->get($this->topic);
558
559
        $this->assertFalse($user->articles->isInitialized());
560
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
561
        $this->assertSame($article, $this->_em->find(CmsArticle::class, $this->articleId));
562
    }
563
564
    /**
565
     * @group DDC-1398
566
     */
567
    public function testGetIndexByManyToManyInverseSide()
568
    {
569
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
570
        /** @var CmsGroup $group */
571
572
        $queryCount = $this->getCurrentQueryCount();
573
574
        $user = $group->users->get($this->username);
575
576
        $this->assertFalse($group->users->isInitialized());
577
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
578
        $this->assertSame($user, $this->_em->find(CmsUser::class, $this->userId));
579
    }
580
581
    /**
582
     * @group DDC-1398
583
     */
584
    public function testGetIndexByManyToManyOwningSide()
585
    {
586
        $user = $this->_em->find(CmsUser::class, $this->userId);
587
        /** @var CmsUser $user */
588
589
        $queryCount = $this->getCurrentQueryCount();
590
591
        $group = $user->groups->get($this->groupname);
592
593
        $this->assertFalse($user->groups->isInitialized());
594
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
595
        $this->assertSame($group, $this->_em->find(CmsGroup::class, $this->groupId));
596
    }
597
598
    /**
599
     * @group DDC-1398
600
     */
601
    public function testGetNonExistentIndexBy()
602
    {
603
        $user = $this->_em->find(CmsUser::class, $this->userId);
604
        $this->assertNull($user->articles->get(-1));
605
        $this->assertNull($user->groups->get(-1));
606
    }
607
608
    public function testContainsKeyIndexByOneToMany()
609
    {
610
        $user = $this->_em->find(CmsUser::class, $this->userId);
611
        /** @var CmsUser $user */
612
613
        $queryCount = $this->getCurrentQueryCount();
614
615
        $contains = $user->articles->containsKey($this->topic);
616
617
        $this->assertTrue($contains);
618
        $this->assertFalse($user->articles->isInitialized());
619
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
620
    }
621
622
    public function testContainsKeyIndexByOneToManyJoinedInheritance()
623
    {
624
        $class                                                 = $this->_em->getClassMetadata(DDC2504OtherClass::class);
625
        $class->associationMappings['childClasses']['indexBy'] = 'id';
626
627
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
628
629
        $queryCount = $this->getCurrentQueryCount();
630
631
        $contains = $otherClass->childClasses->containsKey($this->ddc2504ChildClassId);
0 ignored issues
show
Bug introduced by
The method containsKey() does not exist on Doctrine\Tests\Models\DDC2504\DDC2504ChildClass. ( Ignorable by Annotation )

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

631
        /** @scrutinizer ignore-call */ 
632
        $contains = $otherClass->childClasses->containsKey($this->ddc2504ChildClassId);

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...
632
633
        $this->assertTrue($contains);
634
        $this->assertFalse($otherClass->childClasses->isInitialized());
635
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
636
    }
637
638
    public function testContainsKeyIndexByManyToMany()
639
    {
640
        $user = $this->_em->find(CmsUser::class, $this->userId2);
641
642
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
643
644
        $queryCount = $this->getCurrentQueryCount();
645
646
        $contains = $user->groups->containsKey($group->name);
647
648
        $this->assertTrue($contains, 'The item is not into collection');
649
        $this->assertFalse($user->groups->isInitialized(), 'The collection must not be initialized');
650
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
651
    }
652
    public function testContainsKeyIndexByManyToManyNonOwning()
653
    {
654
        $user  = $this->_em->find(CmsUser::class, $this->userId2);
655
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
656
657
        $queryCount = $this->getCurrentQueryCount();
658
659
        $contains = $group->users->containsKey($user->username);
660
661
        $this->assertTrue($contains, 'The item is not into collection');
662
        $this->assertFalse($group->users->isInitialized(), 'The collection must not be initialized');
663
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
664
    }
665
666
    public function testContainsKeyIndexByWithPkManyToMany()
667
    {
668
        $class                                           = $this->_em->getClassMetadata(CmsUser::class);
669
        $class->associationMappings['groups']['indexBy'] = 'id';
670
671
        $user = $this->_em->find(CmsUser::class, $this->userId2);
672
673
        $queryCount = $this->getCurrentQueryCount();
674
675
        $contains = $user->groups->containsKey($this->groupId);
676
677
        $this->assertTrue($contains, 'The item is not into collection');
678
        $this->assertFalse($user->groups->isInitialized(), 'The collection must not be initialized');
679
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
680
    }
681
    public function testContainsKeyIndexByWithPkManyToManyNonOwning()
682
    {
683
        $class                                          = $this->_em->getClassMetadata(CmsGroup::class);
684
        $class->associationMappings['users']['indexBy'] = 'id';
685
686
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
687
688
        $queryCount = $this->getCurrentQueryCount();
689
690
        $contains = $group->users->containsKey($this->userId2);
691
692
        $this->assertTrue($contains, 'The item is not into collection');
693
        $this->assertFalse($group->users->isInitialized(), 'The collection must not be initialized');
694
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
695
    }
696
697
    public function testContainsKeyNonExistentIndexByOneToMany()
698
    {
699
        $user = $this->_em->find(CmsUser::class, $this->userId2);
700
701
        $queryCount = $this->getCurrentQueryCount();
702
703
        $contains = $user->articles->containsKey('NonExistentTopic');
704
705
        $this->assertFalse($contains);
706
        $this->assertFalse($user->articles->isInitialized());
707
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
708
    }
709
710
    public function testContainsKeyNonExistentIndexByManyToMany()
711
    {
712
        $user = $this->_em->find(CmsUser::class, $this->userId2);
713
714
        $queryCount = $this->getCurrentQueryCount();
715
716
        $contains = $user->groups->containsKey('NonExistentTopic');
717
718
        $this->assertFalse($contains);
719
        $this->assertFalse($user->groups->isInitialized());
720
        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
721
    }
722
723
    private function loadFixture()
724
    {
725
        $user1           = new CmsUser();
726
        $user1->username = 'beberlei';
727
        $user1->name     = 'Benjamin';
728
        $user1->status   = 'active';
729
730
        $user2           = new CmsUser();
731
        $user2->username = 'jwage';
732
        $user2->name     = 'Jonathan';
733
        $user2->status   = 'active';
734
735
        $user3           = new CmsUser();
736
        $user3->username = 'romanb';
737
        $user3->name     = 'Roman';
738
        $user3->status   = 'active';
739
740
        $user4           = new CmsUser();
741
        $user4->username = 'gblanco';
742
        $user4->name     = 'Guilherme';
743
        $user4->status   = 'active';
744
745
        $this->_em->persist($user1);
746
        $this->_em->persist($user2);
747
        $this->_em->persist($user3);
748
        $this->_em->persist($user4);
749
750
        $group1       = new CmsGroup();
751
        $group1->name = 'Test1';
752
753
        $group2       = new CmsGroup();
754
        $group2->name = 'Test2';
755
756
        $group3       = new CmsGroup();
757
        $group3->name = 'Test3';
758
759
        $user1->addGroup($group1);
760
        $user1->addGroup($group2);
761
        $user1->addGroup($group3);
762
763
        $user2->addGroup($group1);
764
        $user3->addGroup($group1);
765
        $user4->addGroup($group1);
766
767
        $this->_em->persist($group1);
768
        $this->_em->persist($group2);
769
        $this->_em->persist($group3);
770
771
        $article1        = new CmsArticle();
772
        $article1->topic = 'Test1';
773
        $article1->text  = 'Test1';
774
        $article1->setAuthor($user1);
775
776
        $article2        = new CmsArticle();
777
        $article2->topic = 'Test2';
778
        $article2->text  = 'Test2';
779
        $article2->setAuthor($user1);
780
781
        $this->_em->persist($article1);
782
        $this->_em->persist($article2);
783
784
        $phonenumber1              = new CmsPhonenumber();
785
        $phonenumber1->phonenumber = '12345';
786
787
        $phonenumber2              = new CmsPhonenumber();
788
        $phonenumber2->phonenumber = '67890';
789
790
        $this->_em->persist($phonenumber1);
791
        $this->_em->persist($phonenumber2);
792
793
        $user1->addPhonenumber($phonenumber1);
794
795
        // DDC-2504
796
        $otherClass  = new DDC2504OtherClass();
797
        $childClass1 = new DDC2504ChildClass();
798
        $childClass2 = new DDC2504ChildClass();
799
800
        $childClass1->other = $otherClass;
801
        $childClass2->other = $otherClass;
802
803
        $otherClass->childClasses[] = $childClass1;
804
        $otherClass->childClasses[] = $childClass2;
805
806
        $this->_em->persist($childClass1);
807
        $this->_em->persist($childClass2);
808
        $this->_em->persist($otherClass);
809
810
        $this->_em->flush();
811
        $this->_em->clear();
812
813
        $this->articleId           = $article1->id;
814
        $this->userId              = $user1->getId();
815
        $this->userId2             = $user2->getId();
816
        $this->groupId             = $group1->id;
817
        $this->ddc2504OtherClassId = $otherClass->id;
818
        $this->ddc2504ChildClassId = $childClass1->id;
819
820
        $this->username    = $user1->username;
821
        $this->groupname   = $group1->name;
822
        $this->topic       = $article1->topic;
823
        $this->phonenumber = $phonenumber1->phonenumber;
824
    }
825
}
826