PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Gedmo/Translator/TranslatableTest.php

https://github.com/Atlantic18/DoctrineExtensions
PHP | 245 lines | 186 code | 42 blank | 17 comment | 0 complexity | 7f71ba45544bb3fee1a4a5a84794ff1e MD5 | raw file
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the Doctrine Behavioral Extensions package.
  5. * (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Gedmo\Tests\Translator;
  10. use Doctrine\Common\EventManager;
  11. use Doctrine\Persistence\Proxy;
  12. use Gedmo\Tests\Tool\BaseTestCaseORM;
  13. use Gedmo\Tests\Translator\Fixture\Person;
  14. use Gedmo\Tests\Translator\Fixture\PersonCustom;
  15. /**
  16. * These are tests for translatable behavior
  17. *
  18. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  19. */
  20. final class TranslatableTest extends BaseTestCaseORM
  21. {
  22. public const PERSON = Person::class;
  23. public const PERSON_CUSTOM_PROXY = PersonCustom::class;
  24. protected function setUp(): void
  25. {
  26. parent::setUp();
  27. $evm = new EventManager();
  28. $this->getDefaultMockSqliteEntityManager($evm);
  29. }
  30. public function testTranslatable(): void
  31. {
  32. $person = new Person();
  33. $person->setName('Jen');
  34. $person->translate('ru_RU')->setName('Женя');
  35. $person->setDescription('description');
  36. $person->translate('ru_RU')->setDescription('multilingual description');
  37. static::assertSame('Jen', $person->getName());
  38. static::assertSame('Женя', $person->translate('ru_RU')->getName());
  39. static::assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  40. static::assertSame('multilingual description', $person->getDescription());
  41. $this->em->persist($person);
  42. $this->em->flush();
  43. $this->em->clear();
  44. // retrieve record (translations would be fetched later - by demand)
  45. $person = $this->em->getRepository(self::PERSON)->findOneBy(['name' => 'Jen']);
  46. static::assertSame('Jen', $person->getName());
  47. static::assertSame('Женя', $person->translate('ru_RU')->getName());
  48. static::assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  49. static::assertSame('multilingual description', $person->getDescription());
  50. // retrieve record with all translations in one query
  51. $persons = $this->em->getRepository(self::PERSON)
  52. ->createQueryBuilder('p')
  53. ->select('p, t')
  54. ->join('p.translations', 't')
  55. ->getQuery()
  56. ->execute();
  57. $person = $persons[0];
  58. static::assertSame('Jen', $person->getName());
  59. static::assertSame('Женя', $person->translate('ru_RU')->getName());
  60. static::assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  61. static::assertSame('multilingual description', $person->getDescription());
  62. $person->translate('es_ES')->setName('Amigo');
  63. $this->em->flush();
  64. // retrieve record with all translations in one query
  65. $persons = $this->em->getRepository(self::PERSON)
  66. ->createQueryBuilder('p')
  67. ->select('p, t')
  68. ->join('p.translations', 't')
  69. ->getQuery()
  70. ->execute();
  71. $person = $persons[0];
  72. static::assertSame('Jen', $person->getName());
  73. static::assertSame('Женя', $person->translate('ru_RU')->getName());
  74. static::assertSame('Amigo', $person->translate('es_ES')->getName());
  75. static::assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  76. }
  77. public function testShouldTranslateRelation(): void
  78. {
  79. $person = new Person();
  80. $person->setName('Jen');
  81. $person->translate('ru')->setName('Женя');
  82. $person->setDescription('description');
  83. $person->translate('ru')->setDescription('multilingual description');
  84. $parent = new Person();
  85. $parent->setName('Jen');
  86. $parent->translate('ru')->setName('Женя starshai');
  87. $parent->translate('fr')->setName('zenia');
  88. $parent->setDescription('description');
  89. $parent->translate('ru')->setDescription('multilingual description');
  90. $person->setParent($parent);
  91. $this->em->persist($person);
  92. $this->em->persist($parent);
  93. $this->em->flush();
  94. $this->em->clear();
  95. $person = $this->em->getRepository(self::PERSON)->findOneBy(['name' => 'Jen']);
  96. static::assertSame('Женя', $person->translate('ru')->getName());
  97. $parent = $person->getParent();
  98. static::assertInstanceOf(Proxy::class, $parent);
  99. static::assertInstanceOf(Person::class, $parent);
  100. static::assertSame('Женя starshai', $parent->translate('ru')->getName());
  101. static::assertSame('zenia', $parent->translate('fr')->getName());
  102. }
  103. public function testShouldHandleDomainObjectProxy(): void
  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, ['id' => 1]);
  114. static::assertInstanceOf(Proxy::class, $personProxy);
  115. $name = $personProxy->translate('ru_RU')->getName();
  116. static::assertSame('Женя', $name);
  117. }
  118. public function testTranslatableProxyWithUpperCaseProperty(): void
  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, ['id' => 1]);
  131. static::assertInstanceOf(Proxy::class, $personProxy);
  132. $name = $personProxy->translate('ru_RU')->getName();
  133. static::assertSame('Женя', $name);
  134. $lastName = $personProxy->translate('ru_RU')->getLastName();
  135. static::assertSame('Абрамович', $lastName);
  136. }
  137. public function testTranslatableWithMagicProperties(): void
  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. static::assertSame('Jen', $person->name);
  144. static::assertSame('Jen', $person->translate()->name);
  145. static::assertSame('Женя', $person->translate('ru_RU')->name);
  146. static::assertSame('multilingual description', $person->translate('ru_RU')->description);
  147. static::assertSame('multilingual description', $person->description);
  148. }
  149. public function testTranslatableWithCustomProxy(): void
  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. static::assertSame('Jen', $person->getName());
  157. static::assertSame('Женя', $person->translate('ru_RU')->getName());
  158. static::assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  159. static::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)->findOneBy(['name' => 'Jen']);
  165. static::assertSame('Jen', $person->getName());
  166. static::assertSame('Женя', $person->translate('ru_RU')->getName());
  167. static::assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  168. static::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. static::assertSame('Jen', $person->getName());
  178. static::assertSame('Женя', $person->translate('ru_RU')->getName());
  179. static::assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  180. static::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. static::assertSame('Jen', $person->getName());
  192. static::assertSame('Женя', $person->translate('ru_RU')->getName());
  193. static::assertSame('Amigo', $person->translate('es_ES')->getName());
  194. static::assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  195. }
  196. protected function getUsedEntityFixtures(): array
  197. {
  198. return [
  199. self::PERSON, self::PERSON.'Translation',
  200. self::PERSON_CUSTOM_PROXY, self::PERSON_CUSTOM_PROXY.'Translation',
  201. ];
  202. }
  203. }