TranslatableIdentifierTest.php 3.8 KB

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