Skip to content

Commit bedea55

Browse files
committed
Add a test
1 parent d856e01 commit bedea55

6 files changed

+192
-1
lines changed

src/Doctrine/PolyglotListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ private function getTranslationMetadatas(object $entity, EntityManager $em): arr
117117
$this->translatableClassMetadatasByClass[$class] = [];
118118
$classMetadata = $em->getClassMetadata($class);
119119

120-
foreach (array_merge([$classMetadata->name], $classMetadata->parentClasses) as $className) {
120+
$candidates = array_merge([$classMetadata->name], $classMetadata->parentClasses);
121+
foreach ($candidates as $className) {
121122
if ($tm = $this->loadTranslationMetadataForClass($className, $em)) {
122123
$this->translatableClassMetadatasByClass[$class][] = $tm;
123124
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\EntityInheritance;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
7+
8+
#[ORM\Entity]
9+
#[Polyglot\Locale(primary: 'en_GB')]
10+
class EntityInheritance_MappedSuperclassTranslation
11+
{
12+
#[ORM\Id]
13+
#[ORM\GeneratedValue]
14+
#[ORM\Column(type: 'integer')]
15+
private ?int $id = null;
16+
17+
#[Polyglot\Locale]
18+
#[ORM\Column]
19+
private string $locale;
20+
21+
#[ORM\ManyToOne(targetEntity: EntityInheritance_MappedSuperclassWithTranslationsInterface::class, inversedBy: 'translations')]
22+
private EntityInheritance_MappedSuperclassWithTranslationsInterface $entity;
23+
24+
#[ORM\Column]
25+
private string $text;
26+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\EntityInheritance;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\Common\Collections\Collection;
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
9+
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;
10+
11+
/**
12+
* A mapped superclass that carries a translatable property, including translations.
13+
* Since translations are a one-to-many relationship, the ResolveTargetEntityListener
14+
* must be used to make the translation class ::$entity field reference back to this
15+
* class here, see https://www.doctrine-project.org/projects/doctrine-orm/en/3.6/reference/inheritance-mapping.html#:~:text=ResolveTargetEntityListener.
16+
*/
17+
#[ORM\MappedSuperclass]
18+
abstract class EntityInheritance_MappedSuperclassWithTranslations implements EntityInheritance_MappedSuperclassWithTranslationsInterface
19+
{
20+
#[ORM\Id]
21+
#[ORM\GeneratedValue]
22+
#[ORM\Column(type: 'integer')]
23+
private ?int $id = null;
24+
25+
#[Polyglot\TranslationCollection]
26+
#[ORM\OneToMany(targetEntity: EntityInheritance_MappedSuperclassTranslation::class, mappedBy: 'entity')]
27+
private Collection $translations;
28+
29+
#[ORM\Column(type: 'string', nullable: true)]
30+
#[Polyglot\Translatable]
31+
protected TranslatableInterface|string|null $text = null;
32+
33+
public function __construct()
34+
{
35+
$this->translations = new ArrayCollection();
36+
}
37+
38+
public function getId(): ?int
39+
{
40+
return $this->id;
41+
}
42+
43+
public function setText(TranslatableInterface $text): void
44+
{
45+
$this->text = $text;
46+
}
47+
48+
public function getText(): TranslatableInterface|string|null
49+
{
50+
return $this->text;
51+
}
52+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\EntityInheritance;
4+
5+
interface EntityInheritance_MappedSuperclassWithTranslationsInterface
6+
{
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\EntityInheritance;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\Common\Collections\Collection;
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
9+
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;
10+
11+
#[Polyglot\Locale(primary: 'en_GB')]
12+
#[ORM\Entity]
13+
class EntityInheritance_MappedSuperclassWithTranslations_Entity extends EntityInheritance_MappedSuperclassWithTranslations
14+
{
15+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Webfactory\Bundle\PolyglotBundle\Tests\Functional;
4+
5+
use Doctrine\ORM\Tools\ResolveTargetEntity;
6+
use Doctrine\ORM\Tools\ResolveTargetEntityListener;
7+
use Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\EntityInheritance\EntityInheritance_MappedSuperclassTranslation;
8+
use Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\EntityInheritance\EntityInheritance_MappedSuperclassWithTranslations;
9+
use Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\EntityInheritance\EntityInheritance_MappedSuperclassWithTranslations_Entity;
10+
use Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\EntityInheritance\EntityInheritance_MappedSuperclassWithTranslationsInterface;
11+
use Webfactory\Bundle\PolyglotBundle\Translatable;
12+
13+
class MappedSuperclassWithTranslationsTest extends DatabaseFunctionalTestCase
14+
{
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$resolveTargetEntity = new ResolveTargetEntityListener();
20+
$resolveTargetEntity->addResolveTargetEntity(
21+
EntityInheritance_MappedSuperclassWithTranslationsInterface::class,
22+
EntityInheritance_MappedSuperclassWithTranslations_Entity::class,
23+
[]
24+
);
25+
26+
$this->entityManager->getEventManager()->addEventSubscriber($resolveTargetEntity);
27+
28+
self::setupSchema([
29+
EntityInheritance_MappedSuperclassWithTranslations::class,
30+
EntityInheritance_MappedSuperclassWithTranslations_Entity::class,
31+
EntityInheritance_MappedSuperclassTranslation::class,
32+
]);
33+
}
34+
35+
public function testPersistAndReloadEntity(): void
36+
{
37+
$entity = new EntityInheritance_MappedSuperclassWithTranslations_Entity();
38+
$t = new Translatable('base text');
39+
$t->setTranslation('Basistext', 'de_DE');
40+
$entity->setText($t);
41+
42+
self::import([$entity]);
43+
44+
$loaded = $this->entityManager->find(EntityInheritance_MappedSuperclassWithTranslations_Entity::class, $entity->getId());
45+
46+
self::assertSame('base text', $loaded->getText()->translate('en_GB'));
47+
self::assertSame('Basistext', $loaded->getText()->translate('de_DE'));
48+
}
49+
50+
public function testAddTranslation(): void
51+
{
52+
$entityManager = $this->entityManager;
53+
54+
$entity = new EntityInheritance_MappedSuperclassWithTranslations_Entity();
55+
$entity->setText(new Translatable('base text'));
56+
self::import([$entity]);
57+
58+
$loaded = $entityManager->find(EntityInheritance_MappedSuperclassWithTranslations_Entity::class, $entity->getId());
59+
$loaded->getText()->setTranslation('Basistext', 'de_DE');
60+
$entityManager->flush();
61+
62+
$entityManager->clear();
63+
$reloaded = $entityManager->find(EntityInheritance_MappedSuperclassWithTranslations_Entity::class, $entity->getId());
64+
65+
self::assertSame('base text', $reloaded->getText()->translate('en_GB'));
66+
self::assertSame('Basistext', $reloaded->getText()->translate('de_DE'));
67+
}
68+
69+
public function testUpdateTranslations(): void
70+
{
71+
$entityManager = $this->entityManager;
72+
73+
$entity = new EntityInheritance_MappedSuperclassWithTranslations_Entity();
74+
$t = new Translatable('old text');
75+
$t->setTranslation('alter Text', 'de_DE');
76+
$entity->setText($t);
77+
self::import([$entity]);
78+
79+
$loaded = $entityManager->find(EntityInheritance_MappedSuperclassWithTranslations_Entity::class, $entity->getId());
80+
$loaded->getText()->setTranslation('new text');
81+
$loaded->getText()->setTranslation('neuer Text', 'de_DE');
82+
$entityManager->flush();
83+
84+
$entityManager->clear();
85+
$reloaded = $entityManager->find(EntityInheritance_MappedSuperclassWithTranslations_Entity::class, $entity->getId());
86+
87+
self::assertSame('new text', $reloaded->getText()->translate('en_GB'));
88+
self::assertSame('neuer Text', $reloaded->getText()->translate('de_DE'));
89+
}
90+
}

0 commit comments

Comments
 (0)