|
| 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