From 93d7b43d6a59fb83d160752333eb5a8c13890e54 Mon Sep 17 00:00:00 2001 From: Dariusz Markowicz Date: Sun, 14 Aug 2016 00:32:30 +0200 Subject: [PATCH] Fix missing locale annotation for Gedmo trait --- .../ORM/ArticlePersonalTranslatable.php | 62 +++++++ .../Traits/ORM/ArticlePersonalTranslation.php | 19 ++ Tests/Tool/BaseTestCaseORM.php | 165 ++++++++++++++++++ Tests/Traits/GedmoORMTest.php | 87 +++++++++ Traits/Gedmo/PersonalTranslatableTrait.php | 1 - Traits/Gedmo/TranslatableTrait.php | 45 +++++ 6 files changed, 378 insertions(+), 1 deletion(-) create mode 100644 Tests/Fixtures/Traits/ORM/ArticlePersonalTranslatable.php create mode 100644 Tests/Fixtures/Traits/ORM/ArticlePersonalTranslation.php create mode 100644 Tests/Tool/BaseTestCaseORM.php create mode 100644 Tests/Traits/GedmoORMTest.php create mode 100644 Traits/Gedmo/TranslatableTrait.php diff --git a/Tests/Fixtures/Traits/ORM/ArticlePersonalTranslatable.php b/Tests/Fixtures/Traits/ORM/ArticlePersonalTranslatable.php new file mode 100644 index 00000000..e48c396c --- /dev/null +++ b/Tests/Fixtures/Traits/ORM/ArticlePersonalTranslatable.php @@ -0,0 +1,62 @@ +translations = new ArrayCollection(); + } + + public function getId() + { + return $this->id; + } + + public function setTitle($title) + { + $this->title = $title; + } + + public function getTitle() + { + return $this->title; + } +} diff --git a/Tests/Fixtures/Traits/ORM/ArticlePersonalTranslation.php b/Tests/Fixtures/Traits/ORM/ArticlePersonalTranslation.php new file mode 100644 index 00000000..667b9d99 --- /dev/null +++ b/Tests/Fixtures/Traits/ORM/ArticlePersonalTranslation.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\TranslationBundle\Tests\Tool; + +use Doctrine\Common\Annotations\AnnotationReader; +use Doctrine\Common\EventManager; +use Doctrine\ORM\Configuration; +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\Mapping\Driver\AnnotationDriver; +use Doctrine\ORM\Tools\SchemaTool; +use Gedmo\Translatable\TranslatableListener; + +abstract class BaseTestCaseORM extends \PHPUnit_Framework_TestCase +{ + /** + * @var EntityManager + */ + protected $em; + + /** + * {@inheritdoc} + */ + protected function setUp() + { + } + + /** + * EntityManager mock object together with + * annotation mapping driver and pdo_sqlite + * database in memory. + * + * @param EventManager $evm + * + * @return EntityManager + */ + protected function getMockSqliteEntityManager(EventManager $evm = null, Configuration $config = null) + { + $conn = array( + 'driver' => 'pdo_sqlite', + 'memory' => true, + ); + + $config = $config ?: $this->getMockAnnotatedConfig(); + $em = EntityManager::create($conn, $config, $evm ?: $this->getEventManager()); + + $schema = array_map(function ($class) use ($em) { + return $em->getClassMetadata($class); + }, (array) $this->getUsedEntityFixtures()); + + $schemaTool = new SchemaTool($em); + $schemaTool->dropSchema(array()); + $schemaTool->createSchema($schema); + + return $this->em = $em; + } + + /** + * EntityManager mock object together with + * annotation mapping driver and custom + * connection. + * + * @param array $conn + * @param EventManager $evm + * + * @return EntityManager + */ + protected function getMockCustomEntityManager(array $conn, EventManager $evm = null) + { + $config = $this->getMockAnnotatedConfig(); + $em = EntityManager::create($conn, $config, $evm ?: $this->getEventManager()); + + $schema = array_map(function ($class) use ($em) { + return $em->getClassMetadata($class); + }, (array) $this->getUsedEntityFixtures()); + + $schemaTool = new SchemaTool($em); + $schemaTool->dropSchema(array()); + $schemaTool->createSchema($schema); + + return $this->em = $em; + } + + /** + * EntityManager mock object with + * annotation mapping driver. + * + * @param EventManager $evm + * + * @return EntityManager + */ + protected function getMockMappedEntityManager(EventManager $evm = null) + { + $driver = $this->getMockBuilder('Doctrine\DBAL\Driver')->getMock(); + $driver->expects($this->once()) + ->method('getDatabasePlatform') + ->will($this->returnValue($this->getMockBuilder('Doctrine\DBAL\Platforms\MySqlPlatform')->getMock())); + + $conn = $this->getMockBuilder('Doctrine\DBAL\Connection') + ->setConstructorArgs(array(), $driver) + ->getMock(); + + $conn->expects($this->once()) + ->method('getEventManager') + ->will($this->returnValue($evm ?: $this->getEventManager())); + + $config = $this->getMockAnnotatedConfig(); + $this->em = EntityManager::create($conn, $config); + + return $this->em; + } + + /** + * Creates default mapping driver. + * + * @return \Doctrine\ORM\Mapping\Driver\AnnotationDriver + */ + protected function getMetadataDriverImplementation() + { + return new AnnotationDriver(new AnnotationReader()); + } + + /** + * Get a list of used fixture classes. + * + * @return array + */ + abstract protected function getUsedEntityFixtures(); + + /** + * Get annotation mapping configuration. + * + * @return \Doctrine\ORM\Configuration + */ + protected function getMockAnnotatedConfig() + { + $config = new Configuration(); + $config->setProxyDir(sys_get_temp_dir().'/sonata-translation-bundle'); + $config->setProxyNamespace('Proxy'); + $config->setMetadataDriverImpl($this->getMetadataDriverImplementation()); + + return $config; + } + + /** + * Build event manager. + * + * @return EventManager + */ + private function getEventManager() + { + $evm = new EventManager(); + $evm->addEventSubscriber(new TranslatableListener()); + + return $evm; + } +} diff --git a/Tests/Traits/GedmoORMTest.php b/Tests/Traits/GedmoORMTest.php new file mode 100644 index 00000000..57d2ab6c --- /dev/null +++ b/Tests/Traits/GedmoORMTest.php @@ -0,0 +1,87 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\TranslationBundle\Tests\Traits; + +use Doctrine\Common\EventManager; +use Gedmo\Translatable\TranslatableListener; +use Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\ArticlePersonalTranslatable; +use Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\ArticlePersonalTranslation; +use Sonata\TranslationBundle\Tests\Tool\BaseTestCaseORM; + +class GedmoORMTest extends BaseTestCaseORM +{ + const ARTICLE = 'Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\ArticlePersonalTranslatable'; + const TRANSLATION = 'Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\ArticlePersonalTranslation'; + + /** @var TranslatableListener */ + private $translatableListener; + + protected function setUp() + { + parent::setUp(); + + if (!class_exists('Doctrine\\ORM\\Version')) { + $this->markTestSkipped('Doctrine ORM is not available.'); + } + + $evm = new EventManager(); + $this->translatableListener = new TranslatableListener(); + $this->translatableListener->setTranslatableLocale('en'); + $this->translatableListener->setDefaultLocale('en'); + $evm->addEventSubscriber($this->translatableListener); + + $this->getMockSqliteEntityManager($evm); + } + + public function testPersonalTranslatableEntity() + { + $article = new ArticlePersonalTranslatable(); + $article->setTitle('en'); + + $this->em->persist($article); + $this->em->flush(); + + $this->translatableListener->setTranslatableLocale('de'); + $article->setTitle('de'); + + $ltTranslation = new ArticlePersonalTranslation(); + $ltTranslation + ->setField('title') + ->setContent('lt') + ->setObject($article) + ->setLocale('lt') + ; + $this->em->persist($ltTranslation); + $this->em->persist($article); + $this->em->flush(); + + // Tests if Gedmo\Locale annotation exists + $article->setLocale('pl'); + $article->setTitle('pl'); + $this->em->persist($article); + $this->em->flush(); + + $this->em->clear(); + + $article = $this->em->find(self::ARTICLE, array('id' => 1)); + $translations = $article->getTranslations(); + $this->assertCount(3, $translations); + } + + protected function getUsedEntityFixtures() + { + return array( + self::ARTICLE, + self::TRANSLATION, + ); + } +} diff --git a/Traits/Gedmo/PersonalTranslatableTrait.php b/Traits/Gedmo/PersonalTranslatableTrait.php index c38db6fb..1692049b 100644 --- a/Traits/Gedmo/PersonalTranslatableTrait.php +++ b/Traits/Gedmo/PersonalTranslatableTrait.php @@ -12,7 +12,6 @@ namespace Sonata\TranslationBundle\Traits\Gedmo; use Sonata\TranslationBundle\Model\Gedmo\AbstractPersonalTranslation; -use Sonata\TranslationBundle\Traits\TranslatableTrait; /** * If you don't want to use trait, you can extend AbstractPersonalTranslatable instead. diff --git a/Traits/Gedmo/TranslatableTrait.php b/Traits/Gedmo/TranslatableTrait.php new file mode 100644 index 00000000..9646e78d --- /dev/null +++ b/Traits/Gedmo/TranslatableTrait.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\TranslationBundle\Traits\Gedmo; + +/** + * If you don't want to use trait, you can extend AbstractTranslatable instead. + * + * @author Nicolas Bastien + */ +trait TranslatableTrait +{ + /** + * @Gedmo\Locale + * Used locale to override Translation listener`s locale + * this is not a mapped field of entity metadata, just a simple property + * + * @var string + */ + protected $locale; + + /** + * @param string $locale + */ + public function setLocale($locale) + { + $this->locale = $locale; + } + + /** + * @return string + */ + public function getLocale() + { + return $this->locale; + } +}