123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745 |
- <?php
- namespace Gedmo\Translatable;
- use Doctrine\Common\EventManager;
- use Tool\BaseTestCaseORM;
- use Doctrine\ORM\Query;
- use Gedmo\Translatable\Query\TreeWalker\TranslationWalker;
- use Translatable\Fixture\Article;
- use Translatable\Fixture\Comment;
- /**
- * These are tests for translation query walker
- *
- * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
- * @link http://www.gediminasm.org
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- class TranslationQueryWalkerTest extends BaseTestCaseORM
- {
- const ARTICLE = 'Translatable\\Fixture\\Article';
- const COMMENT = 'Translatable\\Fixture\\Comment';
- const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
- const TREE_WALKER_TRANSLATION = 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker';
- private $translatableListener;
- protected function setUp()
- {
- parent::setUp();
- $evm = new EventManager;
- $this->translatableListener = new TranslatableListener();
- $this->translatableListener->setTranslatableLocale('en_us');
- $this->translatableListener->setDefaultLocale('en_us');
- $evm->addEventSubscriber($this->translatableListener);
- $this->getMockSqliteEntityManager($evm);
- $this->populate();
- }
- /**
- * @test
- */
- function shouldHandleQueryCache()
- {
- $cache = new \Doctrine\Common\Cache\ArrayCache();
- $this->em
- ->getConfiguration()
- ->expects($this->any())
- ->method('getQueryCacheImpl')
- ->will($this->returnValue($cache))
- ;
- $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- // array hydration
- $this->translatableListener->setTranslatableLocale('en_us');
- $result = $q->getArrayResult();
- $this->assertCount(1, $result);
- $q2 = clone $q;
- $q2->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- $result = $q->getArrayResult();
- $this->assertCount(1, $result);
- }
- public function testSubselectByTranslatedField()
- {
- $this->populateMore();
- $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
- $subSelect = 'SELECT a2.title FROM ' . self::ARTICLE . ' a2';
- $subSelect .= " WHERE a2.title LIKE '%ab%'";
- $dql .= " WHERE a.title IN ({$subSelect})";
- $dql .= ' ORDER BY a.title';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- // array hydration
- $this->translatableListener->setTranslatableLocale('en_us');
- $result = $q->getArrayResult();
- $this->assertCount(2, $result);
- $this->assertEquals('Alfabet', $result[0]['title']);
- $this->assertEquals('Cabbages', $result[1]['title']);
- }
- public function testSubselectStatements()
- {
- $this->populateMore();
- $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
- $subSelect = 'SELECT a2.id FROM ' . self::ARTICLE . ' a2';
- $subSelect .= " WHERE a2.title LIKE '%ab%'";
- $dql .= " WHERE a.id IN ({$subSelect})";
- $dql .= ' ORDER BY a.title';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- // array hydration
- $this->translatableListener->setTranslatableLocale('en_us');
- $result = $q->getArrayResult();
- $this->assertCount(2, $result);
- $this->assertEquals('Alfabet', $result[0]['title']);
- $this->assertEquals('Cabbages', $result[1]['title']);
- }
- public function testJoinedWithStatements()
- {
- $this->populateMore();
- $dql = 'SELECT a, c FROM ' . self::ARTICLE . ' a';
- $dql .= ' LEFT JOIN a.comments c WITH c.subject LIKE :lookup';
- $dql .= ' WHERE a.title LIKE :filter';
- $dql .= ' ORDER BY a.title';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- // array hydration
- $this->translatableListener->setTranslatableLocale('en_us');
- $q->setParameter('lookup', '%goo%');
- $q->setParameter('filter', 'Foo%');
- $result = $q->getArrayResult();
- $this->assertCount(1, $result);
- $this->assertEquals('Food', $result[0]['title']);
- $comments = $result[0]['comments'];
- $this->assertCount(1, $comments);
- $this->assertEquals('good', $comments[0]['subject']);
- }
- public function testSelectWithTranslationFallbackOnSimpleObjectHydration()
- {
- $this->em
- ->getConfiguration()
- ->expects($this->any())
- ->method('getCustomHydrationMode')
- ->with(TranslationWalker::HYDRATE_SIMPLE_OBJECT_TRANSLATION)
- ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\SimpleObjectHydrator'));
- $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- $this->translatableListener->setTranslatableLocale('ru_ru');
- $this->translatableListener->setTranslationFallback(false);
- // simple object hydration
- $this->startQueryLog();
- $result = $q->getResult(Query::HYDRATE_SIMPLEOBJECT);
- $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
- $this->assertEquals('', $result[0]->getTitle());
- $this->assertEquals('', $result[0]->getContent());
- $this->translatableListener->setTranslationFallback(true);
- $this->queryAnalyzer->cleanUp();
- $result = $q->getResult(Query::HYDRATE_SIMPLEOBJECT);
- $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
- //Default translation is en_us, so we expect the results in that locale
- $this->assertEquals('Food', $result[0]->getTitle());
- $this->assertEquals('about food', $result[0]->getContent());
- }
- public function testSelectWithTranslationFallbackOnArrayHydration()
- {
- $dql = 'SELECT a, c FROM ' . self::ARTICLE . ' a';
- $dql .= ' LEFT JOIN a.comments c';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- $this->translatableListener->setTranslatableLocale('ru_ru');
- $this->translatableListener->setTranslationFallback(false);
- // array hydration
- $this->startQueryLog();
- $result = $q->getArrayResult();
- $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
- $this->assertEquals('', $result[0]['title']);
- $this->assertEquals('', $result[0]['content']);
- $this->translatableListener->setTranslationFallback(true);
- $this->queryAnalyzer->cleanUp();
- $result = $q->getArrayResult();
- $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
- //Default translation is en_us, so we expect the results in that locale
- $this->assertEquals('Food', $result[0]['title']);
- $this->assertEquals('about food', $result[0]['content']);
- }
- public function testSelectWithOptionalFallbackOnSimpleObjectHydration()
- {
- $this->em
- ->getConfiguration()
- ->expects($this->any())
- ->method('getCustomHydrationMode')
- ->with(TranslationWalker::HYDRATE_SIMPLE_OBJECT_TRANSLATION)
- ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\SimpleObjectHydrator'));
- $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- $this->translatableListener->setTranslatableLocale('ru_ru');
- $this->translatableListener->setTranslationFallback(false);
- // simple object hydration
- $this->startQueryLog();
- $result = $q->getResult(Query::HYDRATE_SIMPLEOBJECT);
- $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
- $this->assertEquals('', $result[0]->getTitle());
- $this->assertEquals('John Doe', $result[0]->getAuthor()); // optional fallback is true, force fallback
- $this->assertEquals(0, $result[0]->getViews());
- $this->translatableListener->setTranslationFallback(true);
- $this->queryAnalyzer->cleanUp();
- $result = $q->getResult(Query::HYDRATE_SIMPLEOBJECT);
- $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
- //Default translation is en_us, so we expect the results in that locale
- $this->assertEquals('Food', $result[0]->getTitle());
- $this->assertEquals('John Doe', $result[0]->getAuthor());
- $this->assertEquals(0, $result[0]->getViews()); // optional fallback is false, thus no translation required
- }
- /**
- * @test
- */
- public function shouldBeAbleToUseInnerJoinStrategyForTranslations()
- {
- $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- $q->setHint(TranslatableListener::HINT_INNER_JOIN, true);
- $this->translatableListener->setTranslatableLocale('ru_ru');
- $this->translatableListener->setTranslationFallback(false);
- // array hydration
- $result = $q->getArrayResult();
- $this->assertCount(0, $result);
- }
- /**
- * referres to issue #755
- * @test
- */
- public function shouldBeAbleToOverrideTranslationFallbackByHint()
- {
- $this->translatableListener->setTranslatableLocale('lt_lt');
- $this->translatableListener->setTranslationFallback(false);
- $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- $q->setHint(TranslatableListener::HINT_TRANSLATABLE_LOCALE, 'undefined');
- $q->setHint(TranslatableListener::HINT_FALLBACK, true);
- // array hydration
- $result = $q->getArrayResult();
- $this->assertCount(1, $result);
- $this->assertEquals('Food', $result[0]['title']);
- // fallback false hint
- $q->setHint(TranslatableListener::HINT_FALLBACK, false);
- // array hydration
- $result = $q->getArrayResult();
- $this->assertCount(1, $result);
- $this->assertEquals(null, $result[0]['title']);
- }
- /**
- * @test
- */
- public function shouldBeAbleToOverrideTranslatableLocale()
- {
- $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- $q->setHint(TranslatableListener::HINT_TRANSLATABLE_LOCALE, 'lt_lt');
- $this->translatableListener->setTranslatableLocale('ru_ru');
- $this->translatableListener->setTranslationFallback(false);
- // array hydration
- $result = $q->getArrayResult();
- $this->assertCount(1, $result);
- $this->assertEquals('Maistas', $result[0]['title']);
- }
- public function testSelectWithTranslationFallbackOnObjectHydration()
- {
- $this->em
- ->getConfiguration()
- ->expects($this->any())
- ->method('getCustomHydrationMode')
- ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
- ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
- $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- $this->translatableListener->setTranslatableLocale('ru_ru');
- $this->translatableListener->setTranslationFallback(false);
- // object hydration
- $this->startQueryLog();
- $result = $q->getResult();
- $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
- $this->assertEquals('', $result[0]->getTitle());
- $this->assertEquals('', $result[0]->getContent());
- $this->translatableListener->setTranslationFallback(true);
- $this->queryAnalyzer->cleanUp();
- $result = $q->getResult();
- $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
- //Default translation is en_us, so we expect the results in that locale
- $this->assertEquals('Food', $result[0]->getTitle());
- $this->assertEquals('about food', $result[0]->getContent());
- // test fallback hint
- $this->translatableListener->setTranslationFallback(false);
- $q->setHint(TranslatableListener::HINT_FALLBACK, 1);
- $result = $q->getResult();
- //Default translation is en_us, so we expect the results in that locale
- $this->assertEquals('Food', $result[0]->getTitle());
- $this->assertEquals('about food', $result[0]->getContent());
- // test fallback hint
- $this->translatableListener->setTranslationFallback(true);
- $q->setHint(TranslatableListener::HINT_FALLBACK, 0);
- $result = $q->getResult();
- //Default translation is en_us, so we expect the results in that locale
- $this->assertEquals('', $result[0]->getTitle());
- $this->assertEquals('', $result[0]->getContent());
- }
- public function testSelectCountStatement()
- {
- $dql = 'SELECT COUNT(a) FROM ' . self::ARTICLE . ' a';
- $dql .= ' WHERE a.title LIKE :title';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- $this->translatableListener->setTranslatableLocale('en_us');
- $q->setParameter('title', 'Foo%');
- $result = $q->getSingleScalarResult();
- $this->assertEquals(1, $result);
- $this->translatableListener->setTranslatableLocale('lt_lt');
- $q->setParameter('title', 'Mai%');
- $result = $q->getSingleScalarResult();
- $this->assertEquals(1, $result);
- $this->translatableListener->setTranslatableLocale('en_us');
- $q->setParameter('title', 'Mai%');
- $result = $q->getSingleScalarResult();
- $this->assertEquals(0, $result);
- }
- public function testSelectOrderedJoinedComponentTranslation()
- {
- $this->em
- ->getConfiguration()
- ->expects($this->any())
- ->method('getCustomHydrationMode')
- ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
- ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
- $this->populateMore();
- $dql = 'SELECT a, c FROM ' . self::ARTICLE . ' a';
- $dql .= ' LEFT JOIN a.comments c';
- $dql .= ' ORDER BY a.title';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- // array hydration
- $this->translatableListener->setTranslatableLocale('en_us');
- $result = $q->getArrayResult();
- $this->assertCount(4, $result);
- $this->assertEquals('Alfabet', $result[0]['title']);
- $this->assertEquals('Cabbages', $result[1]['title']);
- $this->assertEquals('Food', $result[2]['title']);
- $this->assertEquals('Woman', $result[3]['title']);
- $this->translatableListener->setTranslatableLocale('lt_lt');
- $result = $q->getArrayResult();
- $this->assertCount(4, $result);
- $this->assertEquals('Alfabetas', $result[0]['title']);
- $this->assertEquals('Kopustai', $result[1]['title']);
- $this->assertEquals('Maistas', $result[2]['title']);
- $this->assertEquals('Moteris', $result[3]['title']);
- // object hydration
- $this->translatableListener->setTranslatableLocale('en_us');
- $result = $q->getResult();
- $this->assertCount(4, $result);
- $this->assertEquals('Alfabet', $result[0]->getTitle());
- $this->assertEquals('Cabbages', $result[1]->getTitle());
- $this->assertEquals('Food', $result[2]->getTitle());
- $this->assertEquals('Woman', $result[3]->getTitle());
- $this->translatableListener->setTranslatableLocale('lt_lt');
- $result = $q->getResult();
- $this->assertCount(4, $result);
- $this->assertEquals('Alfabetas', $result[0]->getTitle());
- $this->assertEquals('Kopustai', $result[1]->getTitle());
- $this->assertEquals('Maistas', $result[2]->getTitle());
- $this->assertEquals('Moteris', $result[3]->getTitle());
- }
- public function testSelectOrderedByTranslatableInteger()
- {
- // Given
- $this->populateMore();
- $dql = 'SELECT a.title, a.views FROM ' . self::ARTICLE . ' a';
- $dql .= ' ORDER BY a.views';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- // Test original
- $this->translatableListener->setTranslatableLocale('en_us');
- $result = $q->getArrayResult();
- array_walk($result, function($value, $key) use(&$result) {
- // Make each record be a "Title - Views" string
- $result[$key] = implode(" - ", $value);
- });
- $this->assertEquals(
- array("Alfabet - 1", "Food - 99", "Cabbages - 2222", "Woman - 3333"), $result,
- "Original of localizible integers should be sorted numerically"
- );
- $this->translatableListener->setTranslatableLocale('lt_lt');
- $result = $q->getArrayResult();
- array_walk($result, function($value, $key) use(&$result) {
- // Make each record be a "Title - Views" string
- $result[$key] = implode(" - ", $value);
- });
- $this->assertEquals(
- array("Moteris - 33", "Alfabetas - 111", "Maistas - 999", "Kopustai - 22222"), $result,
- "Localized integers should be sorted numerically"
- );
- }
- public function testSelectSecondJoinedComponentTranslation()
- {
- $this->em
- ->getConfiguration()
- ->expects($this->any())
- ->method('getCustomHydrationMode')
- ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
- ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
- $dql = 'SELECT a, c FROM ' . self::ARTICLE . ' a';
- $dql .= ' LEFT JOIN a.comments c';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- // array hydration
- $this->translatableListener->setTranslatableLocale('en_us');
- $result = $q->getArrayResult();
- $this->assertCount(1, $result);
- $food = $result[0];
- $this->assertCount(6, $food);
- $this->assertEquals('Food', $food['title']);
- $this->assertEquals('about food', $food['content']);
- $comments = $food['comments'];
- $this->assertCount(2, $comments);
- $good = $comments[0];
- $this->assertCount(3, $good);
- $this->assertEquals('good', $good['subject']);
- $this->assertEquals('food is good', $good['message']);
- $bad = $comments[1];
- $this->assertCount(3, $bad);
- $this->assertEquals('bad', $bad['subject']);
- $this->assertEquals('food is bad', $bad['message']);
- $this->translatableListener->setTranslatableLocale('lt_lt');
- $result = $q->getArrayResult();
- $this->assertCount(1, $result);
- $food = $result[0];
- $this->assertCount(6, $food);
- $this->assertEquals('Maistas', $food['title']);
- $this->assertEquals('apie maista', $food['content']);
- $comments = $food['comments'];
- $this->assertCount(2, $comments);
- $good = $comments[0];
- $this->assertCount(3, $good);
- $this->assertEquals('geras', $good['subject']);
- $this->assertEquals('maistas yra geras', $good['message']);
- $bad = $comments[1];
- $this->assertCount(3, $bad);
- $this->assertEquals('blogas', $bad['subject']);
- $this->assertEquals('maistas yra blogas', $bad['message']);
- // object hydration
- $this->translatableListener->setTranslatableLocale('en_us');
- $result = $q->getResult();
- $this->assertCount(1, $result);
- $food = $result[0];
- $this->assertEquals('Food', $food->getTitle());
- $this->assertEquals('about food', $food->getContent());
- $comments = $food->getComments();
- $this->assertCount(2, $comments);
- $good = $comments[0];
- $this->assertEquals('good', $good->getSubject());
- $this->assertEquals('food is good', $good->getMessage());
- $bad = $comments[1];
- $this->assertEquals('bad', $bad->getSubject());
- $this->assertEquals('food is bad', $bad->getMessage());
- $this->translatableListener->setTranslatableLocale('lt_lt');
- $result = $q->getResult();
- $this->assertCount(1, $result);
- $food = $result[0];
- $this->assertEquals('Maistas', $food->getTitle());
- $this->assertEquals('apie maista', $food->getContent());
- $comments = $food->getComments();
- $this->assertCount(2, $comments);
- $good = $comments[0];
- $this->assertInstanceOf(self::COMMENT, $good);
- $this->assertEquals('geras', $good->getSubject());
- $this->assertEquals('maistas yra geras', $good->getMessage());
- $bad = $comments[1];
- $this->assertEquals('blogas', $bad->getSubject());
- $this->assertEquals('maistas yra blogas', $bad->getMessage());
- }
- public function testSelectSinglePartializedComponentTranslation()
- {
- $this->em
- ->getConfiguration()
- ->expects($this->any())
- ->method('getCustomHydrationMode')
- ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
- ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
- $dql = 'SELECT a.title FROM ' . self::ARTICLE . ' a';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- // array hydration
- $this->translatableListener->setTranslatableLocale('en_us');
- $result = $q->getArrayResult();
- $this->assertCount(1, $result);
- $food = $result[0];
- $this->assertCount(1, $food);
- $this->assertEquals('Food', $food['title']);
- $this->translatableListener->setTranslatableLocale('lt_lt');
- $result = $q->getArrayResult();
- $this->assertCount(1, $result);
- $food = $result[0];
- $this->assertCount(1, $food);
- $this->assertEquals('Maistas', $food['title']);
- // object hydration
- $this->translatableListener->setTranslatableLocale('en_us');
- $result = $q->getResult();
- $this->assertCount(1, $result);
- $food = $result[0];
- $this->assertCount(1, $food);
- $this->assertEquals('Food', $food['title']);
- $this->translatableListener->setTranslatableLocale('lt_lt');
- $result = $q->getResult();
- $this->assertCount(1, $result);
- $food = $result[0];
- $this->assertCount(1, $food);
- $this->assertEquals('Maistas', $food['title']);
- }
- public function testSelectSingleComponentTranslation()
- {
- $this->em
- ->getConfiguration()
- ->expects($this->any())
- ->method('getCustomHydrationMode')
- ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
- ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
- $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- // array hydration
- $this->translatableListener->setTranslatableLocale('en_us');
- $result = $q->getArrayResult();
- $this->assertCount(1, $result);
- $food = $result[0];
- $this->assertCount(5, $food);
- $this->assertEquals('Food', $food['title']);
- $this->assertEquals('about food', $food['content']);
- $this->translatableListener->setTranslatableLocale('lt_lt');
- $result = $q->getArrayResult();
- $this->assertCount(1, $result);
- $food = $result[0];
- $this->assertCount(5, $food);
- $this->assertEquals('Maistas', $food['title']);
- $this->assertEquals('apie maista', $food['content']);
- // object hydration
- $this->translatableListener->setTranslatableLocale('en_us');
- $result = $q->getResult();
- $this->assertCount(1, $result);
- $food = $result[0];
- $this->assertInstanceOf(self::ARTICLE, $food);
- $this->assertEquals('Food', $food->getTitle());
- $this->assertEquals('about food', $food->getContent());
- $this->translatableListener->setTranslatableLocale('lt_lt');
- $result = $q->getResult();
- $this->assertCount(1, $result);
- $food = $result[0];
- $this->assertEquals('Maistas', $food->getTitle());
- $this->assertEquals('apie maista', $food->getContent());
- }
- /**
- * @group testSelectWithUnmappedField
- */
- public function testSelectWithUnmappedField()
- {
- $dql = 'SELECT a.title, count(a.id) AS num FROM ' . self::ARTICLE . ' a';
- $dql .= ' ORDER BY a.title';
- $q = $this->em->createQuery($dql);
- $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
- // array hydration
- $this->translatableListener->setTranslatableLocale('en_us');
- $result = $q->getArrayResult();
- $this->assertCount(1, $result);
- $this->assertEquals('Food', $result[0]['title']);
- $this->assertEquals(1, $result[0]['num']);
- }
- protected function getUsedEntityFixtures()
- {
- return array(
- self::ARTICLE,
- self::TRANSLATION,
- self::COMMENT
- );
- }
- private function populateMore()
- {
- $repo = $this->em->getRepository(self::ARTICLE);
- $commentRepo = $this->em->getRepository(self::COMMENT);
- $this->translatableListener->setTranslatableLocale('en_us');
- $alfabet = new Article;
- $alfabet->setTitle('Alfabet');
- $alfabet->setContent('hey wtf!');
- $alfabet->setViews(1);
- $woman = new Article;
- $woman->setTitle('Woman');
- $woman->setContent('i like them');
- $woman->setViews(3333);
- $cabbages = new Article;
- $cabbages->setTitle('Cabbages');
- $cabbages->setContent('where went the woman?');
- $cabbages->setViews(2222);
- $this->em->persist($alfabet);
- $this->em->persist($woman);
- $this->em->persist($cabbages);
- $this->em->flush();
- $this->em->clear();
- $this->translatableListener->setTranslatableLocale('lt_lt');
- $alfabet = $repo->find(2);
- $alfabet->setTitle('Alfabetas');
- $alfabet->setContent('ei wtf!');
- $alfabet->setViews(111);
- $woman = $repo->find(3);
- $woman->setTitle('Moteris');
- $woman->setContent('as megstu jas');
- $woman->setViews(33);
- $cabbages = $repo->find(4);
- $cabbages->setTitle('Kopustai');
- $cabbages->setContent('kur dingo moteris?');
- $cabbages->setViews(22222);
- $this->em->persist($alfabet);
- $this->em->persist($woman);
- $this->em->persist($cabbages);
- $this->em->flush();
- $this->em->clear();
- }
- private function populate()
- {
- $repo = $this->em->getRepository(self::ARTICLE);
- $commentRepo = $this->em->getRepository(self::COMMENT);
- $food = new Article;
- $food->setTitle('Food');
- $food->setContent('about food');
- $food->setAuthor('John Doe');
- $food->setViews(99);
- $goodFood = new Comment;
- $goodFood->setArticle($food);
- $goodFood->setMessage('food is good');
- $goodFood->setSubject('good');
- $badFood = new Comment;
- $badFood->setArticle($food);
- $badFood->setMessage('food is bad');
- $badFood->setSubject('bad');
- $this->em->persist($food);
- $this->em->persist($goodFood);
- $this->em->persist($badFood);
- $this->em->flush();
- $this->em->clear();
- $this->translatableListener->setTranslatableLocale('lt_lt');
- $food = $repo->find(1);
- $food->setTitle('Maistas');
- $food->setContent('apie maista');
- $food->setViews(999);
- $goodFood = $commentRepo->find(1);
- $goodFood->setArticle($food);
- $goodFood->setMessage('maistas yra geras');
- $goodFood->setSubject('geras');
- $badFood = $commentRepo->find(2);
- $badFood->setArticle($food);
- $badFood->setMessage('maistas yra blogas');
- $badFood->setSubject('blogas');
- $this->em->persist($food);
- $this->em->persist($goodFood);
- $this->em->persist($badFood);
- $this->em->flush();
- $this->em->clear();
- }
- }
|