TranslatableTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Gedmo\Translator;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Translator\Fixture\Person;
  6. use Translator\Fixture\PersonCustom;
  7. /**
  8. * These are tests for translatable behavior
  9. *
  10. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  11. * @package Gedmo.Translatable
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class TranslatableTest extends BaseTestCaseORM
  16. {
  17. const PERSON = 'Translator\\Fixture\\Person';
  18. const PERSON_CUSTOM_PROXY = 'Translator\\Fixture\\PersonCustom';
  19. protected function setUp()
  20. {
  21. parent::setUp();
  22. $evm = new EventManager;
  23. $this->getMockSqliteEntityManager($evm);
  24. }
  25. public function testTranslatable()
  26. {
  27. $person = new Person();
  28. $person->setName('Jen');
  29. $person->translate('ru_RU')->setName('Женя');
  30. $person->setDescription('description');
  31. $person->translate('ru_RU')->setDescription('multilingual description');
  32. $this->assertSame('Jen', $person->getName());
  33. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  34. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  35. $this->assertSame('multilingual description', $person->getDescription());
  36. $this->em->persist($person);
  37. $this->em->flush();
  38. $this->em->clear();
  39. // retrieve record (translations would be fetched later - by demand)
  40. $person = $this->em->getRepository(self::PERSON)->findOneByName('Jen');
  41. $this->assertSame('Jen', $person->getName());
  42. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  43. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  44. $this->assertSame('multilingual description', $person->getDescription());
  45. // retrieve record with all translations in one query
  46. $persons = $this->em->getRepository(self::PERSON)
  47. ->createQueryBuilder('p')
  48. ->select('p, t')
  49. ->join('p.translations', 't')
  50. ->getQuery()
  51. ->execute();
  52. $person = $persons[0];
  53. $this->assertSame('Jen', $person->getName());
  54. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  55. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  56. $this->assertSame('multilingual description', $person->getDescription());
  57. $person->translate('es_ES')->setName('Amigo');
  58. $this->em->flush();
  59. // retrieve record with all translations in one query
  60. $persons = $this->em->getRepository(self::PERSON)
  61. ->createQueryBuilder('p')
  62. ->select('p, t')
  63. ->join('p.translations', 't')
  64. ->getQuery()
  65. ->execute();
  66. $person = $persons[0];
  67. $this->assertSame('Jen', $person->getName());
  68. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  69. $this->assertSame('Amigo', $person->translate('es_ES')->getName());
  70. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  71. }
  72. public function testTranslatableWithMagicProperties()
  73. {
  74. $person = new Person();
  75. $person->setName('Jen');
  76. $person->translate('ru_RU')->name = 'Женя';
  77. $person->translate('ru_RU')->description = 'multilingual description';
  78. $this->assertSame('Jen', $person->name);
  79. $this->assertSame('Jen', $person->translate()->name);
  80. $this->assertSame('Женя', $person->translate('ru_RU')->name);
  81. $this->assertSame('multilingual description', $person->translate('ru_RU')->description);
  82. $this->assertSame('multilingual description', $person->description);
  83. }
  84. public function testTranslatableWithCustomProxy()
  85. {
  86. $person = new PersonCustom();
  87. $person->setName('Jen');
  88. $person->translate('ru_RU')->setName('Женя');
  89. $person->setDescription('description');
  90. $person->translate('ru_RU')->setDescription('multilingual description');
  91. $this->assertSame('Jen', $person->getName());
  92. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  93. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  94. $this->assertSame('multilingual description', $person->getDescription());
  95. $this->em->persist($person);
  96. $this->em->flush();
  97. $this->em->clear();
  98. // retrieve record (translations would be fetched later - by demand)
  99. $person = $this->em->getRepository(self::PERSON_CUSTOM_PROXY)->findOneByName('Jen');
  100. $this->assertSame('Jen', $person->getName());
  101. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  102. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  103. $this->assertSame('multilingual description', $person->getDescription());
  104. // retrieve record with all translations in one query
  105. $persons = $this->em->getRepository(self::PERSON_CUSTOM_PROXY)
  106. ->createQueryBuilder('p')
  107. ->select('p, t')
  108. ->join('p.translations', 't')
  109. ->getQuery()
  110. ->execute();
  111. $person = $persons[0];
  112. $this->assertSame('Jen', $person->getName());
  113. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  114. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  115. $this->assertSame('multilingual description', $person->getDescription());
  116. $person->translate('es_ES')->setName('Amigo');
  117. $this->em->flush();
  118. // retrieve record with all translations in one query
  119. $persons = $this->em->getRepository(self::PERSON_CUSTOM_PROXY)
  120. ->createQueryBuilder('p')
  121. ->select('p, t')
  122. ->join('p.translations', 't')
  123. ->getQuery()
  124. ->execute();
  125. $person = $persons[0];
  126. $this->assertSame('Jen', $person->getName());
  127. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  128. $this->assertSame('Amigo', $person->translate('es_ES')->getName());
  129. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  130. }
  131. protected function getUsedEntityFixtures()
  132. {
  133. return array(
  134. self::PERSON, self::PERSON.'Translation',
  135. self::PERSON_CUSTOM_PROXY, self::PERSON_CUSTOM_PROXY.'Translation',
  136. );
  137. }
  138. }