Bladeren bron

added tests for translator

everzet 13 jaren geleden
bovenliggende
commit
d00270469f

+ 107 - 0
tests/Gedmo/Translator/Fixture/Person.php

@@ -0,0 +1,107 @@
+<?php
+
+namespace Translator\Fixture;
+
+use Doctrine\ORM\Mapping as ORM;
+
+use Gedmo\Translator\ObjectTranslator;
+use Doctrine\Common\Collections\ArrayCollection;
+
+/**
+ * @ORM\Entity
+ * @ORM\HasLifecycleCallbacks
+ */
+class Person
+{
+    /**
+     * @ORM\Id
+     * @ORM\GeneratedValue
+     * @ORM\Column(type="integer")
+     */
+    private $id;
+
+    /**
+     * @ORM\Column(name="name", type="string", length=128)
+     */
+    private $name;
+
+    /**
+     * @ORM\Column(name="desc", type="string", length=128)
+     */
+    private $description;
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setName($name)
+    {
+        $this->name = $name;
+    }
+
+    public function getName()
+    {
+        return $this->name;
+    }
+
+    public function setDescription($description)
+    {
+        $this->description = $description;
+    }
+
+    public function getDescription()
+    {
+        return $this->description;
+    }
+
+
+
+
+    //
+    // TRANSLATIONS DEFINITION:
+    //
+
+
+
+
+    /**
+     * @ORM\OneToMany(targetEntity="PersonTranslation", mappedBy="translatable", cascade={"persist"})
+     */
+    private $translations;
+    private $translator;
+
+    public function __construct()
+    {
+        $this->translations = new ArrayCollection();
+
+        $this->initializeTranslator();
+    }
+
+    /** @ORM\PrePersist */
+    public function translateEntityToDefaultLocale()
+    {
+        $this->translator->translate();
+    }
+
+    /** @ORM\PostLoad */
+    public function initializeTranslator()
+    {
+        if (null === $this->translator) {
+            $this->translator = new ObjectTranslator($this,
+            /* List of translatable properties:  */ array('name'),
+            /* Translation entity class:         */ 'Translator\Fixture\PersonTranslation',
+            /* Translations collection property: */ $this->translations
+            );
+
+            return;
+        }
+
+        $this->translateEntityToDefaultLocale();
+    }
+
+    public function translate($locale = null)
+    {
+        return $this->translator->translate($locale);
+    }
+}

+ 25 - 0
tests/Gedmo/Translator/Fixture/PersonTranslation.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace Translator\Fixture;
+
+use Doctrine\ORM\Mapping as ORM;
+use Gedmo\Translator\Entity\Translation;
+
+/**
+ * @ORM\Table(
+ *         indexes={@ORM\index(name="translations_lookup_idx", columns={
+ *             "locale", "translatable_id"
+ *         })},
+ *         uniqueConstraints={@ORM\UniqueConstraint(name="lookup_unique_idx", columns={
+ *             "locale", "translatable_id", "property"
+ *         })}
+ * )
+ * @ORM\Entity
+ */
+class PersonTranslation extends Translation
+{
+    /**
+     * @ORM\ManyToOne(targetEntity="Person", inversedBy="translations")
+     */
+    protected $translatable;
+}

+ 99 - 0
tests/Gedmo/Translator/TranslatableTest.php

@@ -0,0 +1,99 @@
+<?php
+
+namespace Gedmo\Translator;
+
+use Doctrine\Common\EventManager;
+use Tool\BaseTestCaseORM;
+use Translator\Fixture\Person;
+
+/**
+ * These are tests for translatable behavior
+ *
+ * @author Konstantin Kudryashov <ever.zet@gmail.com>
+ * @package Gedmo.Translatable
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class TranslatableTest extends BaseTestCaseORM
+{
+    const PERSON = 'Translator\\Fixture\\Person';
+
+    protected function setUp()
+    {
+        parent::setUp();
+
+        $evm = new EventManager;
+        $this->getMockSqliteEntityManager($evm);
+    }
+
+    public function testTranslatable()
+    {
+        $person = new Person();
+        $person->translate()->setName('Jen');
+        $person->translate('ru_RU')->setName('Женя');
+        $person->setDescription('description');
+        $person->translate('ru_RU')->setDescription('multilingual description');
+
+        $this->assertSame('Jen', $person->translate()->getName());
+        $this->assertSame('Женя', $person->translate('ru_RU')->getName());
+        $this->assertSame('multilingual description', $person->getDescription());
+        $this->assertSame('multilingual description', $person->translate()->getDescription());
+        $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
+
+        $this->em->persist($person);
+        $this->em->flush();
+        $this->em->clear();
+
+        // retrieve record (translations would be fetched later - by demand)
+        $person = $this->em->getRepository(self::PERSON)->findOneByName('Jen');
+
+        $this->assertSame('Jen', $person->getName());
+        $this->assertSame('Jen', $person->translate()->getName());
+        $this->assertSame('Женя', $person->translate('ru_RU')->getName());
+        $this->assertSame('multilingual description', $person->getDescription());
+        $this->assertSame('multilingual description', $person->translate()->getDescription());
+        $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
+
+        // retrieve record with all translations in one query
+        $persons = $this->em->getRepository(self::PERSON)
+            ->createQueryBuilder('p')
+            ->select('p, t')
+            ->join('p.translations', 't')
+            ->getQuery()
+            ->execute();
+        $person = $persons[0];
+
+        $this->assertSame('Jen', $person->getName());
+        $this->assertSame('Jen', $person->translate()->getName());
+        $this->assertSame('Женя', $person->translate('ru_RU')->getName());
+        $this->assertSame('multilingual description', $person->getDescription());
+        $this->assertSame('multilingual description', $person->translate()->getDescription());
+        $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
+
+        $person->translate('es_ES')->setName('Amigo');
+
+        $this->em->flush();
+
+        // retrieve record with all translations in one query
+        $persons = $this->em->getRepository(self::PERSON)
+            ->createQueryBuilder('p')
+            ->select('p, t')
+            ->join('p.translations', 't')
+            ->getQuery()
+            ->execute();
+        $person = $persons[0];
+
+        $this->assertSame('Jen', $person->getName());
+        $this->assertSame('Jen', $person->translate()->getName());
+        $this->assertSame('Женя', $person->translate('ru_RU')->getName());
+        $this->assertSame('Amigo', $person->translate('es_ES')->getName());
+        $this->assertSame('multilingual description', $person->getDescription());
+        $this->assertSame('multilingual description', $person->translate()->getDescription());
+        $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
+    }
+
+    protected function getUsedEntityFixtures()
+    {
+        return array(self::PERSON, self::PERSON.'Translation');
+    }
+}

+ 1 - 0
tests/bootstrap.php

@@ -40,6 +40,7 @@ $loader->registerNamespaces(array(
     'Gedmo'                      => __DIR__.'/../lib',
     'Tool'                       => __DIR__.'/Gedmo',
     // fixture namespaces
+    'Translator\\Fixture'        => __DIR__.'/Gedmo',
     'Translatable\\Fixture'      => __DIR__.'/Gedmo',
     'Timestampable\\Fixture'     => __DIR__.'/Gedmo',
     'Tree\\Fixture'              => __DIR__.'/Gedmo',