InheritanceTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\ORM\Query;
  6. use Translatable\Fixture\File;
  7. use Translatable\Fixture\Image;
  8. use Translatable\Fixture\TemplatedArticle;
  9. use Gedmo\Translatable\Query\TreeWalker\TranslationWalker;
  10. /**
  11. * These are tests for translatable behavior
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class InheritanceTest extends BaseTestCaseORM
  18. {
  19. const ARTICLE = 'Translatable\\Fixture\\TemplatedArticle';
  20. const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
  21. const FILE = 'Translatable\\Fixture\\File';
  22. const IMAGE = 'Translatable\\Fixture\\Image';
  23. const TREE_WALKER_TRANSLATION = 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker';
  24. private $translatableListener;
  25. protected function setUp()
  26. {
  27. parent::setUp();
  28. $evm = new EventManager;
  29. $this->translatableListener = new TranslatableListener();
  30. $this->translatableListener->setTranslatableLocale('en');
  31. $this->translatableListener->setDefaultLocale('en');
  32. $evm->addEventSubscriber($this->translatableListener);
  33. $this->getMockSqliteEntityManager($evm);
  34. }
  35. /**
  36. * @test
  37. */
  38. function shouldHandleMappedSuperclass()
  39. {
  40. $article = new TemplatedArticle();
  41. $article->setName('name in en');
  42. $article->setContent('content in en');
  43. $article->setTitle('title in en');
  44. $this->em->persist($article);
  45. $this->em->flush();
  46. $this->em->clear();
  47. $repo = $this->em->getRepository(self::TRANSLATION);
  48. $this->assertTrue($repo instanceof Entity\Repository\TranslationRepository);
  49. $translations = $repo->findTranslations($article);
  50. $this->assertCount(0, $translations);
  51. // test second translations
  52. $article = $this->em->getRepository(self::ARTICLE)->find(1);
  53. $this->translatableListener->setTranslatableLocale('de');
  54. $article->setName('name in de');
  55. $article->setContent('content in de');
  56. $article->setTitle('title in de');
  57. $this->em->persist($article);
  58. $this->em->flush();
  59. $this->em->clear();
  60. $translations = $repo->findTranslations($article);
  61. $this->assertCount(1, $translations);
  62. $this->assertArrayHasKey('de', $translations);
  63. $this->assertArrayHasKey('name', $translations['de']);
  64. $this->assertEquals('name in de', $translations['de']['name']);
  65. $this->assertArrayHasKey('title', $translations['de']);
  66. $this->assertEquals('title in de', $translations['de']['title']);
  67. $this->assertArrayHasKey('content', $translations['de']);
  68. $this->assertEquals('content in de', $translations['de']['content']);
  69. }
  70. /**
  71. * @test
  72. */
  73. function shouldHandleInheritedTranslationsThroughBaseObjectClass()
  74. {
  75. $file = new File;
  76. $file->setSize(500);
  77. $file->setName('file en');
  78. $image = new Image;
  79. $image->setMime('mime en');
  80. $image->setName('image en');
  81. $image->setSize(445);
  82. $this->em->persist($file);
  83. $this->em->persist($image);
  84. $this->em->flush();
  85. $this->translatableListener->setTranslatableLocale('de');
  86. $file->setName('file de');
  87. $image->setName('image de');
  88. $image->setMime('mime de');
  89. $this->em->persist($file);
  90. $this->em->persist($image);
  91. $this->em->flush();
  92. $this->em->clear();
  93. $dql = 'SELECT f FROM ' . self::FILE . ' f';
  94. $q = $this->em->createQuery($dql);
  95. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  96. $files = $q->getArrayResult();
  97. $this->assertCount(2, $files);
  98. $this->assertEquals('image de', $files[0]['name']);
  99. $this->assertEquals('file de', $files[1]['name']);
  100. // test loading in locale
  101. $images = $this->em->getRepository(self::IMAGE)->findAll();
  102. $this->assertCount(1, $images);
  103. $this->assertEquals('image de', $images[0]->getName());
  104. $this->assertEquals('mime de', $images[0]->getMime());
  105. }
  106. protected function getUsedEntityFixtures()
  107. {
  108. return array(
  109. self::ARTICLE,
  110. self::TRANSLATION,
  111. self::FILE,
  112. self::IMAGE
  113. );
  114. }
  115. }