Issue109Test.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\ORM\Query;
  6. use Gedmo\Translatable\Query\TreeWalker\TranslationWalker;
  7. use Translatable\Fixture\Article;
  8. use Translatable\Fixture\Comment;
  9. /**
  10. * These are tests for translation query walker
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @link http://www.gediminasm.org
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. class Issue109Test extends BaseTestCaseORM
  17. {
  18. const ARTICLE = 'Translatable\\Fixture\\Article';
  19. const COMMENT = 'Translatable\\Fixture\\Comment';
  20. const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
  21. const TREE_WALKER_TRANSLATION = 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker';
  22. private $translatableListener;
  23. protected function setUp()
  24. {
  25. parent::setUp();
  26. $evm = new EventManager;
  27. $this->translatableListener = new TranslatableListener();
  28. $this->translatableListener->setTranslatableLocale('en');
  29. $this->translatableListener->setDefaultLocale('en');
  30. $evm->addEventSubscriber($this->translatableListener);
  31. $this->getMockSqliteEntityManager($evm);
  32. $this->populate();
  33. }
  34. public function testIssue109()
  35. {
  36. $this->em
  37. ->getConfiguration()
  38. ->expects($this->any())
  39. ->method('getCustomHydrationMode')
  40. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  41. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'))
  42. ;
  43. $query = $this->em->createQueryBuilder();
  44. $query->select('a')
  45. ->from(self::ARTICLE, 'a')
  46. ->add('where', $query->expr()->not($query->expr()->eq('a.title', ':title')))
  47. ->setParameter('title', 'NA')
  48. ;
  49. $this->translatableListener->setTranslatableLocale('es');
  50. $this->translatableListener->setDefaultLocale('en');
  51. $this->translatableListener->setTranslationFallback(true);
  52. $query = $query->getQuery();
  53. $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  54. $result = $query->getResult();
  55. $this->assertEquals(3, count($result));
  56. }
  57. protected function getUsedEntityFixtures()
  58. {
  59. return array(
  60. self::ARTICLE,
  61. self::TRANSLATION,
  62. self::COMMENT
  63. );
  64. }
  65. public function populate()
  66. {
  67. $text0 = new Article;
  68. $text0->setTitle('text0');
  69. $this->em->persist($text0);
  70. $text1 = new Article;
  71. $text1->setTitle('text1');
  72. $this->em->persist($text1);
  73. $na = new Article;
  74. $na->setTitle('NA');
  75. $this->em->persist($na);
  76. $out = new Article;
  77. $out->setTitle('Out');
  78. $this->em->persist($out);
  79. $this->em->flush();
  80. $this->translatableListener->setTranslatableLocale('es');
  81. $text1->setTitle('texto1');
  82. $text0->setTitle('texto0');
  83. $this->em->persist($text1);
  84. $this->em->persist($text0);
  85. $this->em->flush();
  86. }
  87. }