InheritanceTest.php 4.4KB

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