Selaa lähdekoodia

[translatable] closes #138 improved query hint sql replacements

gediminasm 13 vuotta sitten
vanhempi
commit
ebf3e3a4dd

+ 23 - 25
lib/Gedmo/Translatable/Query/TreeWalker/TranslationWalker.php

@@ -189,11 +189,7 @@ class TranslationWalker extends SqlWalker
             }
             $this->getQuery()->setHint(self::HINT_TRANSLATION_FALLBACKS, $fallbackAliases);
         }
-        $result = str_replace(
-            array_keys($this->replacements),
-            array_values($this->replacements),
-            $result
-        );
+        $result = $this->replace($this->replacements, $result);
         $result .= $fallbackSql;
         return $result;
     }
@@ -216,11 +212,7 @@ class TranslationWalker extends SqlWalker
     public function walkWhereClause($whereClause)
     {
         $result = parent::walkWhereClause($whereClause);
-        return str_replace(
-            array_keys($this->replacements),
-            array_values($this->replacements),
-            $result
-        );
+        return $this->replace($this->replacements, $result);
     }
 
     /**
@@ -229,11 +221,7 @@ class TranslationWalker extends SqlWalker
     public function walkHavingClause($havingClause)
     {
         $result = parent::walkHavingClause($havingClause);
-        return str_replace(
-            array_keys($this->replacements),
-            array_values($this->replacements),
-            $result
-        );
+        return $this->replace($this->replacements, $result);
     }
 
     /**
@@ -242,11 +230,7 @@ class TranslationWalker extends SqlWalker
     public function walkOrderByClause($orderByClause)
     {
         $result = parent::walkOrderByClause($orderByClause);
-        return str_replace(
-            array_keys($this->replacements),
-            array_values($this->replacements),
-            $result
-        );
+        return $this->replace($this->replacements, $result);
     }
 
     /**
@@ -277,11 +261,7 @@ class TranslationWalker extends SqlWalker
     public function walkSimpleSelectClause($simpleSelectClause)
     {
         $result = parent::walkSimpleSelectClause($simpleSelectClause);
-        return str_replace(
-            array_keys($this->replacements),
-            array_values($this->replacements),
-            $result
-        );
+        return $this->replace($this->replacements, $result);
     }
 
     /**
@@ -412,4 +392,22 @@ class TranslationWalker extends SqlWalker
         }
         return $translationListener;
     }
+
+    /**
+     * Replaces given sql $str with required
+     * results
+     *
+     * @param array $repl
+     * @param string $str
+     * @return string
+     */
+    private function replace(array $repl, $str)
+    {
+        foreach ($repl as $target => $result) {
+            $str = preg_replace_callback('/(\s|\()('.$target.')(\s|\))/smi', function($m) use ($result) {
+                return $m[1].$result.$m[3];
+            }, $str);
+        }
+        return $str;
+    }
 }

+ 58 - 0
tests/Gedmo/Translatable/Fixture/Issue138/Article.php

@@ -0,0 +1,58 @@
+<?php
+
+namespace Translatable\Fixture\Issue138;
+
+use Gedmo\Mapping\Annotation as Gedmo;
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\Entity
+ */
+class Article
+{
+    /**
+     * @ORM\Id
+     * @ORM\GeneratedValue
+     * @ORM\Column(type="integer")
+     */
+    private $id;
+
+    /**
+     * @Gedmo\Translatable
+     * @ORM\Column(length=128)
+     */
+    private $title;
+
+    /**
+     * @Gedmo\Translatable
+     * @ORM\Column(length=128)
+     */
+    private $titleTest;
+
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setTitle($title)
+    {
+        $this->title = $title;
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+
+    public function setTitleTest($titleTest)
+    {
+        $this->titleTest = $titleTest;
+    }
+
+    public function getTitleTest()
+    {
+        return $this->titleTest;
+    }
+}
+

+ 93 - 0
tests/Gedmo/Translatable/Issue/Issue138Test.php

@@ -0,0 +1,93 @@
+<?php
+
+namespace Gedmo\Translatable;
+
+use Doctrine\Common\EventManager;
+use Tool\BaseTestCaseORM;
+use Translatable\Fixture\Issue138\Article;
+use Gedmo\Translatable\Query\TreeWalker\TranslationWalker;
+use Doctrine\ORM\Query;
+
+/**
+ * These are tests for translatable behavior
+ *
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Translatable
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class Issue138Test extends BaseTestCaseORM
+{
+    const ARTICLE = 'Translatable\\Fixture\\Issue138\\Article';
+    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 TranslationListener();
+        $this->translatableListener->setTranslatableLocale('en');
+        $this->translatableListener->setDefaultLocale('en');
+        $evm->addEventSubscriber($this->translatableListener);
+
+        $this->getMockSqliteEntityManager($evm);
+    }
+
+    public function testIssue138()
+    {
+        $this->populate();
+        $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
+        $dql .= " WHERE a.title LIKE '%foo%'";
+        $q = $this->em->createQuery($dql);
+        $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
+
+        // array hydration
+        $this->translatableListener->setTranslatableLocale('en_us');
+        //die($q->getSQL());
+        $result = $q->getArrayResult();
+        $this->assertEquals(1, count($result));
+        $this->assertEquals('Food', $result[0]['title']);
+    }
+
+    protected function getUsedEntityFixtures()
+    {
+        return array(
+            self::ARTICLE,
+            self::TRANSLATION,
+
+        );
+    }
+
+    private function populate()
+    {
+        $repo = $this->em->getRepository(self::ARTICLE);
+
+        $food = new Article;
+        $food->setTitle('Food');
+        $food->setTitleTest('about food');
+
+        $citron = new Article;
+        $citron->setTitle('Citron');
+        $citron->setTitleTest('something citron');
+
+        $this->em->persist($food);
+        $this->em->persist($citron);
+        $this->em->flush();
+
+        $this->translatableListener->setTranslatableLocale('lt_lt');
+        $food->setTitle('Maistas');
+        $food->setTitleTest('apie maista');
+
+        $citron->setTitle('Citrina');
+        $citron->setTitleTest('kazkas citrina');
+
+        $this->em->persist($food);
+        $this->em->persist($citron);
+        $this->em->flush();
+        $this->em->clear();
+    }
+}

+ 0 - 7
tests/Gedmo/Translatable/TranslationQueryWalkerTest.php

@@ -43,13 +43,6 @@ class TranslationQueryWalkerTest extends BaseTestCaseORM
     public function testIssue135()
     {
         $this->populateIssue109();
-        $this->em
-            ->getConfiguration()
-            ->expects($this->any())
-            ->method('getCustomHydrationMode')
-            ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
-            ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'))
-        ;
         $query = $this->em->createQueryBuilder();
         $query->select('a')
             ->from(self::ARTICLE, 'a')