Issue138Test.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Translatable\Fixture\Issue138\Article;
  6. use Gedmo\Translatable\Query\TreeWalker\TranslationWalker;
  7. use Doctrine\ORM\Query;
  8. /**
  9. * These are tests for translatable behavior
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class Issue138Test extends BaseTestCaseORM
  16. {
  17. const ARTICLE = 'Translatable\\Fixture\\Issue138\\Article';
  18. const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
  19. const TREE_WALKER_TRANSLATION = 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker';
  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');
  27. $this->translatableListener->setDefaultLocale('en');
  28. $this->translatableListener->setTranslationFallback(true);
  29. $evm->addEventSubscriber($this->translatableListener);
  30. $this->getMockSqliteEntityManager($evm);
  31. }
  32. public function testIssue138()
  33. {
  34. $this->populate();
  35. $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
  36. $dql .= " WHERE a.title LIKE '%foo%'";
  37. $q = $this->em->createQuery($dql);
  38. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  39. // array hydration
  40. $this->translatableListener->setTranslatableLocale('en_us');
  41. //die($q->getSQL());
  42. $result = $q->getArrayResult();
  43. $this->assertEquals(1, count($result));
  44. $this->assertEquals('Food', $result[0]['title']);
  45. }
  46. protected function getUsedEntityFixtures()
  47. {
  48. return array(
  49. self::ARTICLE,
  50. self::TRANSLATION,
  51. );
  52. }
  53. private function populate()
  54. {
  55. $repo = $this->em->getRepository(self::ARTICLE);
  56. $food = new Article;
  57. $food->setTitle('Food');
  58. $food->setTitleTest('about food');
  59. $citron = new Article;
  60. $citron->setTitle('Citron');
  61. $citron->setTitleTest('something citron');
  62. $this->em->persist($food);
  63. $this->em->persist($citron);
  64. $this->em->flush();
  65. $this->translatableListener->setTranslatableLocale('lt_lt');
  66. $food->setTitle('Maistas');
  67. $food->setTitleTest('apie maista');
  68. $citron->setTitle('Citrina');
  69. $citron->setTitleTest('kazkas citrina');
  70. $this->em->persist($food);
  71. $this->em->persist($citron);
  72. $this->em->flush();
  73. $this->em->clear();
  74. }
  75. }