TranslationQueryWalkerTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. * @package Gedmo.Translatable
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class TranslationQueryWalkerTest extends BaseTestCaseORM
  18. {
  19. const ARTICLE = 'Translatable\\Fixture\\Article';
  20. const COMMENT = 'Translatable\\Fixture\\Comment';
  21. const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
  22. const TREE_WALKER_TRANSLATION = 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker';
  23. private $translationListener;
  24. protected function setUp()
  25. {
  26. parent::setUp();
  27. $evm = new EventManager;
  28. $this->translationListener = new TranslationListener();
  29. $this->translationListener->setTranslatableLocale('en_us');
  30. $evm->addEventSubscriber($this->translationListener);
  31. $this->getMockSqliteEntityManager($evm);
  32. $this->populate();
  33. }
  34. public function testIssue135()
  35. {
  36. $this->populateIssue109();
  37. $this->em
  38. ->getConfiguration()
  39. ->expects($this->any())
  40. ->method('getCustomHydrationMode')
  41. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  42. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'))
  43. ;
  44. $query = $this->em->createQueryBuilder();
  45. $query->select('a')
  46. ->from(self::ARTICLE, 'a')
  47. ->add('where', $query->expr()->not($query->expr()->eq('a.title', ':title')))
  48. ->setParameter('title', 'NA')
  49. ;
  50. $this->translationListener->setTranslatableLocale('en');
  51. $this->translationListener->setDefaultLocale('en');
  52. $this->translationListener->setTranslationFallback(true);
  53. $query = $query->getQuery();
  54. $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  55. $count = 0;
  56. str_replace("locale = 'en'", '', $query->getSql(), $count);
  57. $this->assertEquals(2, $count);
  58. }
  59. public function testIssue109()
  60. {
  61. $this->populateIssue109();
  62. $this->em
  63. ->getConfiguration()
  64. ->expects($this->any())
  65. ->method('getCustomHydrationMode')
  66. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  67. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'))
  68. ;
  69. $query = $this->em->createQueryBuilder();
  70. $query->select('a')
  71. ->from(self::ARTICLE, 'a')
  72. ->add('where', $query->expr()->not($query->expr()->eq('a.title', ':title')))
  73. ->setParameter('title', 'NA')
  74. ;
  75. $this->translationListener->setTranslatableLocale('es');
  76. $this->translationListener->setDefaultLocale('en');
  77. $this->translationListener->setTranslationFallback(true);
  78. $query = $query->getQuery();
  79. $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  80. $result = $query->getResult();
  81. $this->assertEquals(3, count($result));
  82. }
  83. public function testSubselectByTranslatedField()
  84. {
  85. $this->populateMore();
  86. $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
  87. $subSelect = 'SELECT a2.title FROM ' . self::ARTICLE . ' a2';
  88. $subSelect .= " WHERE a2.title LIKE '%ab%'";
  89. $dql .= " WHERE a.title IN ({$subSelect})";
  90. $dql .= ' ORDER BY a.title';
  91. $q = $this->em->createQuery($dql);
  92. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  93. // array hydration
  94. $this->translationListener->setTranslatableLocale('en_us');
  95. $result = $q->getArrayResult();
  96. $this->assertEquals(2, count($result));
  97. $this->assertEquals('Alfabet', $result[0]['title']);
  98. $this->assertEquals('Cabbages', $result[1]['title']);
  99. }
  100. public function testSubselectStatements()
  101. {
  102. $this->populateMore();
  103. $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
  104. $subSelect = 'SELECT a2.id FROM ' . self::ARTICLE . ' a2';
  105. $subSelect .= " WHERE a2.title LIKE '%ab%'";
  106. $dql .= " WHERE a.id IN ({$subSelect})";
  107. $dql .= ' ORDER BY a.title';
  108. $q = $this->em->createQuery($dql);
  109. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  110. // array hydration
  111. $this->translationListener->setTranslatableLocale('en_us');
  112. $result = $q->getArrayResult();
  113. $this->assertEquals(2, count($result));
  114. $this->assertEquals('Alfabet', $result[0]['title']);
  115. $this->assertEquals('Cabbages', $result[1]['title']);
  116. }
  117. public function testJoinedWithStatements()
  118. {
  119. $this->populateMore();
  120. $dql = 'SELECT a, c FROM ' . self::ARTICLE . ' a';
  121. //@todo: its impossible to support translated values in WITH statement
  122. $dql .= ' LEFT JOIN a.comments c WITH c.subject LIKE :lookup';
  123. $dql .= ' WHERE a.title LIKE :filter';
  124. $dql .= ' ORDER BY a.title';
  125. $q = $this->em->createQuery($dql);
  126. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  127. // array hydration
  128. $this->translationListener->setTranslatableLocale('en_us');
  129. $q->setParameter('lookup', '%ger%');
  130. $q->setParameter('filter', 'Foo%');
  131. $result = $q->getArrayResult();
  132. $this->assertEquals(1, count($result));
  133. $this->assertEquals('Food', $result[0]['title']);
  134. $comments = $result[0]['comments'];
  135. $this->assertEquals(1, count($comments));
  136. $this->assertEquals('good', $comments[0]['subject']);
  137. }
  138. public function testSelectWithTranslationFallbackOnSimpleObjectHydration()
  139. {
  140. $this->em
  141. ->getConfiguration()
  142. ->expects($this->any())
  143. ->method('getCustomHydrationMode')
  144. ->with(TranslationWalker::HYDRATE_SIMPLE_OBJECT_TRANSLATION)
  145. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\SimpleObjectHydrator'));
  146. $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
  147. $q = $this->em->createQuery($dql);
  148. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  149. $this->translationListener->setTranslatableLocale('ru_ru');
  150. $this->translationListener->setTranslationFallback(false);
  151. // simple object hydration
  152. $this->startQueryLog();
  153. $result = $q->getResult(Query::HYDRATE_SIMPLEOBJECT);
  154. $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
  155. $this->assertEquals('', $result[0]->getTitle());
  156. $this->assertEquals('', $result[0]->getContent());
  157. $this->translationListener->setTranslationFallback(true);
  158. $this->queryAnalyzer->cleanUp();
  159. $result = $q->getResult(Query::HYDRATE_SIMPLEOBJECT);
  160. $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
  161. $this->assertEquals('Maistas', $result[0]->getTitle());
  162. $this->assertEquals('apie maista', $result[0]->getContent());
  163. }
  164. public function testSelectWithTranslationFallbackOnArrayHydration()
  165. {
  166. $this->em
  167. ->getConfiguration()
  168. ->expects($this->any())
  169. ->method('getCustomHydrationMode')
  170. ->with(TranslationWalker::HYDRATE_ARRAY_TRANSLATION)
  171. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ArrayHydrator'));
  172. $dql = 'SELECT a, c FROM ' . self::ARTICLE . ' a';
  173. $dql .= ' LEFT JOIN a.comments c';
  174. $q = $this->em->createQuery($dql);
  175. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  176. $this->translationListener->setTranslatableLocale('ru_ru');
  177. $this->translationListener->setTranslationFallback(false);
  178. // array hydration
  179. $this->startQueryLog();
  180. $result = $q->getArrayResult();
  181. $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
  182. $this->assertEquals('', $result[0]['title']);
  183. $this->assertEquals('', $result[0]['content']);
  184. $this->translationListener->setTranslationFallback(true);
  185. $this->queryAnalyzer->cleanUp();
  186. $result = $q->getArrayResult();
  187. $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
  188. $this->assertEquals('Maistas', $result[0]['title']);
  189. $this->assertEquals('apie maista', $result[0]['content']);
  190. }
  191. public function testSelectWithTranslationFallbackOnObjectHydration()
  192. {
  193. $this->em
  194. ->getConfiguration()
  195. ->expects($this->any())
  196. ->method('getCustomHydrationMode')
  197. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  198. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
  199. $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
  200. $q = $this->em->createQuery($dql);
  201. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  202. $this->translationListener->setTranslatableLocale('ru_ru');
  203. $this->translationListener->setTranslationFallback(false);
  204. // object hydration
  205. $this->startQueryLog();
  206. $result = $q->getResult();
  207. $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
  208. $this->assertEquals('', $result[0]->getTitle());
  209. $this->assertEquals('', $result[0]->getContent());
  210. $this->translationListener->setTranslationFallback(true);
  211. $this->queryAnalyzer->cleanUp();
  212. $result = $q->getResult();
  213. $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
  214. $this->assertEquals('Maistas', $result[0]->getTitle());
  215. $this->assertEquals('apie maista', $result[0]->getContent());
  216. }
  217. public function testSelectCountStatement()
  218. {
  219. $dql = 'SELECT COUNT(a) FROM ' . self::ARTICLE . ' a';
  220. $dql .= ' WHERE a.title LIKE :title';
  221. $q = $this->em->createQuery($dql);
  222. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  223. $this->translationListener->setTranslatableLocale('en_us');
  224. $q->setParameter('title', 'Foo%');
  225. $result = $q->getSingleScalarResult();
  226. $this->assertEquals(1, $result);
  227. $this->translationListener->setTranslatableLocale('lt_lt');
  228. $q->setParameter('title', 'Mai%');
  229. $result = $q->getSingleScalarResult();
  230. $this->assertEquals(1, $result);
  231. $this->translationListener->setTranslatableLocale('en_us');
  232. $q->setParameter('title', 'Mai%');
  233. $result = $q->getSingleScalarResult();
  234. $this->assertEquals(0, $result);
  235. }
  236. public function testSelectOrderedJoinedComponentTranslation()
  237. {
  238. $this->em
  239. ->getConfiguration()
  240. ->expects($this->any())
  241. ->method('getCustomHydrationMode')
  242. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  243. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
  244. $this->populateMore();
  245. $dql = 'SELECT a, c FROM ' . self::ARTICLE . ' a';
  246. $dql .= ' LEFT JOIN a.comments c';
  247. $dql .= ' ORDER BY a.title';
  248. $q = $this->em->createQuery($dql);
  249. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  250. // array hydration
  251. $this->translationListener->setTranslatableLocale('en_us');
  252. $result = $q->getArrayResult();
  253. $this->assertEquals(4, count($result));
  254. $this->assertEquals('Alfabet', $result[0]['title']);
  255. $this->assertEquals('Cabbages', $result[1]['title']);
  256. $this->assertEquals('Food', $result[2]['title']);
  257. $this->assertEquals('Woman', $result[3]['title']);
  258. $this->translationListener->setTranslatableLocale('lt_lt');
  259. $result = $q->getArrayResult();
  260. $this->assertEquals(4, count($result));
  261. $this->assertEquals('Alfabetas', $result[0]['title']);
  262. $this->assertEquals('Kopustai', $result[1]['title']);
  263. $this->assertEquals('Maistas', $result[2]['title']);
  264. $this->assertEquals('Moteris', $result[3]['title']);
  265. // object hydration
  266. $this->translationListener->setTranslatableLocale('en_us');
  267. $result = $q->getResult();
  268. $this->assertEquals(4, count($result));
  269. $this->assertEquals('Alfabet', $result[0]->getTitle());
  270. $this->assertEquals('Cabbages', $result[1]->getTitle());
  271. $this->assertEquals('Food', $result[2]->getTitle());
  272. $this->assertEquals('Woman', $result[3]->getTitle());
  273. $this->translationListener->setTranslatableLocale('lt_lt');
  274. $result = $q->getResult();
  275. $this->assertEquals(4, count($result));
  276. $this->assertEquals('Alfabetas', $result[0]->getTitle());
  277. $this->assertEquals('Kopustai', $result[1]->getTitle());
  278. $this->assertEquals('Maistas', $result[2]->getTitle());
  279. $this->assertEquals('Moteris', $result[3]->getTitle());
  280. }
  281. public function testSelectSecondJoinedComponentTranslation()
  282. {
  283. $this->em
  284. ->getConfiguration()
  285. ->expects($this->any())
  286. ->method('getCustomHydrationMode')
  287. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  288. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
  289. $dql = 'SELECT a, c FROM ' . self::ARTICLE . ' a';
  290. $dql .= ' LEFT JOIN a.comments c';
  291. $q = $this->em->createQuery($dql);
  292. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  293. // array hydration
  294. $this->translationListener->setTranslatableLocale('en_us');
  295. $result = $q->getArrayResult();
  296. $this->assertEquals(1, count($result));
  297. $food = $result[0];
  298. $this->assertEquals(4, count($food));
  299. $this->assertEquals('Food', $food['title']);
  300. $this->assertEquals('about food', $food['content']);
  301. $comments = $food['comments'];
  302. $this->assertEquals(2, count($comments));
  303. $good = $comments[0];
  304. $this->assertEquals(3, count($good));
  305. $this->assertEquals('good', $good['subject']);
  306. $this->assertEquals('food is good', $good['message']);
  307. $bad = $comments[1];
  308. $this->assertEquals(3, count($bad));
  309. $this->assertEquals('bad', $bad['subject']);
  310. $this->assertEquals('food is bad', $bad['message']);
  311. $this->translationListener->setTranslatableLocale('lt_lt');
  312. $result = $q->getArrayResult();
  313. $this->assertEquals(1, count($result));
  314. $food = $result[0];
  315. $this->assertEquals(4, count($food));
  316. $this->assertEquals('Maistas', $food['title']);
  317. $this->assertEquals('apie maista', $food['content']);
  318. $comments = $food['comments'];
  319. $this->assertEquals(2, count($comments));
  320. $good = $comments[0];
  321. $this->assertEquals(3, count($good));
  322. $this->assertEquals('geras', $good['subject']);
  323. $this->assertEquals('maistas yra geras', $good['message']);
  324. $bad = $comments[1];
  325. $this->assertEquals(3, count($bad));
  326. $this->assertEquals('blogas', $bad['subject']);
  327. $this->assertEquals('maistas yra blogas', $bad['message']);
  328. // object hydration
  329. $this->translationListener->setTranslatableLocale('en_us');
  330. $result = $q->getResult();
  331. $this->assertEquals(1, count($result));
  332. $food = $result[0];
  333. $this->assertEquals('Food', $food->getTitle());
  334. $this->assertEquals('about food', $food->getContent());
  335. $comments = $food->getComments();
  336. $this->assertEquals(2, count($comments));
  337. $good = $comments[0];
  338. $this->assertEquals('good', $good->getSubject());
  339. $this->assertEquals('food is good', $good->getMessage());
  340. $bad = $comments[1];
  341. $this->assertEquals('bad', $bad->getSubject());
  342. $this->assertEquals('food is bad', $bad->getMessage());
  343. $this->translationListener->setTranslatableLocale('lt_lt');
  344. $result = $q->getResult();
  345. $this->assertEquals(1, count($result));
  346. $food = $result[0];
  347. $this->assertEquals('Maistas', $food->getTitle());
  348. $this->assertEquals('apie maista', $food->getContent());
  349. $comments = $food->getComments();
  350. $this->assertEquals(2, count($comments));
  351. $good = $comments[0];
  352. $this->assertInstanceOf(self::COMMENT, $good);
  353. $this->assertEquals('geras', $good->getSubject());
  354. $this->assertEquals('maistas yra geras', $good->getMessage());
  355. $bad = $comments[1];
  356. $this->assertEquals('blogas', $bad->getSubject());
  357. $this->assertEquals('maistas yra blogas', $bad->getMessage());
  358. }
  359. public function testSelectSinglePartializedComponentTranslation()
  360. {
  361. $this->em
  362. ->getConfiguration()
  363. ->expects($this->any())
  364. ->method('getCustomHydrationMode')
  365. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  366. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
  367. $dql = 'SELECT a.title FROM ' . self::ARTICLE . ' a';
  368. $q = $this->em->createQuery($dql);
  369. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  370. // array hydration
  371. $this->translationListener->setTranslatableLocale('en_us');
  372. $result = $q->getArrayResult();
  373. $this->assertEquals(1, count($result));
  374. $food = $result[0];
  375. $this->assertEquals(1, count($food));
  376. $this->assertEquals('Food', $food['title']);
  377. $this->translationListener->setTranslatableLocale('lt_lt');
  378. $result = $q->getArrayResult();
  379. $this->assertEquals(1, count($result));
  380. $food = $result[0];
  381. $this->assertEquals(1, count($food));
  382. $this->assertEquals('Maistas', $food['title']);
  383. // object hydration
  384. $this->translationListener->setTranslatableLocale('en_us');
  385. $result = $q->getResult();
  386. $this->assertEquals(1, count($result));
  387. $food = $result[0];
  388. $this->assertEquals(1, count($food));
  389. $this->assertEquals('Food', $food['title']);
  390. $this->translationListener->setTranslatableLocale('lt_lt');
  391. $result = $q->getResult();
  392. $this->assertEquals(1, count($result));
  393. $food = $result[0];
  394. $this->assertEquals(1, count($food));
  395. $this->assertEquals('Maistas', $food['title']);
  396. }
  397. public function testSelectSingleComponentTranslation()
  398. {
  399. $this->em
  400. ->getConfiguration()
  401. ->expects($this->any())
  402. ->method('getCustomHydrationMode')
  403. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  404. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
  405. $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
  406. $q = $this->em->createQuery($dql);
  407. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  408. // array hydration
  409. $this->translationListener->setTranslatableLocale('en_us');
  410. $result = $q->getArrayResult();
  411. $this->assertEquals(1, count($result));
  412. $food = $result[0];
  413. $this->assertEquals(3, count($food));
  414. $this->assertEquals('Food', $food['title']);
  415. $this->assertEquals('about food', $food['content']);
  416. $this->translationListener->setTranslatableLocale('lt_lt');
  417. $result = $q->getArrayResult();
  418. $this->assertEquals(1, count($result));
  419. $food = $result[0];
  420. $this->assertEquals(3, count($food));
  421. $this->assertEquals('Maistas', $food['title']);
  422. $this->assertEquals('apie maista', $food['content']);
  423. // object hydration
  424. $this->translationListener->setTranslatableLocale('en_us');
  425. $result = $q->getResult();
  426. $this->assertEquals(1, count($result));
  427. $food = $result[0];
  428. $this->assertInstanceOf(self::ARTICLE, $food);
  429. $this->assertEquals('Food', $food->getTitle());
  430. $this->assertEquals('about food', $food->getContent());
  431. $this->translationListener->setTranslatableLocale('lt_lt');
  432. $result = $q->getResult();
  433. $this->assertEquals(1, count($result));
  434. $food = $result[0];
  435. $this->assertEquals('Maistas', $food->getTitle());
  436. $this->assertEquals('apie maista', $food->getContent());
  437. }
  438. /**
  439. * @group testSelectWithUnmappedField
  440. */
  441. public function testSelectWithUnmappedField()
  442. {
  443. $dql = 'SELECT a.title, count(a.id) AS num FROM ' . self::ARTICLE . ' a';
  444. $dql .= ' ORDER BY a.title';
  445. $q = $this->em->createQuery($dql);
  446. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  447. // array hydration
  448. $this->translationListener->setTranslatableLocale('en_us');
  449. $result = $q->getArrayResult();
  450. $this->assertEquals(1, count($result));
  451. $this->assertEquals('Food', $result[0]['title']);
  452. $this->assertEquals(1, $result[0]['num']);
  453. }
  454. protected function getUsedEntityFixtures()
  455. {
  456. return array(
  457. self::ARTICLE,
  458. self::TRANSLATION,
  459. self::COMMENT
  460. );
  461. }
  462. private function populateMore()
  463. {
  464. $repo = $this->em->getRepository(self::ARTICLE);
  465. $commentRepo = $this->em->getRepository(self::COMMENT);
  466. $this->translationListener->setTranslatableLocale('en_us');
  467. $alfabet = new Article;
  468. $alfabet->setTitle('Alfabet');
  469. $alfabet->setContent('hey wtf!');
  470. $woman = new Article;
  471. $woman->setTitle('Woman');
  472. $woman->setContent('i like them');
  473. $cabbages = new Article;
  474. $cabbages->setTitle('Cabbages');
  475. $cabbages->setContent('where went the woman?');
  476. $this->em->persist($alfabet);
  477. $this->em->persist($woman);
  478. $this->em->persist($cabbages);
  479. $this->em->flush();
  480. $this->em->clear();
  481. $this->translationListener->setTranslatableLocale('lt_lt');
  482. $alfabet = $repo->find(2);
  483. $alfabet->setTitle('Alfabetas');
  484. $alfabet->setContent('ei wtf!');
  485. $woman = $repo->find(3);
  486. $woman->setTitle('Moteris');
  487. $woman->setContent('as megstu jas');
  488. $cabbages = $repo->find(4);
  489. $cabbages->setTitle('Kopustai');
  490. $cabbages->setContent('kur dingo moteris?');
  491. $this->em->persist($alfabet);
  492. $this->em->persist($woman);
  493. $this->em->persist($cabbages);
  494. $this->em->flush();
  495. $this->em->clear();
  496. }
  497. private function populate()
  498. {
  499. $repo = $this->em->getRepository(self::ARTICLE);
  500. $commentRepo = $this->em->getRepository(self::COMMENT);
  501. $food = new Article;
  502. $food->setTitle('Food');
  503. $food->setContent('about food');
  504. $goodFood = new Comment;
  505. $goodFood->setArticle($food);
  506. $goodFood->setMessage('food is good');
  507. $goodFood->setSubject('good');
  508. $badFood = new Comment;
  509. $badFood->setArticle($food);
  510. $badFood->setMessage('food is bad');
  511. $badFood->setSubject('bad');
  512. $this->em->persist($food);
  513. $this->em->persist($goodFood);
  514. $this->em->persist($badFood);
  515. $this->em->flush();
  516. $this->em->clear();
  517. $this->translationListener->setTranslatableLocale('lt_lt');
  518. $food = $repo->find(1);
  519. $food->setTitle('Maistas');
  520. $food->setContent('apie maista');
  521. $goodFood = $commentRepo->find(1);
  522. $goodFood->setArticle($food);
  523. $goodFood->setMessage('maistas yra geras');
  524. $goodFood->setSubject('geras');
  525. $badFood = $commentRepo->find(2);
  526. $badFood->setArticle($food);
  527. $badFood->setMessage('maistas yra blogas');
  528. $badFood->setSubject('blogas');
  529. $this->em->persist($food);
  530. $this->em->persist($goodFood);
  531. $this->em->persist($badFood);
  532. $this->em->flush();
  533. $this->em->clear();
  534. }
  535. public function populateIssue109()
  536. {
  537. $this->translationListener->setTranslatableLocale('en');
  538. $text0 = new Article;
  539. $text0->setTitle('text0');
  540. $this->em->persist($text0);
  541. $text1 = new Article;
  542. $text1->setTitle('text1');
  543. $this->em->persist($text1);
  544. $na = new Article;
  545. $na->setTitle('NA');
  546. $this->em->persist($na);
  547. $out = new Article;
  548. $out->setTitle('Out');
  549. $this->em->persist($out);
  550. $this->em->flush();
  551. $this->translationListener->setTranslatableLocale('es');
  552. $text1->setTitle('texto1');
  553. $text0->setTitle('texto0');
  554. $this->em->persist($text1);
  555. $this->em->persist($text0);
  556. $this->em->flush();
  557. }
  558. }