TranslationQueryWalkerTest.php 25 KB

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