File: ExtraLazyCollectionTest.php

package info (click to toggle)
doctrine 3.5.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 11,552 kB
  • sloc: php: 108,302; xml: 1,340; makefile: 35; sh: 14
file content (849 lines) | stat: -rw-r--r-- 31,631 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Tests\Models\CMS\CmsArticle;
use Doctrine\Tests\Models\CMS\CmsGroup;
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\Models\DDC2504\DDC2504ChildClass;
use Doctrine\Tests\Models\DDC2504\DDC2504OtherClass;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

use function array_shift;
use function assert;

/**
 * Description of ExtraLazyCollectionTest
 */
class ExtraLazyCollectionTest extends OrmFunctionalTestCase
{
    private int|null $userId = null;

    private int|null $userId2 = null;

    private int|null $groupId = null;

    private int|null $articleId = null;

    private int|null $ddc2504OtherClassId = null;

    private int|null $ddc2504ChildClassId = null;

    private string|null $username = null;

    private string|null $groupname = null;

    private string|null $topic = null;

    /** @var CmsPhonenumber */
    private $phonenumber;

    /** @var array<string, mixed> */
    private $previousCacheConfig = [];

    protected function setUp(): void
    {
        $this->useModelSet('tweet');
        $this->useModelSet('cms');
        $this->useModelSet('ddc2504');

        parent::setUp();

        $class                                               = $this->_em->getClassMetadata(CmsUser::class);
        $class->associationMappings['groups']->fetch         = ClassMetadata::FETCH_EXTRA_LAZY;
        $class->associationMappings['groups']->indexBy       = 'name';
        $class->associationMappings['articles']->fetch       = ClassMetadata::FETCH_EXTRA_LAZY;
        $class->associationMappings['articles']->indexBy     = 'topic';
        $class->associationMappings['phonenumbers']->fetch   = ClassMetadata::FETCH_EXTRA_LAZY;
        $class->associationMappings['phonenumbers']->indexBy = 'phonenumber';

        foreach (['phonenumbers', 'articles', 'users'] as $field) {
            if (isset($class->associationMappings[$field]->cache)) {
                $this->previousCacheConfig[$field] = $class->associationMappings[$field]->cache;
            }

            unset($class->associationMappings[$field]->cache);
        }

        $class                                        = $this->_em->getClassMetadata(CmsGroup::class);
        $class->associationMappings['users']->fetch   = ClassMetadata::FETCH_EXTRA_LAZY;
        $class->associationMappings['users']->indexBy = 'username';

        $this->loadFixture();
    }

    public function tearDown(): void
    {
        parent::tearDown();

        $class                                             = $this->_em->getClassMetadata(CmsUser::class);
        $class->associationMappings['groups']->fetch       = ClassMetadata::FETCH_LAZY;
        $class->associationMappings['articles']->fetch     = ClassMetadata::FETCH_LAZY;
        $class->associationMappings['phonenumbers']->fetch = ClassMetadata::FETCH_LAZY;

        foreach (['phonenumbers', 'articles', 'users'] as $field) {
            if (isset($this->previousCacheConfig[$field])) {
                $class->associationMappings[$field]->cache = $this->previousCacheConfig[$field];
                unset($this->previousCacheConfig[$field]);
            }
        }

        unset($class->associationMappings['groups']->indexBy);
        unset($class->associationMappings['articles']->indexBy);
        unset($class->associationMappings['phonenumbers']->indexBy);

        $class                                      = $this->_em->getClassMetadata(CmsGroup::class);
        $class->associationMappings['users']->fetch = ClassMetadata::FETCH_LAZY;

        unset($class->associationMappings['users']->indexBy);
    }

    #[Group('DDC-546')]
    #[Group('non-cacheable')]
    public function testCountNotInitializesCollection(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        $this->getQueryLog()->reset()->enable();

        self::assertFalse($user->groups->isInitialized());
        self::assertCount(3, $user->groups);
        self::assertFalse($user->groups->isInitialized());

        foreach ($user->groups as $group) {
        }

        $this->assertQueryCount(2, 'Expecting two queries to be fired for count, then iteration.');
    }

    #[Group('DDC-546')]
    public function testCountWhenNewEntityPresent(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);

        $newGroup       = new CmsGroup();
        $newGroup->name = 'Test4';

        $user->addGroup($newGroup);
        $this->_em->persist($newGroup);

        self::assertFalse($user->groups->isInitialized());
        self::assertCount(4, $user->groups);
        self::assertFalse($user->groups->isInitialized());
    }

    #[Group('DDC-546')]
    #[Group('non-cacheable')]
    public function testCountWhenInitialized(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        $this->getQueryLog()->reset()->enable();

        foreach ($user->groups as $group) {
        }

        self::assertTrue($user->groups->isInitialized());
        self::assertCount(3, $user->groups);
        $this->assertQueryCount(1, 'Should only execute one query to initialize collection, no extra query for count() more.');
    }

    #[Group('DDC-546')]
    public function testCountInverseCollection(): void
    {
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
        self::assertFalse($group->users->isInitialized(), 'Pre-Condition');

        self::assertCount(4, $group->users);
        self::assertFalse($group->users->isInitialized(), 'Extra Lazy collection should not be initialized by counting the collection.');
    }

    #[Group('DDC-546')]
    public function testCountOneToMany(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        self::assertFalse($user->groups->isInitialized(), 'Pre-Condition');

        self::assertCount(2, $user->articles);
    }

    #[Group('DDC-2504')]
    public function testCountOneToManyJoinedInheritance(): void
    {
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);

        self::assertFalse($otherClass->childClasses->isInitialized(), 'Pre-Condition');
        self::assertCount(2, $otherClass->childClasses);
    }

    #[Group('non-cacheable')]
    public function testFirstWhenInitialized(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        $this->getQueryLog()->reset()->enable();
        $user->groups->toArray();

        self::assertTrue($user->groups->isInitialized());
        self::assertInstanceOf(CmsGroup::class, $user->groups->first());
        $this->assertQueryCount(1, 'Should only execute one query to initialize collection, no extra query for first().');
    }

    public function testFirstOnEmptyCollectionWhenInitialized(): void
    {
        foreach ($this->_em->getRepository(CmsGroup::class)->findAll() as $group) {
            $this->_em->remove($group);
        }

        $this->_em->flush();

        $user = $this->_em->find(CmsUser::class, $this->userId);
        $this->getQueryLog()->reset()->enable();
        $user->groups->toArray();

        self::assertTrue($user->groups->isInitialized());
        self::assertFalse($user->groups->first());
        $this->assertQueryCount(1, 'Should only execute one query to initialize collection, no extra query for first().');
    }

    public function testFirstWhenNotInitialized(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        $this->getQueryLog()->reset()->enable();

        self::assertFalse($user->groups->isInitialized());
        self::assertInstanceOf(CmsGroup::class, $user->groups->first());
        self::assertFalse($user->groups->isInitialized());
        $this->assertQueryCount(1, 'Should only execute one query for first().');
    }

    public function testFirstOnEmptyCollectionWhenNotInitialized(): void
    {
        foreach ($this->_em->getRepository(CmsGroup::class)->findAll() as $group) {
            $this->_em->remove($group);
        }

        $this->_em->flush();

        $user = $this->_em->find(CmsUser::class, $this->userId);
        $this->getQueryLog()->reset()->enable();

        self::assertFalse($user->groups->isInitialized());
        self::assertFalse($user->groups->first());
        self::assertFalse($user->groups->isInitialized());
        $this->assertQueryCount(1, 'Should only execute one query for first().');
    }

    #[Group('DDC-546')]
    public function testFullSlice(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        self::assertFalse($user->groups->isInitialized(), 'Pre-Condition: Collection is not initialized.');

        $someGroups = $user->groups->slice(0);
        self::assertCount(3, $someGroups);
    }

    #[Group('DDC-546')]
    #[Group('non-cacheable')]
    public function testSlice(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        self::assertFalse($user->groups->isInitialized(), 'Pre-Condition: Collection is not initialized.');

        $this->getQueryLog()->reset()->enable();

        $someGroups = $user->groups->slice(0, 2);

        self::assertContainsOnlyInstancesOf(CmsGroup::class, $someGroups);
        self::assertCount(2, $someGroups);
        self::assertFalse($user->groups->isInitialized(), "Slice should not initialize the collection if it wasn't before!");

        $otherGroup = $user->groups->slice(2, 1);

        self::assertContainsOnlyInstancesOf(CmsGroup::class, $otherGroup);
        self::assertCount(1, $otherGroup);
        self::assertFalse($user->groups->isInitialized());

        foreach ($user->groups as $group) {
        }

        self::assertTrue($user->groups->isInitialized());
        self::assertCount(3, $user->groups);

        $this->assertQueryCount(3);
    }

    #[Group('DDC-546')]
    #[Group('non-cacheable')]
    public function testSliceInitializedCollection(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        $this->getQueryLog()->reset()->enable();

        foreach ($user->groups as $group) {
        }

        $someGroups = $user->groups->slice(0, 2);

        $this->assertQueryCount(1);

        self::assertCount(2, $someGroups);
        self::assertTrue($user->groups->contains(array_shift($someGroups)));
        self::assertTrue($user->groups->contains(array_shift($someGroups)));
    }

    #[Group('DDC-546')]
    public function testSliceInverseCollection(): void
    {
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
        self::assertFalse($group->users->isInitialized(), 'Pre-Condition');
        $this->getQueryLog()->reset()->enable();

        $someUsers  = $group->users->slice(0, 2);
        $otherUsers = $group->users->slice(2, 2);

        self::assertContainsOnlyInstancesOf(CmsUser::class, $someUsers);
        self::assertContainsOnlyInstancesOf(CmsUser::class, $otherUsers);
        self::assertCount(2, $someUsers);
        self::assertCount(2, $otherUsers);

        // +2 queries executed by slice
        $this->assertQueryCount(2, 'Slicing two parts should only execute two additional queries.');
    }

    #[Group('DDC-546')]
    public function testSliceOneToMany(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        self::assertFalse($user->articles->isInitialized(), 'Pre-Condition: Collection is not initialized.');

        $this->getQueryLog()->reset()->enable();

        $someArticle  = $user->articles->slice(0, 1);
        $otherArticle = $user->articles->slice(1, 1);

        $this->assertQueryCount(2);
    }

    #[Group('DDC-546')]
    public function testContainsOneToMany(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        self::assertFalse($user->articles->isInitialized(), 'Pre-Condition: Collection is not initialized.');

        // Test One to Many existence retrieved from DB
        $article = $this->_em->find(CmsArticle::class, $this->articleId);
        $this->getQueryLog()->reset()->enable();

        self::assertTrue($user->articles->contains($article));
        self::assertFalse($user->articles->isInitialized(), 'Post-Condition: Collection is not initialized.');
        $this->assertQueryCount(1);

        // Test One to Many existence with state new
        $article        = new CmsArticle();
        $article->topic = 'Testnew';
        $article->text  = 'blub';

        $this->getQueryLog()->reset()->enable();
        self::assertFalse($user->articles->contains($article));
        $this->assertQueryCount(0, 'Checking for contains of new entity should cause no query to be executed.');

        // Test One to Many existence with state clear
        $this->_em->persist($article);
        $this->_em->flush();

        $this->getQueryLog()->reset()->enable();
        self::assertFalse($user->articles->contains($article));
        $this->assertQueryCount(1, 'Checking for contains of persisted entity should cause one query to be executed.');
        self::assertFalse($user->articles->isInitialized(), 'Post-Condition: Collection is not initialized.');

        // Test One to Many existence with state managed
        $article        = new CmsArticle();
        $article->topic = 'How to not fail anymore on tests';
        $article->text  = 'That is simple! Just write more tests!';

        $this->_em->persist($article);

        $this->getQueryLog()->reset()->enable();

        self::assertFalse($user->articles->contains($article));
        $this->assertQueryCount(0, 'Checking for contains of managed entity (but not persisted) should cause no query to be executed.');
        self::assertFalse($user->articles->isInitialized(), 'Post-Condition: Collection is not initialized.');
    }

    #[Group('DDC-2504')]
    public function testLazyOneToManyJoinedInheritanceIsLazilyInitialized(): void
    {
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);

        self::assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
    }

    #[Group('DDC-2504')]
    public function testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWhenMatchingItemIsFound(): void
    {
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);

        // Test One to Many existence retrieved from DB
        $childClass = $this->_em->find(DDC2504ChildClass::class, $this->ddc2504ChildClassId);
        $this->getQueryLog()->reset()->enable();

        self::assertTrue($otherClass->childClasses->contains($childClass));
        self::assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.');
        $this->assertQueryCount(1, 'Search operation was performed via SQL');
    }

    #[Group('DDC-2504')]
    public function testContainsOnOneToManyJoinedInheritanceWillNotCauseQueriesWhenNonPersistentItemIsMatched(): void
    {
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
        $this->getQueryLog()->reset()->enable();

        self::assertFalse($otherClass->childClasses->contains(new DDC2504ChildClass()));
        $this->assertQueryCount(0, 'Checking for contains of new entity should cause no query to be executed.');
    }

    #[Group('DDC-2504')]
    public function testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWithClearStateMatchingItem(): void
    {
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
        $childClass = new DDC2504ChildClass();

        // Test One to Many existence with state clear
        $this->_em->persist($childClass);
        $this->_em->flush();

        $this->getQueryLog()->reset()->enable();
        self::assertFalse($otherClass->childClasses->contains($childClass));
        $this->assertQueryCount(1, 'Checking for contains of persisted entity should cause one query to be executed.');
        self::assertFalse($otherClass->childClasses->isInitialized(), 'Post-Condition: Collection is not initialized.');
    }

    #[Group('DDC-2504')]
    public function testContainsOnOneToManyJoinedInheritanceWillNotInitializeCollectionWithNewStateNotMatchingItem(): void
    {
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);
        $childClass = new DDC2504ChildClass();

        $this->_em->persist($childClass);

        $this->getQueryLog()->reset()->enable();

        self::assertFalse($otherClass->childClasses->contains($childClass));
        $this->assertQueryCount(0, 'Checking for contains of managed entity (but not persisted) should cause no query to be executed.');
        self::assertFalse($otherClass->childClasses->isInitialized(), 'Post-Condition: Collection is not initialized.');
    }

    #[Group('DDC-2504')]
    public function testCountingOnOneToManyJoinedInheritanceWillNotInitializeCollection(): void
    {
        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);

        self::assertCount(2, $otherClass->childClasses);

        self::assertFalse($otherClass->childClasses->isInitialized());
    }

    #[Group('DDC-546')]
    public function testContainsManyToMany(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        self::assertFalse($user->groups->isInitialized(), 'Pre-Condition: Collection is not initialized.');

        // Test Many to Many existence retrieved from DB
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
        $this->getQueryLog()->reset()->enable();

        self::assertTrue($user->groups->contains($group));
        $this->assertQueryCount(1, 'Checking for contains of managed entity should cause one query to be executed.');
        self::assertFalse($user->groups->isInitialized(), 'Post-Condition: Collection is not initialized.');

        // Test Many to Many existence with state new
        $group       = new CmsGroup();
        $group->name = 'A New group!';

        $this->getQueryLog()->reset()->enable();

        self::assertFalse($user->groups->contains($group));
        $this->assertQueryCount(0, 'Checking for contains of new entity should cause no query to be executed.');
        self::assertFalse($user->groups->isInitialized(), 'Post-Condition: Collection is not initialized.');

        // Test Many to Many existence with state clear
        $this->_em->persist($group);
        $this->_em->flush();

        $this->getQueryLog()->reset()->enable();

        self::assertFalse($user->groups->contains($group));
        $this->assertQueryCount(1, 'Checking for contains of persisted entity should cause one query to be executed.');
        self::assertFalse($user->groups->isInitialized(), 'Post-Condition: Collection is not initialized.');

        // Test Many to Many existence with state managed
        $group       = new CmsGroup();
        $group->name = 'My managed group';

        $this->_em->persist($group);

        $this->getQueryLog()->reset()->enable();

        self::assertFalse($user->groups->contains($group));
        $this->assertQueryCount(0, 'Checking for contains of managed entity (but not persisted) should cause no query to be executed.');
        self::assertFalse($user->groups->isInitialized(), 'Post-Condition: Collection is not initialized.');
    }

    #[Group('DDC-546')]
    public function testContainsManyToManyInverse(): void
    {
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
        self::assertFalse($group->users->isInitialized(), 'Pre-Condition: Collection is not initialized.');

        $user = $this->_em->find(CmsUser::class, $this->userId);

        $this->getQueryLog()->reset()->enable();
        self::assertTrue($group->users->contains($user));
        $this->assertQueryCount(1, 'Checking for contains of managed entity should cause one query to be executed.');
        self::assertFalse($user->groups->isInitialized(), 'Post-Condition: Collection is not initialized.');

        $newUser       = new CmsUser();
        $newUser->name = 'A New group!';

        $this->getQueryLog()->reset()->enable();
        self::assertFalse($group->users->contains($newUser));
        $this->assertQueryCount(0, 'Checking for contains of new entity should cause no query to be executed.');
        self::assertFalse($user->groups->isInitialized(), 'Post-Condition: Collection is not initialized.');
    }

    #[Group('DDC-1399')]
    public function testCountAfterAddThenFlush(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);

        $newGroup       = new CmsGroup();
        $newGroup->name = 'Test4';

        $user->addGroup($newGroup);
        $this->_em->persist($newGroup);

        self::assertFalse($user->groups->isInitialized());
        self::assertCount(4, $user->groups);
        self::assertFalse($user->groups->isInitialized());

        $this->_em->flush();

        self::assertCount(4, $user->groups);
    }

    #[Group('DDC-1462')]
    #[Group('non-cacheable')]
    public function testSliceOnDirtyCollection(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        assert($user instanceof CmsUser);

        $newGroup       = new CmsGroup();
        $newGroup->name = 'Test4';

        $user->addGroup($newGroup);
        $this->_em->persist($newGroup);

        $this->getQueryLog()->reset()->enable();
        $groups = $user->groups->slice(0, 10);

        self::assertCount(4, $groups);
        $this->assertQueryCount(1);
    }

    #[Group('DDC-1398')]
    #[Group('non-cacheable')]
    public function testGetIndexByIdentifier(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        assert($user instanceof CmsUser);

        $this->getQueryLog()->reset()->enable();
        $phonenumber = $user->phonenumbers->get($this->phonenumber);

        self::assertFalse($user->phonenumbers->isInitialized());
        $this->assertQueryCount(1);
        self::assertSame($phonenumber, $this->_em->find(CmsPhonenumber::class, $this->phonenumber));

        $article = $user->phonenumbers->get($this->phonenumber);
        $this->assertQueryCount(1, 'Getting the same entity should not cause an extra query to be executed');
    }

    #[Group('DDC-1398')]
    public function testGetIndexByOneToMany(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        assert($user instanceof CmsUser);

        $this->getQueryLog()->reset()->enable();

        $article = $user->articles->get($this->topic);

        self::assertFalse($user->articles->isInitialized());
        $this->assertQueryCount(1);
        self::assertSame($article, $this->_em->find(CmsArticle::class, $this->articleId));
    }

    #[Group('DDC-1398')]
    public function testGetIndexByManyToManyInverseSide(): void
    {
        $group = $this->_em->find(CmsGroup::class, $this->groupId);
        assert($group instanceof CmsGroup);

        $this->getQueryLog()->reset()->enable();

        $user = $group->users->get($this->username);

        self::assertFalse($group->users->isInitialized());
        $this->assertQueryCount(1);
        self::assertSame($user, $this->_em->find(CmsUser::class, $this->userId));
    }

    #[Group('DDC-1398')]
    public function testGetIndexByManyToManyOwningSide(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        assert($user instanceof CmsUser);

        $this->getQueryLog()->reset()->enable();

        $group = $user->groups->get($this->groupname);

        self::assertFalse($user->groups->isInitialized());
        $this->assertQueryCount(1);
        self::assertSame($group, $this->_em->find(CmsGroup::class, $this->groupId));
    }

    #[Group('DDC-1398')]
    public function testGetNonExistentIndexBy(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        self::assertNull($user->articles->get(-1));
        self::assertNull($user->groups->get(-1));
    }

    public function testContainsKeyIndexByOneToMany(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId);
        assert($user instanceof CmsUser);

        $this->getQueryLog()->reset()->enable();

        $contains = $user->articles->containsKey($this->topic);

        self::assertTrue($contains);
        self::assertFalse($user->articles->isInitialized());
        $this->assertQueryCount(1);
    }

    public function testContainsKeyIndexByOneToManyJoinedInheritance(): void
    {
        $class                                               = $this->_em->getClassMetadata(DDC2504OtherClass::class);
        $class->associationMappings['childClasses']->indexBy = 'id';

        $otherClass = $this->_em->find(DDC2504OtherClass::class, $this->ddc2504OtherClassId);

        $this->getQueryLog()->reset()->enable();

        $contains = $otherClass->childClasses->containsKey($this->ddc2504ChildClassId);

        self::assertTrue($contains);
        self::assertFalse($otherClass->childClasses->isInitialized());
        $this->assertQueryCount(1);
    }

    public function testContainsKeyIndexByManyToMany(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId2);

        $group = $this->_em->find(CmsGroup::class, $this->groupId);

        $this->getQueryLog()->reset()->enable();

        $contains = $user->groups->containsKey($group->name);

        self::assertTrue($contains, 'The item is not into collection');
        self::assertFalse($user->groups->isInitialized(), 'The collection must not be initialized');
        $this->assertQueryCount(1);
    }

    public function testContainsKeyIndexByManyToManyNonOwning(): void
    {
        $user  = $this->_em->find(CmsUser::class, $this->userId2);
        $group = $this->_em->find(CmsGroup::class, $this->groupId);

        $this->getQueryLog()->reset()->enable();

        $contains = $group->users->containsKey($user->username);

        self::assertTrue($contains, 'The item is not into collection');
        self::assertFalse($group->users->isInitialized(), 'The collection must not be initialized');
        $this->assertQueryCount(1);
    }

    public function testContainsKeyIndexByWithPkManyToMany(): void
    {
        $class                                         = $this->_em->getClassMetadata(CmsUser::class);
        $class->associationMappings['groups']->indexBy = 'id';

        $user = $this->_em->find(CmsUser::class, $this->userId2);

        $this->getQueryLog()->reset()->enable();

        $contains = $user->groups->containsKey($this->groupId);

        self::assertTrue($contains, 'The item is not into collection');
        self::assertFalse($user->groups->isInitialized(), 'The collection must not be initialized');
        $this->assertQueryCount(1);
    }

    public function testContainsKeyIndexByWithPkManyToManyNonOwning(): void
    {
        $class                                        = $this->_em->getClassMetadata(CmsGroup::class);
        $class->associationMappings['users']->indexBy = 'id';

        $group = $this->_em->find(CmsGroup::class, $this->groupId);

        $this->getQueryLog()->reset()->enable();

        $contains = $group->users->containsKey($this->userId2);

        self::assertTrue($contains, 'The item is not into collection');
        self::assertFalse($group->users->isInitialized(), 'The collection must not be initialized');
        $this->assertQueryCount(1);
    }

    public function testContainsKeyNonExistentIndexByOneToMany(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId2);

        $this->getQueryLog()->reset()->enable();

        $contains = $user->articles->containsKey('NonExistentTopic');

        self::assertFalse($contains);
        self::assertFalse($user->articles->isInitialized());
        $this->assertQueryCount(1);
    }

    public function testContainsKeyNonExistentIndexByManyToMany(): void
    {
        $user = $this->_em->find(CmsUser::class, $this->userId2);

        $this->getQueryLog()->reset()->enable();

        $contains = $user->groups->containsKey('NonExistentTopic');

        self::assertFalse($contains);
        self::assertFalse($user->groups->isInitialized());
        $this->assertQueryCount(1);
    }

    private function loadFixture(): void
    {
        $user1           = new CmsUser();
        $user1->username = 'beberlei';
        $user1->name     = 'Benjamin';
        $user1->status   = 'active';

        $user2           = new CmsUser();
        $user2->username = 'jwage';
        $user2->name     = 'Jonathan';
        $user2->status   = 'active';

        $user3           = new CmsUser();
        $user3->username = 'romanb';
        $user3->name     = 'Roman';
        $user3->status   = 'active';

        $user4           = new CmsUser();
        $user4->username = 'gblanco';
        $user4->name     = 'Guilherme';
        $user4->status   = 'active';

        $this->_em->persist($user1);
        $this->_em->persist($user2);
        $this->_em->persist($user3);
        $this->_em->persist($user4);

        $group1       = new CmsGroup();
        $group1->name = 'Test1';

        $group2       = new CmsGroup();
        $group2->name = 'Test2';

        $group3       = new CmsGroup();
        $group3->name = 'Test3';

        $user1->addGroup($group1);
        $user1->addGroup($group2);
        $user1->addGroup($group3);

        $user2->addGroup($group1);
        $user3->addGroup($group1);
        $user4->addGroup($group1);

        $this->_em->persist($group1);
        $this->_em->persist($group2);
        $this->_em->persist($group3);

        $article1        = new CmsArticle();
        $article1->topic = 'Test1';
        $article1->text  = 'Test1';
        $article1->setAuthor($user1);

        $article2        = new CmsArticle();
        $article2->topic = 'Test2';
        $article2->text  = 'Test2';
        $article2->setAuthor($user1);

        $this->_em->persist($article1);
        $this->_em->persist($article2);

        $phonenumber1              = new CmsPhonenumber();
        $phonenumber1->phonenumber = '12345';

        $phonenumber2              = new CmsPhonenumber();
        $phonenumber2->phonenumber = '67890';

        $this->_em->persist($phonenumber1);
        $this->_em->persist($phonenumber2);

        $user1->addPhonenumber($phonenumber1);

        // DDC-2504
        $otherClass  = new DDC2504OtherClass();
        $childClass1 = new DDC2504ChildClass();
        $childClass2 = new DDC2504ChildClass();

        $childClass1->other = $otherClass;
        $childClass2->other = $otherClass;

        $otherClass->childClasses[] = $childClass1;
        $otherClass->childClasses[] = $childClass2;

        $this->_em->persist($childClass1);
        $this->_em->persist($childClass2);
        $this->_em->persist($otherClass);

        $this->_em->flush();
        $this->_em->clear();

        $this->articleId           = $article1->id;
        $this->userId              = $user1->getId();
        $this->userId2             = $user2->getId();
        $this->groupId             = $group1->id;
        $this->ddc2504OtherClassId = $otherClass->id;
        $this->ddc2504ChildClassId = $childClass1->id;

        $this->username    = $user1->username;
        $this->groupname   = $group1->name;
        $this->topic       = $article1->topic;
        $this->phonenumber = $phonenumber1->phonenumber;
    }
}