/** * @return $this * @throws \Exception */ public function setUpTranslateable() { $defaultLocale = $this->locale->getDefaultLocale(); if ($defaultLocale === null) { throw new \Exception('Default locale not found! Fraym is not correctly installed, please reinstall Fraym.'); } $this->translatableListener = new \Gedmo\Translatable\TranslatableListener(); $this->translatableListener->setDefaultLocale($defaultLocale->locale); $this->translatableListener->setAnnotationReader($this->cachedAnnotationReader); $this->translatableListener->setTranslationFallback(true); $this->translatableListener->setPersistDefaultLocaleTranslation(true); $this->eventManager->addEventSubscriber($this->translatableListener); return $this; }
/** * @test */ function shouldSelectWithTranslationFallbackOnObjectHydration() { $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()); }