TranslationQueryWalkerTest.php 29 KB

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