TranslatableTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Gedmo\Translator;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Translator\Fixture\Person;
  6. /**
  7. * These are tests for translatable behavior
  8. *
  9. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  10. * @package Gedmo.Translatable
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class TranslatableTest extends BaseTestCaseORM
  15. {
  16. const PERSON = 'Translator\\Fixture\\Person';
  17. protected function setUp()
  18. {
  19. parent::setUp();
  20. $evm = new EventManager;
  21. $this->getMockSqliteEntityManager($evm);
  22. }
  23. public function testTranslatable()
  24. {
  25. $person = new Person();
  26. $person->translate()->setName('Jen');
  27. $person->translate('ru_RU')->setName('Женя');
  28. $person->setDescription('description');
  29. $person->translate('ru_RU')->setDescription('multilingual description');
  30. $this->assertSame('Jen', $person->translate()->getName());
  31. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  32. $this->assertSame('multilingual description', $person->getDescription());
  33. $this->assertSame('multilingual description', $person->translate()->getDescription());
  34. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  35. $this->em->persist($person);
  36. $this->em->flush();
  37. $this->em->clear();
  38. // retrieve record (translations would be fetched later - by demand)
  39. $person = $this->em->getRepository(self::PERSON)->findOneByName('Jen');
  40. $this->assertSame('Jen', $person->getName());
  41. $this->assertSame('Jen', $person->translate()->getName());
  42. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  43. $this->assertSame('multilingual description', $person->getDescription());
  44. $this->assertSame('multilingual description', $person->translate()->getDescription());
  45. $this->assertSame('multilingual description', $person->translate('ru_RU')->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('Jen', $person->translate()->getName());
  56. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  57. $this->assertSame('multilingual description', $person->getDescription());
  58. $this->assertSame('multilingual description', $person->translate()->getDescription());
  59. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  60. $person->translate('es_ES')->setName('Amigo');
  61. $this->em->flush();
  62. // retrieve record with all translations in one query
  63. $persons = $this->em->getRepository(self::PERSON)
  64. ->createQueryBuilder('p')
  65. ->select('p, t')
  66. ->join('p.translations', 't')
  67. ->getQuery()
  68. ->execute();
  69. $person = $persons[0];
  70. $this->assertSame('Jen', $person->getName());
  71. $this->assertSame('Jen', $person->translate()->getName());
  72. $this->assertSame('Женя', $person->translate('ru_RU')->getName());
  73. $this->assertSame('Amigo', $person->translate('es_ES')->getName());
  74. $this->assertSame('multilingual description', $person->getDescription());
  75. $this->assertSame('multilingual description', $person->translate()->getDescription());
  76. $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
  77. }
  78. protected function getUsedEntityFixtures()
  79. {
  80. return array(self::PERSON, self::PERSON.'Translation');
  81. }
  82. }