Skip to content

Commit

Permalink
Add unit test for ODM translation repository
Browse files Browse the repository at this point in the history
Adding a unit test to test custom ODM translation repositories, as there
already exist for ORM.
  • Loading branch information
dgrothaus-mc committed Jul 31, 2024
1 parent 4c88aa7 commit 15f340c
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 0 deletions.
126 changes: 126 additions & 0 deletions tests/Gedmo/Translatable/EntityTranslationCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <[email protected]> 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;

use Doctrine\Common\EventManager;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Gedmo\Tests\Tool\BaseTestCaseMongoODM;
use Gedmo\Tests\Translatable\Fixture\Document\TranslationCollection\Person;
use Gedmo\Tests\Translatable\Fixture\Document\TranslationCollection\PersonTranslation;
use Gedmo\Translatable\Document\Repository\TranslationRepository;
use Gedmo\Translatable\TranslatableListener;

/**
* These are tests for translatable behavior
*
* @author Dominik Grothaus <[email protected]>
*/
class EntityTranslationCollectionTest extends BaseTestCaseMongoODM
{
private const PERSON = Person::class;
private const TRANSLATION = PersonTranslation::class;

private TranslatableListener $translatableListener;

protected function setUp(): void
{
parent::setUp();

$evm = new EventManager();
$this->translatableListener = new TranslatableListener();
$this->translatableListener->setDefaultLocale('en_US');
$this->translatableListener->setTranslatableLocale('en_US');
$evm->addEventSubscriber($this->translatableListener);

$this->getDefaultDocumentManager($evm);
}

public function testFixtureGeneratedTranslations(): void
{
$person = new (self::PERSON)();
$person->setName('name in en');

$this->dm->persist($person);
$this->dm->flush();
$this->dm->clear();

$repo = $this->dm->getRepository(self::TRANSLATION);
static::assertInstanceOf(TranslationRepository::class, $repo);

$translations = $repo->findTranslations($person);
// As Translate locale and Default locale are the same, no records should be present in translations table
static::assertCount(0, $translations);

// test second translations
$person = $this->dm->find(self::PERSON, $person->getId());
$this->translatableListener->setTranslatableLocale('de_DE');
$person->setName('name in de');

$this->dm->persist($person);
$this->dm->flush();
$this->dm->clear();

$translations = $repo->findTranslations($person);
// Only one translation should be present
static::assertCount(1, $translations);
static::assertArrayHasKey('de_DE', $translations);

static::assertArrayHasKey('name', $translations['de_DE']);
static::assertSame('name in de', $translations['de_DE']['name']);

$this->translatableListener->setTranslatableLocale('en_US');
}

public function testShouldPersistDefaultLocaleValue(): void
{
$this->translatableListener->setPersistDefaultLocaleTranslation(true);

$person = new (self::PERSON)();
$person->setName('de_DE');

/** @var TranslationRepository $translationRepository */
$translationRepository = $this->dm->getRepository(self::TRANSLATION);
$translationRepository
->translate($person, 'name', 'de_DE', 'de_DE')
->translate($person, 'name', 'en_US', 'en_US');
$this->dm->persist($person);
$this->dm->flush();

$this->translatableListener->setTranslatableLocale('en_US');

/** @var DocumentRepository<Person> $personRepository */
$personRepository = $this->dm->getRepository(self::PERSON);
$persons = $personRepository
->createQueryBuilder()
->hydrate(false)
->find()
->getQuery()
->getIterator()
->toArray()
;
static::assertSame('en_US', $persons[0]['name']);

$trans = $translationRepository
->createQueryBuilder()
->hydrate(false)
->find()
->getQuery()
->getIterator()
->toArray()
;
static::assertCount(2, $trans);
foreach ($trans as $item) {
static::assertSame($item['locale'], $item['content']);
}
$this->translatableListener->setTranslatableLocale('en_US');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
* For the full copyright and licence information, please view the LICENCE
* file that was distributed with this source code.
*/

namespace Gedmo\Tests\Translatable\Fixture\Document\TranslationCollection;

use Doctrine\DBAL\Types\Types;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
* @ODM\Document(collection="persons")
*/
#[ODM\Document(collection: 'persons')]
#[Gedmo\TranslationEntity(class: PersonTranslation::class)]
class Person
{
/**
* @var ?string
* @ODM\Id
*/
#[ODM\Id]
private ?string $id = null;

/**
* @Gedmo\Translatable
*
* @ODM\Field(name="name", type="string")
*/
#[Gedmo\Translatable]
#[ODM\Field(name: 'name', type: Types::STRING)]
private ?string $name = null;

public function getId(): ?string
{
return $this->id;
}

public function setName(?string $name): void
{
$this->name = $name;
}

public function getName(): ?string
{
return $this->name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
* For the full copyright and licence information, please view the LICENCE
* file that was distributed with this source code.
*/

namespace Gedmo\Tests\Translatable\Fixture\Document\TranslationCollection;

use Doctrine\DBAL\Types\Types;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Gedmo\Translatable\Document\MappedSuperclass\AbstractTranslation;
use Gedmo\Translatable\Document\Repository\TranslationRepository;

/**
* Gedmo\Translatable\Document\Translation
*
* @ODM\Document(
* collection="ext_translations",
* repositoryClass="Gedmo\Translatable\Document\Repository\TranslationRepository"
* )
* @ODM\UniqueIndex(name="lookup_unique_idx", keys={
* "locale": "asc",
* "object_class": "asc",
* "foreign_key": "asc",
* "field": "asc"
* })
* @ODM\Index(name="translations_lookup_idx", keys={
* "locale": "asc",
* "object_class": "asc",
* "foreign_key": "asc"
* })
*/
#[ODM\Document(collection: 'ext_translations', repositoryClass: TranslationRepository::class)]
#[ODM\UniqueIndex(keys: [
'locale' => 'asc',
'object_class' => 'asc',
'foreign_key' => 'asc',
'field' => 'asc',
], name: 'lookup_unique_idx'
)]
#[ODM\Index(keys: [
'locale' => 'asc',
'object_class' => 'asc',
'foreign_key' => 'asc',
], name: 'translations_lookup_idx'
)]
class PersonTranslation extends AbstractTranslation
{
/**
* @ODM\Field(
* name="full_name",
* type= Types::STRING,
* nullable= true,
* notSaved= true,
* )
*/
#[ODM\Field(
name: 'full_name',
type: Types::STRING,
nullable: true,
notSaved: true,
)]
protected ?string $fullName = null;

public function getFullName(): ?string
{
return $this->fullName;
}
}

0 comments on commit 15f340c

Please sign in to comment.