TranslatableTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. * @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. /**
  73. * @test
  74. */
  75. function shouldTranslateRelation()
  76. {
  77. $person = new Person();
  78. $person->setName('Jen');
  79. $person->translate('ru')->setName('Женя');
  80. $person->setDescription('description');
  81. $person->translate('ru')->setDescription('multilingual description');
  82. $parent = new Person();
  83. $parent->setName('Jen');
  84. $parent->translate('ru')->setName('Женя starshai');
  85. $parent->translate('fr')->setName('zenia');
  86. $parent->setDescription('description');
  87. $parent->translate('ru')->setDescription('multilingual description');
  88. $person->setParent($parent);
  89. $this->em->persist($person);
  90. $this->em->persist($parent);
  91. $this->em->flush();
  92. $this->em->clear();
  93. $person = $this->em->getRepository(self::PERSON)->findOneByName('Jen');
  94. $this->assertSame('Женя', $person->translate('ru')->getName());
  95. $parent = $person->getParent();
  96. $this->assertTrue($parent instanceof Proxy);
  97. $this->assertSame('Женя starshai', $parent->translate('ru')->getName());
  98. $this->assertSame('zenia', $parent->translate('fr')->getName());
  99. }
  100. /**
  101. * @test
  102. */
  103. function shouldHandleDomainObjectProxy()
  104. {
  105. $person = new Person();
  106. $person->setName('Jen');
  107. $person->translate('ru_RU')->setName('Женя');
  108. $person->setDescription('description');
  109. $person->translate('ru_RU')->setDescription('multilingual description');
  110. $this->em->persist($person);
  111. $this->em->flush();
  112. $this->em->clear();
  113. $personProxy = $this->em->getReference(self::PERSON, array('id' => 1));
  114. $this->assertTrue($personProxy instanceof Proxy);
  115. $name = $personProxy->translate('ru_RU')->getName();
  116. $this->assertSame('Женя', $name);
  117. }
  118. public function testTranslatableProxyWithUpperCaseProperty()
  119. {
  120. $person = new Person();
  121. $person->setName('Jen');
  122. $person->translate('ru_RU')->name = 'Женя';
  123. $person->setLastName('Abramowicz');
  124. $person->translate('ru_RU')->setLastName('Абрамович');
  125. $person->setDescription('description');
  126. $person->translate('ru_RU')->setDescription('multilingual description');
  127. $this->em->persist($person);
  128. $this->em->flush();
  129. $this->em->clear();
  130. $personProxy = $this->em->getReference(self::PERSON, array('id' => 1));
  131. $this->assertTrue($personProxy instanceof Proxy);
  132. $name = $personProxy->translate('ru_RU')->getName();
  133. $this->assertSame('Женя', $name);
  134. $lastName = $personProxy->translate('ru_RU')->getLastName();
  135. $this->assertSame('Абрамович', $lastName);
  136. }
  137. public function testTranslatableWithMagicProperties()
  138. {
  139. $person = new Person();
  140. $person->translate('en')->setName('Jen');
  141. $person->translate('ru_RU')->name = 'Женя';
  142. $person->translate('ru_RU')->description = 'multilingual description';
  143. $this->assertSame('Jen', $person->name);
  144. $this->assertSame('Jen', $person->translate()->name);
  145. $this->assertSame('Женя', $person->translate('ru_RU')->name);
  146. $this->assertSame('multilingual description', $person->translate('ru_RU')->description);
  147. $this->assertSame('multilingual description', $person->description);
  148. }
  149. public function testTranslatableWithCustomProxy()
  150. {
  151. $person = new PersonCustom();
  152. $person->setName('Jen');
  153. $person->translate('ru_RU')->setName('Женя');
  154. $person->setDescription('description');
  155. $person->translate('ru_RU')->setDescription('multilingual description');
  156. $this->assertSame('Jen', $person->getName());
  157. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  158. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  159. $this->assertSame('multilingual description', $person->getDescription());
  160. $this->em->persist($person);
  161. $this->em->flush();
  162. $this->em->clear();
  163. // retrieve record (translations would be fetched later - by demand)
  164. $person = $this->em->getRepository(self::PERSON_CUSTOM_PROXY)->findOneByName('Jen');
  165. $this->assertSame('Jen', $person->getName());
  166. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  167. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  168. $this->assertSame('multilingual description', $person->getDescription());
  169. // retrieve record with all translations in one query
  170. $persons = $this->em->getRepository(self::PERSON_CUSTOM_PROXY)
  171. ->createQueryBuilder('p')
  172. ->select('p, t')
  173. ->join('p.translations', 't')
  174. ->getQuery()
  175. ->execute();
  176. $person = $persons[0];
  177. $this->assertSame('Jen', $person->getName());
  178. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  179. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  180. $this->assertSame('multilingual description', $person->getDescription());
  181. $person->translate('es_ES')->setName('Amigo');
  182. $this->em->flush();
  183. // retrieve record with all translations in one query
  184. $persons = $this->em->getRepository(self::PERSON_CUSTOM_PROXY)
  185. ->createQueryBuilder('p')
  186. ->select('p, t')
  187. ->join('p.translations', 't')
  188. ->getQuery()
  189. ->execute();
  190. $person = $persons[0];
  191. $this->assertSame('Jen', $person->getName());
  192. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  193. $this->assertSame('Amigo', $person->translate('es_ES')->getName());
  194. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  195. }
  196. protected function getUsedEntityFixtures()
  197. {
  198. return array(
  199. self::PERSON, self::PERSON.'Translation',
  200. self::PERSON_CUSTOM_PROXY, self::PERSON_CUSTOM_PROXY.'Translation',
  201. );
  202. }
  203. }