TranslationQueryWalkerTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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 testSelectOrderedByTranslatableInteger()
  299. {
  300. // Given
  301. $this->populateMore();
  302. $dql = 'SELECT a.title, a.views FROM ' . self::ARTICLE . ' a';
  303. $dql .= ' ORDER BY a.views';
  304. $q = $this->em->createQuery($dql);
  305. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  306. // Test original
  307. $this->translationListener->setTranslatableLocale('en_us');
  308. $result = $q->getArrayResult();
  309. array_walk($result, function($value, $key) use(&$result) {
  310. // Make each record be a "Title - Views" string
  311. $result[$key] = implode(" - ", $value);
  312. });
  313. $this->assertEquals(
  314. array("Alfabet - 1", "Food - 99", "Cabbages - 2222", "Woman - 3333"), $result,
  315. "Original of localizible integers should be sorted numerically"
  316. );
  317. $this->translationListener->setTranslatableLocale('lt_lt');
  318. $result = $q->getArrayResult();
  319. array_walk($result, function($value, $key) use(&$result) {
  320. // Make each record be a "Title - Views" string
  321. $result[$key] = implode(" - ", $value);
  322. });
  323. $this->assertEquals(
  324. array("Moteris - 33", "Alfabetas - 111", "Maistas - 999", "Kopustai - 22222"), $result,
  325. "Localized integers should be sorted numerically"
  326. );
  327. }
  328. public function testSelectSecondJoinedComponentTranslation()
  329. {
  330. $this->em
  331. ->getConfiguration()
  332. ->expects($this->any())
  333. ->method('getCustomHydrationMode')
  334. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  335. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
  336. $dql = 'SELECT a, c FROM ' . self::ARTICLE . ' a';
  337. $dql .= ' LEFT JOIN a.comments c';
  338. $q = $this->em->createQuery($dql);
  339. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  340. // array hydration
  341. $this->translationListener->setTranslatableLocale('en_us');
  342. $result = $q->getArrayResult();
  343. $this->assertEquals(1, count($result));
  344. $food = $result[0];
  345. $this->assertEquals(5, count($food));
  346. $this->assertEquals('Food', $food['title']);
  347. $this->assertEquals('about food', $food['content']);
  348. $comments = $food['comments'];
  349. $this->assertEquals(2, count($comments));
  350. $good = $comments[0];
  351. $this->assertEquals(3, count($good));
  352. $this->assertEquals('good', $good['subject']);
  353. $this->assertEquals('food is good', $good['message']);
  354. $bad = $comments[1];
  355. $this->assertEquals(3, count($bad));
  356. $this->assertEquals('bad', $bad['subject']);
  357. $this->assertEquals('food is bad', $bad['message']);
  358. $this->translationListener->setTranslatableLocale('lt_lt');
  359. $result = $q->getArrayResult();
  360. $this->assertEquals(1, count($result));
  361. $food = $result[0];
  362. $this->assertEquals(5, count($food));
  363. $this->assertEquals('Maistas', $food['title']);
  364. $this->assertEquals('apie maista', $food['content']);
  365. $comments = $food['comments'];
  366. $this->assertEquals(2, count($comments));
  367. $good = $comments[0];
  368. $this->assertEquals(3, count($good));
  369. $this->assertEquals('geras', $good['subject']);
  370. $this->assertEquals('maistas yra geras', $good['message']);
  371. $bad = $comments[1];
  372. $this->assertEquals(3, count($bad));
  373. $this->assertEquals('blogas', $bad['subject']);
  374. $this->assertEquals('maistas yra blogas', $bad['message']);
  375. // object hydration
  376. $this->translationListener->setTranslatableLocale('en_us');
  377. $result = $q->getResult();
  378. $this->assertEquals(1, count($result));
  379. $food = $result[0];
  380. $this->assertEquals('Food', $food->getTitle());
  381. $this->assertEquals('about food', $food->getContent());
  382. $comments = $food->getComments();
  383. $this->assertEquals(2, count($comments));
  384. $good = $comments[0];
  385. $this->assertEquals('good', $good->getSubject());
  386. $this->assertEquals('food is good', $good->getMessage());
  387. $bad = $comments[1];
  388. $this->assertEquals('bad', $bad->getSubject());
  389. $this->assertEquals('food is bad', $bad->getMessage());
  390. $this->translationListener->setTranslatableLocale('lt_lt');
  391. $result = $q->getResult();
  392. $this->assertEquals(1, count($result));
  393. $food = $result[0];
  394. $this->assertEquals('Maistas', $food->getTitle());
  395. $this->assertEquals('apie maista', $food->getContent());
  396. $comments = $food->getComments();
  397. $this->assertEquals(2, count($comments));
  398. $good = $comments[0];
  399. $this->assertInstanceOf(self::COMMENT, $good);
  400. $this->assertEquals('geras', $good->getSubject());
  401. $this->assertEquals('maistas yra geras', $good->getMessage());
  402. $bad = $comments[1];
  403. $this->assertEquals('blogas', $bad->getSubject());
  404. $this->assertEquals('maistas yra blogas', $bad->getMessage());
  405. }
  406. public function testSelectSinglePartializedComponentTranslation()
  407. {
  408. $this->em
  409. ->getConfiguration()
  410. ->expects($this->any())
  411. ->method('getCustomHydrationMode')
  412. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  413. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
  414. $dql = 'SELECT a.title FROM ' . self::ARTICLE . ' a';
  415. $q = $this->em->createQuery($dql);
  416. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  417. // array hydration
  418. $this->translationListener->setTranslatableLocale('en_us');
  419. $result = $q->getArrayResult();
  420. $this->assertEquals(1, count($result));
  421. $food = $result[0];
  422. $this->assertEquals(1, count($food));
  423. $this->assertEquals('Food', $food['title']);
  424. $this->translationListener->setTranslatableLocale('lt_lt');
  425. $result = $q->getArrayResult();
  426. $this->assertEquals(1, count($result));
  427. $food = $result[0];
  428. $this->assertEquals(1, count($food));
  429. $this->assertEquals('Maistas', $food['title']);
  430. // object hydration
  431. $this->translationListener->setTranslatableLocale('en_us');
  432. $result = $q->getResult();
  433. $this->assertEquals(1, count($result));
  434. $food = $result[0];
  435. $this->assertEquals(1, count($food));
  436. $this->assertEquals('Food', $food['title']);
  437. $this->translationListener->setTranslatableLocale('lt_lt');
  438. $result = $q->getResult();
  439. $this->assertEquals(1, count($result));
  440. $food = $result[0];
  441. $this->assertEquals(1, count($food));
  442. $this->assertEquals('Maistas', $food['title']);
  443. }
  444. public function testSelectSingleComponentTranslation()
  445. {
  446. $this->em
  447. ->getConfiguration()
  448. ->expects($this->any())
  449. ->method('getCustomHydrationMode')
  450. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  451. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
  452. $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
  453. $q = $this->em->createQuery($dql);
  454. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  455. // array hydration
  456. $this->translationListener->setTranslatableLocale('en_us');
  457. $result = $q->getArrayResult();
  458. $this->assertEquals(1, count($result));
  459. $food = $result[0];
  460. $this->assertEquals(4, count($food));
  461. $this->assertEquals('Food', $food['title']);
  462. $this->assertEquals('about food', $food['content']);
  463. $this->translationListener->setTranslatableLocale('lt_lt');
  464. $result = $q->getArrayResult();
  465. $this->assertEquals(1, count($result));
  466. $food = $result[0];
  467. $this->assertEquals(4, count($food));
  468. $this->assertEquals('Maistas', $food['title']);
  469. $this->assertEquals('apie maista', $food['content']);
  470. // object hydration
  471. $this->translationListener->setTranslatableLocale('en_us');
  472. $result = $q->getResult();
  473. $this->assertEquals(1, count($result));
  474. $food = $result[0];
  475. $this->assertInstanceOf(self::ARTICLE, $food);
  476. $this->assertEquals('Food', $food->getTitle());
  477. $this->assertEquals('about food', $food->getContent());
  478. $this->translationListener->setTranslatableLocale('lt_lt');
  479. $result = $q->getResult();
  480. $this->assertEquals(1, count($result));
  481. $food = $result[0];
  482. $this->assertEquals('Maistas', $food->getTitle());
  483. $this->assertEquals('apie maista', $food->getContent());
  484. }
  485. /**
  486. * @group testSelectWithUnmappedField
  487. */
  488. public function testSelectWithUnmappedField()
  489. {
  490. $dql = 'SELECT a.title, count(a.id) AS num FROM ' . self::ARTICLE . ' a';
  491. $dql .= ' ORDER BY a.title';
  492. $q = $this->em->createQuery($dql);
  493. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  494. // array hydration
  495. $this->translationListener->setTranslatableLocale('en_us');
  496. $result = $q->getArrayResult();
  497. $this->assertEquals(1, count($result));
  498. $this->assertEquals('Food', $result[0]['title']);
  499. $this->assertEquals(1, $result[0]['num']);
  500. }
  501. protected function getUsedEntityFixtures()
  502. {
  503. return array(
  504. self::ARTICLE,
  505. self::TRANSLATION,
  506. self::COMMENT
  507. );
  508. }
  509. private function populateMore()
  510. {
  511. $repo = $this->em->getRepository(self::ARTICLE);
  512. $commentRepo = $this->em->getRepository(self::COMMENT);
  513. $this->translationListener->setTranslatableLocale('en_us');
  514. $alfabet = new Article;
  515. $alfabet->setTitle('Alfabet');
  516. $alfabet->setContent('hey wtf!');
  517. $alfabet->setViews(1);
  518. $woman = new Article;
  519. $woman->setTitle('Woman');
  520. $woman->setContent('i like them');
  521. $woman->setViews(3333);
  522. $cabbages = new Article;
  523. $cabbages->setTitle('Cabbages');
  524. $cabbages->setContent('where went the woman?');
  525. $cabbages->setViews(2222);
  526. $this->em->persist($alfabet);
  527. $this->em->persist($woman);
  528. $this->em->persist($cabbages);
  529. $this->em->flush();
  530. $this->em->clear();
  531. $this->translationListener->setTranslatableLocale('lt_lt');
  532. $alfabet = $repo->find(2);
  533. $alfabet->setTitle('Alfabetas');
  534. $alfabet->setContent('ei wtf!');
  535. $alfabet->setViews(111);
  536. $woman = $repo->find(3);
  537. $woman->setTitle('Moteris');
  538. $woman->setContent('as megstu jas');
  539. $woman->setViews(33);
  540. $cabbages = $repo->find(4);
  541. $cabbages->setTitle('Kopustai');
  542. $cabbages->setContent('kur dingo moteris?');
  543. $cabbages->setViews(22222);
  544. $this->em->persist($alfabet);
  545. $this->em->persist($woman);
  546. $this->em->persist($cabbages);
  547. $this->em->flush();
  548. $this->em->clear();
  549. }
  550. private function populate()
  551. {
  552. $repo = $this->em->getRepository(self::ARTICLE);
  553. $commentRepo = $this->em->getRepository(self::COMMENT);
  554. $food = new Article;
  555. $food->setTitle('Food');
  556. $food->setContent('about food');
  557. $food->setViews(99);
  558. $goodFood = new Comment;
  559. $goodFood->setArticle($food);
  560. $goodFood->setMessage('food is good');
  561. $goodFood->setSubject('good');
  562. $badFood = new Comment;
  563. $badFood->setArticle($food);
  564. $badFood->setMessage('food is bad');
  565. $badFood->setSubject('bad');
  566. $this->em->persist($food);
  567. $this->em->persist($goodFood);
  568. $this->em->persist($badFood);
  569. $this->em->flush();
  570. $this->em->clear();
  571. $this->translationListener->setTranslatableLocale('lt_lt');
  572. $food = $repo->find(1);
  573. $food->setTitle('Maistas');
  574. $food->setContent('apie maista');
  575. $food->setViews(999);
  576. $goodFood = $commentRepo->find(1);
  577. $goodFood->setArticle($food);
  578. $goodFood->setMessage('maistas yra geras');
  579. $goodFood->setSubject('geras');
  580. $badFood = $commentRepo->find(2);
  581. $badFood->setArticle($food);
  582. $badFood->setMessage('maistas yra blogas');
  583. $badFood->setSubject('blogas');
  584. $this->em->persist($food);
  585. $this->em->persist($goodFood);
  586. $this->em->persist($badFood);
  587. $this->em->flush();
  588. $this->em->clear();
  589. }
  590. }