MixedValueTranslationTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\DBAL\Types\Type;
  6. use Doctrine\Common\Util\Debug,
  7. Translatable\Fixture\MixedValue;
  8. /**
  9. * These are tests for translatable behavior
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class MixedValueTranslationTest extends BaseTestCaseORM
  16. {
  17. const MIXED = 'Translatable\\Fixture\\MixedValue';
  18. const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
  19. private $translatableListener;
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. if (!Type::hasType('custom')) {
  24. Type::addType('custom', 'Translatable\Fixture\Type\Custom');
  25. }
  26. $evm = new EventManager;
  27. $this->translatableListener = new TranslatableListener();
  28. $this->translatableListener->setTranslatableLocale('en_us');
  29. $this->translatableListener->setDefaultLocale('en_us');
  30. $evm->addEventSubscriber($this->translatableListener);
  31. $this->getMockSqliteEntityManager($evm);
  32. $this->populate();
  33. }
  34. public function testFixtureGeneratedTranslations()
  35. {
  36. $repo = $this->em->getRepository(self::MIXED);
  37. $mixed = $repo->findOneById(1);
  38. $this->assertTrue($mixed->getDate() instanceof \DateTime);
  39. $this->assertTrue($mixed->getCust() instanceof \stdClass);
  40. $this->assertEquals('en', $mixed->getCust()->test);
  41. }
  42. public function testOtherTranslation()
  43. {
  44. $repo = $this->em->getRepository(self::MIXED);
  45. $mixed = $repo->findOneById(1);
  46. $this->translatableListener->setTranslatableLocale('de_de');
  47. $mixed->setDate(new \DateTime('2000-00-00 00:00:00'));
  48. $cust = new \stdClass();
  49. $cust->test = 'de';
  50. $mixed->setCust($cust);
  51. $this->em->persist($mixed);
  52. $this->em->flush();
  53. $this->em->clear();
  54. $mixed = $repo->findOneById(1);
  55. $transRepo = $this->em->getRepository(self::TRANSLATION);
  56. $translations = $transRepo->findTranslations($mixed);
  57. $this->assertCount(1, $translations);
  58. $this->assertArrayHasKey('de_de', $translations);
  59. $cust = unserialize($translations['de_de']['cust']);
  60. $this->assertTrue($cust instanceof \stdClass);
  61. $this->assertEquals('de', $cust->test);
  62. }
  63. protected function getUsedEntityFixtures()
  64. {
  65. return array(
  66. self::MIXED,
  67. self::TRANSLATION
  68. );
  69. }
  70. private function populate()
  71. {
  72. $mixedEn = new MixedValue;
  73. $mixedEn->setDate(new \DateTime());
  74. $cust = new \stdClass();
  75. $cust->test = 'en';
  76. $mixedEn->setCust($cust);
  77. $this->em->persist($mixedEn);
  78. $this->em->flush();
  79. $this->em->clear();
  80. }
  81. }