EntityTranslationTableTest.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Doctrine\Common\Util\Debug,
  4. Translatable\Fixture\PersonTranslation,
  5. Translatable\Fixture\Person;
  6. /**
  7. * These are tests for translatable behavior
  8. *
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @package Gedmo.Translatable
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class EntityTranslationTableTest extends \PHPUnit_Framework_TestCase
  15. {
  16. const TEST_ENTITY_CLASS = 'Translatable\Fixture\Person';
  17. const TEST_ENTITY_TRANSLATION = 'Translatable\Fixture\PersonTranslation';
  18. private $translatableListener;
  19. private $em;
  20. public function setUp()
  21. {
  22. $config = new \Doctrine\ORM\Configuration();
  23. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  24. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  25. $config->setProxyDir(__DIR__ . '/Proxy');
  26. $config->setProxyNamespace('Gedmo\Translatable\Proxies');
  27. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  28. $conn = array(
  29. 'driver' => 'pdo_sqlite',
  30. 'memory' => true,
  31. );
  32. //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  33. $evm = new \Doctrine\Common\EventManager();
  34. $this->translatableListener = new TranslationListener();
  35. $this->translatableListener->setTranslatableLocale('en_us');
  36. $evm->addEventSubscriber($this->translatableListener);
  37. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  38. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  39. $schemaTool->dropSchema(array());
  40. $schemaTool->createSchema(array(
  41. $this->em->getClassMetadata(self::TEST_ENTITY_CLASS),
  42. $this->em->getClassMetadata(self::TEST_ENTITY_TRANSLATION)
  43. ));
  44. }
  45. public function testFixtureGeneratedTranslations()
  46. {
  47. $person = new Person;
  48. $person->setName('name in en');
  49. $this->em->persist($person);
  50. $this->em->flush();
  51. $this->em->clear();
  52. $repo = $this->em->getRepository(self::TEST_ENTITY_TRANSLATION);
  53. $this->assertTrue($repo instanceof Entity\Repository\TranslationRepository);
  54. $translations = $repo->findTranslations($person);
  55. $this->assertEquals(count($translations), 1);
  56. $this->assertArrayHasKey('en_us', $translations);
  57. $this->assertArrayHasKey('name', $translations['en_us']);
  58. $this->assertEquals('name in en', $translations['en_us']['name']);
  59. // test second translations
  60. $person = $this->em->find(self::TEST_ENTITY_CLASS, $person->getId());
  61. $this->translatableListener->setTranslatableLocale('de_de');
  62. $person->setName('name in de');
  63. $this->em->persist($person);
  64. $this->em->flush();
  65. $this->em->clear();
  66. $translations = $repo->findTranslations($person);
  67. $this->assertEquals(count($translations), 2);
  68. $this->assertArrayHasKey('de_de', $translations);
  69. $this->assertArrayHasKey('name', $translations['de_de']);
  70. $this->assertEquals('name in de', $translations['de_de']['name']);
  71. $this->translatableListener->setTranslatableLocale('en_us');
  72. }
  73. }