-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Translatable] fix Type error for non-nullable getter upon a missing …
…translation
- Loading branch information
Showing
5 changed files
with
223 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Doctrine Behavioral Extensions package. | ||
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Gedmo\Tests\Translatable\Fixture\Issue2167; | ||
|
||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Gedmo\Mapping\Annotation as Gedmo; | ||
|
||
/** | ||
* @ORM\Entity | ||
*/ | ||
#[ORM\Entity] | ||
class Article | ||
{ | ||
/** | ||
* @var int | ||
* | ||
* @ORM\Id | ||
* @ORM\GeneratedValue | ||
* @ORM\Column(type="integer") | ||
*/ | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue] | ||
#[ORM\Column(type: Types::INTEGER)] | ||
private $id; | ||
|
||
/** | ||
* @var string | ||
* | ||
* @Gedmo\Translatable | ||
* @ORM\Column(name="title", type="string", length=128) | ||
*/ | ||
#[Gedmo\Translatable] | ||
#[ORM\Column(name: 'title', type: Types::STRING, length: 128)] | ||
private $title; | ||
|
||
/** | ||
* @var string | ||
* | ||
* @Gedmo\Locale() | ||
*/ | ||
#[Gedmo\Locale] | ||
private $locale; | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getTitle(): ?string | ||
{ | ||
return $this->title; | ||
} | ||
|
||
public function setTitle(string $title): void | ||
{ | ||
$this->title = $title; | ||
} | ||
|
||
public function getLocale(): string | ||
{ | ||
return $this->locale; | ||
} | ||
|
||
public function setLocale(string $locale): void | ||
{ | ||
$this->locale = $locale; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Doctrine Behavioral Extensions package. | ||
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Gedmo\Translatable\Issue; | ||
|
||
use Doctrine\Common\EventManager; | ||
use Gedmo\Tests\Tool\BaseTestCaseORM; | ||
use Gedmo\Tests\Translatable\Fixture\Issue2167\Article; | ||
use Gedmo\Translatable\Entity\Translation; | ||
use Gedmo\Translatable\TranslatableListener; | ||
|
||
class Issue2167Test extends BaseTestCaseORM | ||
{ | ||
private const TRANSLATION = Translation::class; | ||
private const ENTITY = Article::class; | ||
|
||
/** | ||
* @var TranslatableListener | ||
*/ | ||
private $translatableListener; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$evm = new EventManager(); | ||
|
||
$this->translatableListener = new TranslatableListener(); | ||
$this->translatableListener->setTranslatableLocale('en'); | ||
$this->translatableListener->setDefaultLocale('en'); | ||
$this->translatableListener->setTranslationFallback(false); | ||
$evm->addEventSubscriber($this->translatableListener); | ||
|
||
$this->getDefaultMockSqliteEntityManager($evm); | ||
} | ||
|
||
public function testShouldFindInheritedClassTranslations(): void | ||
{ | ||
$enTitle = 'My english title'; | ||
$deTitle = 'My german title'; | ||
|
||
// English | ||
$entity = new Article(); | ||
$entity->setTitle($enTitle); | ||
$entity->setLocale('en'); | ||
$this->em->persist($entity); | ||
$this->em->flush(); | ||
|
||
// German | ||
$entity->setLocale('de'); | ||
$entity->setTitle($deTitle); | ||
$this->em->flush(); | ||
|
||
// Find with default translation value as null value (default setting) | ||
$entityInEn = $this->findUsingQueryBuilder('en'); | ||
$entityInDe = $this->findUsingQueryBuilder('de'); | ||
$entityInFr = $this->findUsingQueryBuilder('fr'); | ||
|
||
static::assertSame($enTitle, $entityInEn->getTitle()); | ||
static::assertSame($deTitle, $entityInDe->getTitle()); | ||
static::assertNull($entityInFr->getTitle()); | ||
|
||
// Find with default translation value as empty string | ||
$this->translatableListener->setDefaultTranslationValue(''); | ||
|
||
$entityInEn = $this->findUsingQueryBuilder('en'); | ||
$entityInDe = $this->findUsingQueryBuilder('de'); | ||
$entityInFr = $this->findUsingQueryBuilder('fr'); | ||
|
||
static::assertSame($enTitle, $entityInEn->getTitle()); | ||
static::assertSame($deTitle, $entityInDe->getTitle()); | ||
static::assertSame('', $entityInFr->getTitle()); | ||
|
||
// Find with default translation value as not empty string | ||
$this->translatableListener->setDefaultTranslationValue('no_translated'); | ||
|
||
$entityInEn = $this->findUsingQueryBuilder('en'); | ||
$entityInDe = $this->findUsingQueryBuilder('de'); | ||
$entityInFr = $this->findUsingQueryBuilder('fr'); | ||
|
||
static::assertSame($enTitle, $entityInEn->getTitle()); | ||
static::assertSame($deTitle, $entityInDe->getTitle()); | ||
static::assertSame('no_translated', $entityInFr->getTitle()); | ||
} | ||
|
||
protected function getUsedEntityFixtures(): array | ||
{ | ||
return [ | ||
self::TRANSLATION, | ||
self::ENTITY, | ||
]; | ||
} | ||
|
||
private function findUsingQueryBuilder(string $locale): ?Article | ||
{ | ||
$this->em->clear(); | ||
$this->translatableListener->setTranslatableLocale($locale); | ||
|
||
$qb = $this->em->createQueryBuilder()->select('e')->from(self::ENTITY, 'e'); | ||
|
||
return $qb->getQuery()->getSingleResult(); | ||
} | ||
} |