TranslatableIdentifierTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Doctrine\Common\Util\Debug,
  4. Translatable\Fixture\StringIdentifier;
  5. /**
  6. * These are tests for translatable behavior
  7. *
  8. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  9. * @package Gedmo.Translatable
  10. * @link http://www.gediminasm.org
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. class TranslatableIdentifierTest extends \PHPUnit_Framework_TestCase
  14. {
  15. const TEST_ENTITY_CLASS = 'Translatable\Fixture\StringIdentifier';
  16. const TRANSLATION_CLASS = 'Gedmo\Translatable\Entity\Translation';
  17. private $testObjectId;
  18. private $translationListener;
  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->translationListener = new TranslationListener();
  35. $this->translationListener->setTranslatableLocale('en_us');
  36. $evm->addEventSubscriber($this->translationListener);
  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::TRANSLATION_CLASS),
  43. ));
  44. }
  45. public function testStringIdentifier()
  46. {
  47. $object = new StringIdentifier();
  48. $object->setTitle('title in en');
  49. $object->setUid(md5(self::TEST_ENTITY_CLASS . time()));
  50. $this->em->persist($object);
  51. $this->em->flush();
  52. $this->em->clear();
  53. $this->testObjectId = $object->getUid();
  54. $repo = $this->em->getRepository(self::TRANSLATION_CLASS);
  55. $object = $this->em->find(self::TEST_ENTITY_CLASS, $this->testObjectId);
  56. $translations = $repo->findTranslations($object);
  57. $this->assertEquals(count($translations), 1);
  58. $this->assertArrayHasKey('en_us', $translations);
  59. $this->assertArrayHasKey('title', $translations['en_us']);
  60. $this->assertEquals('title in en', $translations['en_us']['title']);
  61. $object = $this->em->find(self::TEST_ENTITY_CLASS, $this->testObjectId);
  62. $object->setTitle('title in de');
  63. $object->setTranslatableLocale('de_de');
  64. $this->em->persist($object);
  65. $this->em->flush();
  66. $this->em->clear();
  67. $repo = $this->em->getRepository(self::TRANSLATION_CLASS);
  68. // test the entity load by translated title
  69. $object = $repo->findObjectByTranslatedField(
  70. 'title',
  71. 'title in en',
  72. self::TEST_ENTITY_CLASS
  73. );
  74. $this->assertEquals($this->testObjectId, $object->getUid());
  75. $object = $repo->findObjectByTranslatedField(
  76. 'title',
  77. 'title in de',
  78. self::TEST_ENTITY_CLASS
  79. );
  80. $this->assertEquals($this->testObjectId, $object->getUid());
  81. $translations = $repo->findTranslations($object);
  82. $this->assertEquals(count($translations), 2);
  83. $this->assertArrayHasKey('de_de', $translations);
  84. $this->assertArrayHasKey('title', $translations['de_de']);
  85. $this->assertEquals('title in de', $translations['de_de']['title']);
  86. // dql test object hydration
  87. $q = $this->em->createQuery('SELECT si FROM ' . self::TEST_ENTITY_CLASS . ' si WHERE si.uid = :id');
  88. $data = $q->execute(
  89. array('id' => $this->testObjectId),
  90. \Doctrine\ORM\Query::HYDRATE_OBJECT
  91. );
  92. $this->assertEquals(count($data), 1);
  93. $object = $data[0];
  94. $this->assertEquals('title in en', $object->getTitle());
  95. $this->translationListener->setTranslatableLocale('de_de');
  96. $q = $this->em->createQuery('SELECT si FROM ' . self::TEST_ENTITY_CLASS . ' si WHERE si.uid = :id');
  97. $data = $q->execute(
  98. array('id' => $this->testObjectId),
  99. \Doctrine\ORM\Query::HYDRATE_OBJECT
  100. );
  101. $this->assertEquals(count($data), 1);
  102. $object = $data[0];
  103. $this->assertEquals('title in de', $object->getTitle());
  104. }
  105. }