-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix missing locale annotation for Gedmo trait
- Loading branch information
Dariusz Markowicz
committed
Aug 21, 2016
1 parent
366400c
commit 93d7b43
Showing
6 changed files
with
378 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Gedmo\Mapping\Annotation as Gedmo; | ||
use Sonata\TranslationBundle\Traits\Gedmo\PersonalTranslatableTrait; | ||
|
||
/** | ||
* @Gedmo\TranslationEntity(class="Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\ArticlePersonalTranslation") | ||
* @ORM\Table(name="article") | ||
* @ORM\Entity | ||
*/ | ||
class ArticlePersonalTranslatable | ||
{ | ||
use PersonalTranslatableTrait; | ||
|
||
/** | ||
* @ORM\Id | ||
* @ORM\GeneratedValue | ||
* @ORM\Column(type="integer") | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @Gedmo\Translatable | ||
* @ORM\Column(length=128) | ||
*/ | ||
private $title; | ||
|
||
/** | ||
* @var ArrayCollection | ||
* | ||
* @ORM\OneToMany( | ||
* targetEntity="Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\ArticlePersonalTranslation", | ||
* mappedBy="object", | ||
* cascade={"persist", "remove"} | ||
* ) | ||
*/ | ||
protected $translations; | ||
|
||
public function __construct() | ||
{ | ||
$this->translations = new ArrayCollection(); | ||
} | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setTitle($title) | ||
{ | ||
$this->title = $title; | ||
} | ||
|
||
public function getTitle() | ||
{ | ||
return $this->title; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use Sonata\TranslationBundle\Model\Gedmo\AbstractPersonalTranslation; | ||
|
||
/** | ||
* @ORM\Table(name="article_translation") | ||
* @ORM\Entity | ||
*/ | ||
class ArticlePersonalTranslation extends AbstractPersonalTranslation | ||
{ | ||
/** | ||
* @ORM\ManyToOne(targetEntity="Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\ArticlePersonalTranslatable", inversedBy="translations") | ||
* @ORM\JoinColumn(name="object_id", referencedColumnName="id", onDelete="CASCADE") | ||
*/ | ||
protected $object; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* 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, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* 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 <nbastien.pro@gmail.com> | ||
*/ | ||
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; | ||
} | ||
} |