TranslatableTest.php 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. use Doctrine\ORM\Proxy\Proxy;
  8. /**
  9. * These are tests for translatable behavior
  10. *
  11. * @author Konstantin Kudryashov <ever.zet@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 TranslatableTest extends BaseTestCaseORM
  17. {
  18. const PERSON = 'Translator\\Fixture\\Person';
  19. const PERSON_CUSTOM_PROXY = 'Translator\\Fixture\\PersonCustom';
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $evm = new EventManager;
  24. $this->getMockSqliteEntityManager($evm);
  25. }
  26. public function testTranslatable()
  27. {
  28. $person = new Person();
  29. $person->setName('Jen');
  30. $person->translate('ru_RU')->setName('Женя');
  31. $person->setDescription('description');
  32. $person->translate('ru_RU')->setDescription('multilingual description');
  33. $this->assertSame('Jen', $person->getName());
  34. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  35. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  36. $this->assertSame('multilingual description', $person->getDescription());
  37. $this->em->persist($person);
  38. $this->em->flush();
  39. $this->em->clear();
  40. // retrieve record (translations would be fetched later - by demand)
  41. $person = $this->em->getRepository(self::PERSON)->findOneByName('Jen');
  42. $this->assertSame('Jen', $person->getName());
  43. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  44. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  45. $this->assertSame('multilingual description', $person->getDescription());
  46. // retrieve record with all translations in one query
  47. $persons = $this->em->getRepository(self::PERSON)
  48. ->createQueryBuilder('p')
  49. ->select('p, t')
  50. ->join('p.translations', 't')
  51. ->getQuery()
  52. ->execute();
  53. $person = $persons[0];
  54. $this->assertSame('Jen', $person->getName());
  55. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  56. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  57. $this->assertSame('multilingual description', $person->getDescription());
  58. $person->translate('es_ES')->setName('Amigo');
  59. $this->em->flush();
  60. // retrieve record with all translations in one query
  61. $persons = $this->em->getRepository(self::PERSON)
  62. ->createQueryBuilder('p')
  63. ->select('p, t')
  64. ->join('p.translations', 't')
  65. ->getQuery()
  66. ->execute();
  67. $person = $persons[0];
  68. $this->assertSame('Jen', $person->getName());
  69. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  70. $this->assertSame('Amigo', $person->translate('es_ES')->getName());
  71. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  72. }
  73. /**
  74. * @test
  75. */
  76. function shouldTranslateRelation()
  77. {
  78. $person = new Person();
  79. $person->setName('Jen');
  80. $person->translate('ru')->setName('Женя');
  81. $person->setDescription('description');
  82. $person->translate('ru')->setDescription('multilingual description');
  83. $parent = new Person();
  84. $parent->setName('Jen');
  85. $parent->translate('ru')->setName('Женя starshai');
  86. $parent->translate('fr')->setName('zenia');
  87. $parent->setDescription('description');
  88. $parent->translate('ru')->setDescription('multilingual description');
  89. $person->setParent($parent);
  90. $this->em->persist($person);
  91. $this->em->persist($parent);
  92. $this->em->flush();
  93. $this->em->clear();
  94. $person = $this->em->getRepository(self::PERSON)->findOneByName('Jen');
  95. $this->assertSame('Женя', $person->translate('ru')->getName());
  96. $parent = $person->getParent();
  97. $this->assertTrue($parent instanceof Proxy);
  98. $this->assertSame('Женя starshai', $parent->translate('ru')->getName());
  99. $this->assertSame('zenia', $parent->translate('fr')->getName());
  100. }
  101. /**
  102. * @test
  103. */
  104. function shouldHandleDomainObjectProxy()
  105. {
  106. $person = new Person();
  107. $person->setName('Jen');
  108. $person->translate('ru_RU')->setName('Женя');
  109. $person->setDescription('description');
  110. $person->translate('ru_RU')->setDescription('multilingual description');
  111. $this->em->persist($person);
  112. $this->em->flush();
  113. $this->em->clear();
  114. $personProxy = $this->em->getReference(self::PERSON, array('id' => 1));
  115. $this->assertTrue($personProxy instanceof Proxy);
  116. $name = $personProxy->translate('ru_RU')->getName();
  117. $this->assertSame('Женя', $name);
  118. }
  119. public function testTranslatableProxyWithUpperCaseProperty()
  120. {
  121. $person = new Person();
  122. $person->setName('Jen');
  123. $person->translate('ru_RU')->name = 'Женя';
  124. $person->setLastName('Abramowicz');
  125. $person->translate('ru_RU')->setLastName('Абрамович');
  126. $person->setDescription('description');
  127. $person->translate('ru_RU')->setDescription('multilingual description');
  128. $this->em->persist($person);
  129. $this->em->flush();
  130. $this->em->clear();
  131. $personProxy = $this->em->getReference(self::PERSON, array('id' => 1));
  132. $this->assertTrue($personProxy instanceof Proxy);
  133. $name = $personProxy->translate('ru_RU')->getName();
  134. $this->assertSame('Женя', $name);
  135. $lastName = $personProxy->translate('ru_RU')->getLastName();
  136. $this->assertSame('Абрамович', $lastName);
  137. }
  138. public function testTranslatableWithMagicProperties()
  139. {
  140. $person = new Person();
  141. $person->translate('en')->setName('Jen');
  142. $person->translate('ru_RU')->name = 'Женя';
  143. $person->translate('ru_RU')->description = 'multilingual description';
  144. $this->assertSame('Jen', $person->name);
  145. $this->assertSame('Jen', $person->translate()->name);
  146. $this->assertSame('Женя', $person->translate('ru_RU')->name);
  147. $this->assertSame('multilingual description', $person->translate('ru_RU')->description);
  148. $this->assertSame('multilingual description', $person->description);
  149. }
  150. public function testTranslatableWithCustomProxy()
  151. {
  152. $person = new PersonCustom();
  153. $person->setName('Jen');
  154. $person->translate('ru_RU')->setName('Женя');
  155. $person->setDescription('description');
  156. $person->translate('ru_RU')->setDescription('multilingual description');
  157. $this->assertSame('Jen', $person->getName());
  158. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  159. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  160. $this->assertSame('multilingual description', $person->getDescription());
  161. $this->em->persist($person);
  162. $this->em->flush();
  163. $this->em->clear();
  164. // retrieve record (translations would be fetched later - by demand)
  165. $person = $this->em->getRepository(self::PERSON_CUSTOM_PROXY)->findOneByName('Jen');
  166. $this->assertSame('Jen', $person->getName());
  167. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  168. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  169. $this->assertSame('multilingual description', $person->getDescription());
  170. // retrieve record with all translations in one query
  171. $persons = $this->em->getRepository(self::PERSON_CUSTOM_PROXY)
  172. ->createQueryBuilder('p')
  173. ->select('p, t')
  174. ->join('p.translations', 't')
  175. ->getQuery()
  176. ->execute();
  177. $person = $persons[0];
  178. $this->assertSame('Jen', $person->getName());
  179. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  180. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  181. $this->assertSame('multilingual description', $person->getDescription());
  182. $person->translate('es_ES')->setName('Amigo');
  183. $this->em->flush();
  184. // retrieve record with all translations in one query
  185. $persons = $this->em->getRepository(self::PERSON_CUSTOM_PROXY)
  186. ->createQueryBuilder('p')
  187. ->select('p, t')
  188. ->join('p.translations', 't')
  189. ->getQuery()
  190. ->execute();
  191. $person = $persons[0];
  192. $this->assertSame('Jen', $person->getName());
  193. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  194. $this->assertSame('Amigo', $person->translate('es_ES')->getName());
  195. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  196. }
  197. protected function getUsedEntityFixtures()
  198. {
  199. return array(
  200. self::PERSON, self::PERSON.'Translation',
  201. self::PERSON_CUSTOM_PROXY, self::PERSON_CUSTOM_PROXY.'Translation',
  202. );
  203. }
  204. }