Skip to content

Commit

Permalink
Broken eager loading testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
wmouwen committed Feb 17, 2024
1 parent 3918dcf commit 3cf2d72
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH11149/GH11149Test.php
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11149;

use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;

#[RequiresPhp('8.1')]
class GH11149Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->setUpEntitySchema([
Locale::class,
Product::class,
ProductTranslation::class,
]);
}

public function testEagerLoadAssociationIndexedByEntity(): void
{
$this->_em->persist($product = new Product(11149));
$this->_em->persist($locale = new Locale('fr_FR'));
$this->_em->persist(new ProductTranslation($product, $locale));

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

$product = $this->_em->find(Product::class, 11149);
static::assertInstanceOf(Product::class, $product);
static::assertCount(1, $product->translations);

$translation = $product->translations->get('fr_FR');
static::assertInstanceOf(ProductTranslation::class, $translation);
}
}
21 changes: 21 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH11149/Locale.php
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11149;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table('gh11149_locale')]
class Locale
{
#[ORM\Id]
#[ORM\Column(type: 'string', enumType: LocaleCode::class)]
public LocaleCode $code;

public function __construct(string $code)
{
$this->code = LocaleCode::from($code);
}
}
10 changes: 10 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH11149/LocaleCode.php
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11149;

enum LocaleCode: string
{
case French = 'fr_FR';
}
32 changes: 32 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH11149/Product.php
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11149;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table('gh11149_product')]
class Product
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
public int $id;

#[ORM\OneToMany(
targetEntity: ProductTranslation::class,
mappedBy: 'product',
fetch: 'EAGER',
indexBy: 'locale_code',
)]
public Collection $translations;

public function __construct(int $id)
{
$this->id = $id;
$this->translations = new ArrayCollection();
}
}
28 changes: 28 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH11149/ProductTranslation.php
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11149;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table('gh11149_product_translation')]
class ProductTranslation
{
#[ORM\Id]
#[ORM\ManyToOne(targetEntity: Product::class, inversedBy: 'translations')]
#[ORM\JoinColumn(nullable: false)]
public Product $product;

#[ORM\Id]
#[ORM\ManyToOne(targetEntity: Locale::class)]
#[ORM\JoinColumn(name: 'locale_code', referencedColumnName: 'code', nullable: false)]
public Locale $locale;

public function __construct(Product $product, Locale $locale)
{
$this->product = $product;
$this->locale = $locale;
}
}

0 comments on commit 3cf2d72

Please sign in to comment.