diff --git a/tests/Tests/ORM/Functional/Ticket/GH11149/EagerProduct.php b/tests/Tests/ORM/Functional/Ticket/GH11149/EagerProduct.php new file mode 100644 index 00000000000..dfbdfe2b503 --- /dev/null +++ b/tests/Tests/ORM/Functional/Ticket/GH11149/EagerProduct.php @@ -0,0 +1,32 @@ +id = $id; + $this->translations = new ArrayCollection(); + } +} diff --git a/tests/Tests/ORM/Functional/Ticket/GH11149/EagerProductTranslation.php b/tests/Tests/ORM/Functional/Ticket/GH11149/EagerProductTranslation.php new file mode 100644 index 00000000000..12a524ce702 --- /dev/null +++ b/tests/Tests/ORM/Functional/Ticket/GH11149/EagerProductTranslation.php @@ -0,0 +1,28 @@ +product = $product; + $this->locale = $locale; + } +} diff --git a/tests/Tests/ORM/Functional/Ticket/GH11149/GH11149Test.php b/tests/Tests/ORM/Functional/Ticket/GH11149/GH11149Test.php new file mode 100644 index 00000000000..5fe081ba9f8 --- /dev/null +++ b/tests/Tests/ORM/Functional/Ticket/GH11149/GH11149Test.php @@ -0,0 +1,108 @@ +setUpEntitySchema([ + Locale::class, + EagerProduct::class, + EagerProductTranslation::class, + RegularProduct::class, + RegularProductTranslation::class, + ]); + } + + public function testFetchDefaultModeWithIndexBy(): void + { + // Load entities into database + $this->_em->persist($product = new RegularProduct(11149)); + $this->_em->persist($locale = new Locale('de_DE')); + $this->_em->persist(new RegularProductTranslation($product, $locale)); + $this->_em->flush(); + $this->_em->clear(); + + // Fetch entity from database + $product = $this->_em->find(RegularProduct::class, 11149); + + // Assert associated entity is not loaded eagerly + static::assertInstanceOf(RegularProduct::class, $product); + static::assertInstanceOf(PersistentCollection::class, $product->translations); + static::assertFalse($product->translations->isInitialized()); + static::assertCount(1, $product->translations); + + // Assert associated entity is indexed by given property + $translation = $product->translations->get('de_DE'); + static::assertInstanceOf(RegularProductTranslation::class, $translation); + static::assertNotInstanceOf(Proxy::class, $translation); + } + + public function testFetchDefaultModeThroughEagerRepositoryCall(): void + { + // Load entities into database + $this->_em->persist($product = new RegularProduct(11185)); + $this->_em->persist($locale = new Locale('nl_NL')); + $this->_em->persist(new RegularProductTranslation($product, $locale)); + $this->_em->flush(); + $this->_em->clear(); + + // Fetch entity from database + $queryBuilder = $this->_em->createQueryBuilder(); + + $queryBuilder + ->select('product', 'translations') + ->from(RegularProduct::class, 'product') + ->leftJoin('product.translations', 'translations') + ->where($queryBuilder->expr()->eq('product.id', ':product_id')); + + $product = $queryBuilder + ->getQuery() + ->setParameter('product_id', 11185) + ->getSingleResult(); + + // Assert associated entity is loaded eagerly + static::assertInstanceOf(RegularProduct::class, $product); + static::assertInstanceOf(PersistentCollection::class, $product->translations); + static::assertTrue($product->translations->isInitialized()); + static::assertCount(1, $product->translations); + + // Assert associated entity is indexed by given property + $translation = $product->translations->get('nl_NL'); + static::assertInstanceOf(RegularProductTranslation::class, $translation); + static::assertNotInstanceOf(Proxy::class, $translation); + } + + public function testFetchEagerModeWithIndexBy(): void + { + // Load entities into database + $this->_em->persist($product = new EagerProduct(11149)); + $this->_em->persist($locale = new Locale('fr_FR')); + $this->_em->persist(new EagerProductTranslation($product, $locale)); + $this->_em->flush(); + $this->_em->clear(); + + // Fetch entity from database + $product = $this->_em->find(EagerProduct::class, 11149); + + // Assert associated entity is loaded eagerly + static::assertInstanceOf(EagerProduct::class, $product); + static::assertInstanceOf(PersistentCollection::class, $product->translations); + static::assertTrue($product->translations->isInitialized()); + static::assertCount(1, $product->translations); + + // Assert associated entity is indexed by given property + $translation = $product->translations->get('fr_FR'); + static::assertInstanceOf(EagerProductTranslation::class, $translation); + static::assertNotInstanceOf(Proxy::class, $translation); + } +} diff --git a/tests/Tests/ORM/Functional/Ticket/GH11149/Locale.php b/tests/Tests/ORM/Functional/Ticket/GH11149/Locale.php new file mode 100644 index 00000000000..90feaebad21 --- /dev/null +++ b/tests/Tests/ORM/Functional/Ticket/GH11149/Locale.php @@ -0,0 +1,21 @@ +code = LocaleCode::from($code); + } +} diff --git a/tests/Tests/ORM/Functional/Ticket/GH11149/LocaleCode.php b/tests/Tests/ORM/Functional/Ticket/GH11149/LocaleCode.php new file mode 100644 index 00000000000..f8ef5e1b15e --- /dev/null +++ b/tests/Tests/ORM/Functional/Ticket/GH11149/LocaleCode.php @@ -0,0 +1,12 @@ +id = $id; + $this->translations = new ArrayCollection(); + } +} diff --git a/tests/Tests/ORM/Functional/Ticket/GH11149/RegularProductTranslation.php b/tests/Tests/ORM/Functional/Ticket/GH11149/RegularProductTranslation.php new file mode 100644 index 00000000000..99427badba6 --- /dev/null +++ b/tests/Tests/ORM/Functional/Ticket/GH11149/RegularProductTranslation.php @@ -0,0 +1,28 @@ +product = $product; + $this->locale = $locale; + } +}