TranslatableIdentifierTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class TranslatableIdentifierTest extends BaseTestCaseORM
  15. {
  16. const FIXTURE = 'Translatable\\Fixture\\StringIdentifier';
  17. const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
  18. private $testObjectId;
  19. private $translatableListener;
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $evm = new EventManager;
  24. $this->translatableListener = new TranslatableListener();
  25. $this->translatableListener->setTranslatableLocale('en_us');
  26. $this->translatableListener->setDefaultLocale('en_us');
  27. $evm->addEventSubscriber($this->translatableListener);
  28. $conn = array(
  29. 'driver' => 'pdo_mysql',
  30. 'host' => '127.0.0.1',
  31. 'dbname' => 'test',
  32. 'user' => 'root',
  33. 'password' => 'nimda'
  34. );
  35. //$this->getMockCustomEntityManager($conn, $evm);
  36. $this->getMockSqliteEntityManager($evm);
  37. }
  38. /**
  39. * @test
  40. */
  41. function shouldHandleStringIdentifier()
  42. {
  43. $object = new StringIdentifier();
  44. $object->setTitle('title in en');
  45. $object->setUid(md5(self::FIXTURE . time()));
  46. $this->em->persist($object);
  47. $this->em->flush();
  48. $this->em->clear();
  49. $this->testObjectId = $object->getUid();
  50. $repo = $this->em->getRepository(self::TRANSLATION);
  51. $object = $this->em->find(self::FIXTURE, $this->testObjectId);
  52. $translations = $repo->findTranslations($object);
  53. $this->assertCount(0, $translations);
  54. $object = $this->em->find(self::FIXTURE, $this->testObjectId);
  55. $object->setTitle('title in de');
  56. $object->setTranslatableLocale('de_de');
  57. $this->em->persist($object);
  58. $this->em->flush();
  59. $this->em->clear();
  60. $repo = $this->em->getRepository(self::TRANSLATION);
  61. // test the entity load by translated title
  62. $object = $repo->findObjectByTranslatedField(
  63. 'title',
  64. 'title in de',
  65. self::FIXTURE
  66. );
  67. $this->assertEquals($this->testObjectId, $object->getUid());
  68. $translations = $repo->findTranslations($object);
  69. $this->assertCount(1, $translations);
  70. $this->assertArrayHasKey('de_de', $translations);
  71. $this->assertArrayHasKey('title', $translations['de_de']);
  72. $this->assertEquals('title in de', $translations['de_de']['title']);
  73. // dql test object hydration
  74. $q = $this->em
  75. ->createQuery('SELECT si FROM ' . self::FIXTURE . ' si WHERE si.uid = :id')
  76. ->setParameter('id', $this->testObjectId)
  77. ->useResultCache(false)
  78. ;
  79. $data = $q->getResult();
  80. $this->assertCount(1, $data);
  81. $object = $data[0];
  82. $this->assertEquals('title in en', $object->getTitle());
  83. $this->em->clear(); // based on 2.3.0 it caches in identity map
  84. $this->translatableListener->setTranslatableLocale('de_de');
  85. $data = $q->getResult();
  86. $this->assertCount(1, $data);
  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. }