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
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Tests\Models\Legacy\LegacyArticle;
use Doctrine\Tests\Models\Legacy\LegacyCar;
use Doctrine\Tests\Models\Legacy\LegacyUser;
use Doctrine\Tests\Models\Legacy\LegacyUserReference;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;
#[Group('non-cacheable')]
#[Group('DDC-1301')]
class DDC1301Test extends OrmFunctionalTestCase
{
private int|null $userId = null;
protected function setUp(): void
{
$this->useModelSet('legacy');
parent::setUp();
$class = $this->_em->getClassMetadata(LegacyUser::class);
$class->associationMappings['articles']->fetch = ClassMetadata::FETCH_EXTRA_LAZY;
$class->associationMappings['references']->fetch = ClassMetadata::FETCH_EXTRA_LAZY;
$class->associationMappings['cars']->fetch = ClassMetadata::FETCH_EXTRA_LAZY;
$this->loadFixture();
}
public function tearDown(): void
{
parent::tearDown();
$class = $this->_em->getClassMetadata(LegacyUser::class);
$class->associationMappings['articles']->fetch = ClassMetadata::FETCH_LAZY;
$class->associationMappings['references']->fetch = ClassMetadata::FETCH_LAZY;
$class->associationMappings['cars']->fetch = ClassMetadata::FETCH_LAZY;
}
public function testCountNotInitializesLegacyCollection(): void
{
$user = $this->_em->find(LegacyUser::class, $this->userId);
$this->getQueryLog()->reset()->enable();
self::assertFalse($user->articles->isInitialized());
self::assertCount(2, $user->articles);
self::assertFalse($user->articles->isInitialized());
foreach ($user->articles as $article) {
}
$this->assertQueryCount(2, 'Expecting two queries to be fired for count, then iteration.');
}
public function testCountNotInitializesLegacyCollectionWithForeignIdentifier(): void
{
$user = $this->_em->find(LegacyUser::class, $this->userId);
$this->getQueryLog()->reset()->enable();
self::assertFalse($user->references->isInitialized());
self::assertCount(2, $user->references);
self::assertFalse($user->references->isInitialized());
foreach ($user->references as $reference) {
}
$this->assertQueryCount(2, 'Expecting two queries to be fired for count, then iteration.');
}
public function testCountNotInitializesLegacyManyToManyCollection(): void
{
$user = $this->_em->find(LegacyUser::class, $this->userId);
$this->getQueryLog()->reset()->enable();
self::assertFalse($user->cars->isInitialized());
self::assertCount(3, $user->cars);
self::assertFalse($user->cars->isInitialized());
foreach ($user->cars as $reference) {
}
$this->assertQueryCount(2, 'Expecting two queries to be fired for count, then iteration.');
}
public function loadFixture(): void
{
$user1 = new LegacyUser();
$user1->username = 'beberlei';
$user1->name = 'Benjamin';
$user2 = new LegacyUser();
$user2->username = 'jwage';
$user2->name = 'Jonathan';
$user3 = new LegacyUser();
$user3->username = 'romanb';
$user3->name = 'Roman';
$this->_em->persist($user1);
$this->_em->persist($user2);
$this->_em->persist($user3);
$article1 = new LegacyArticle();
$article1->topic = 'Test';
$article1->text = 'Test';
$article1->setAuthor($user1);
$article2 = new LegacyArticle();
$article2->topic = 'Test';
$article2->text = 'Test';
$article2->setAuthor($user1);
$this->_em->persist($article1);
$this->_em->persist($article2);
$car1 = new LegacyCar();
$car1->description = 'Test1';
$car2 = new LegacyCar();
$car2->description = 'Test2';
$car3 = new LegacyCar();
$car3->description = 'Test3';
$user1->addCar($car1);
$user1->addCar($car2);
$user1->addCar($car3);
$user2->addCar($car1);
$user3->addCar($car1);
$this->_em->persist($car1);
$this->_em->persist($car2);
$this->_em->persist($car3);
$this->_em->flush();
$detail1 = new LegacyUserReference($user1, $user2, 'foo');
$detail2 = new LegacyUserReference($user1, $user3, 'bar');
$this->_em->persist($detail1);
$this->_em->persist($detail2);
$this->_em->flush();
$this->_em->clear();
$this->userId = $user1->getId();
}
}
|