EntityTranslationTableTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug,
  6. Translatable\Fixture\PersonTranslation,
  7. Translatable\Fixture\Person;
  8. /**
  9. * These are tests for translatable behavior
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @package Gedmo.Translatable
  13. * @link http://www.gediminasm.org
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. class EntityTranslationTableTest extends BaseTestCaseORM
  17. {
  18. const PERSON = 'Translatable\\Fixture\\Person';
  19. const TRANSLATION = 'Translatable\\Fixture\\PersonTranslation';
  20. private $translatableListener;
  21. protected function setUp()
  22. {
  23. parent::setUp();
  24. $evm = new EventManager;
  25. $this->translatableListener = new TranslatableListener();
  26. $this->translatableListener->setTranslatableLocale('en_us');
  27. $this->translatableListener->setDefaultLocale('en_us');
  28. $evm->addEventSubscriber($this->translatableListener);
  29. $this->getMockSqliteEntityManager($evm);
  30. }
  31. public function testFixtureGeneratedTranslations()
  32. {
  33. $person = new Person;
  34. $person->setName('name in en');
  35. $this->em->persist($person);
  36. $this->em->flush();
  37. $this->em->clear();
  38. $repo = $this->em->getRepository(self::TRANSLATION);
  39. $this->assertTrue($repo instanceof Entity\Repository\TranslationRepository);
  40. $translations = $repo->findTranslations($person);
  41. //As Translate locale and Default locale are the same, no records should be present in translations table
  42. $this->assertCount(0, $translations);
  43. // test second translations
  44. $person = $this->em->find(self::PERSON, $person->getId());
  45. $this->translatableListener->setTranslatableLocale('de_de');
  46. $person->setName('name in de');
  47. $this->em->persist($person);
  48. $this->em->flush();
  49. $this->em->clear();
  50. $translations = $repo->findTranslations($person);
  51. //Only one translation should be present
  52. $this->assertCount(1, $translations);
  53. $this->assertArrayHasKey('de_de', $translations);
  54. $this->assertArrayHasKey('name', $translations['de_de']);
  55. $this->assertEquals('name in de', $translations['de_de']['name']);
  56. $this->translatableListener->setTranslatableLocale('en_us');
  57. }
  58. protected function getUsedEntityFixtures()
  59. {
  60. return array(
  61. self::PERSON,
  62. self::TRANSLATION
  63. );
  64. }
  65. }