MixedValueTranslationTest.php 2.9KB

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