TranslatableSluggableTreeTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug,
  6. Tree\Fixture\BehavioralCategory,
  7. Tree\Fixture\Article,
  8. Tree\Fixture\Comment,
  9. Gedmo\Translatable\TranslationListener,
  10. Gedmo\Translatable\Entity\Translation,
  11. Gedmo\Sluggable\SluggableListener,
  12. Doctrine\ORM\Proxy\Proxy;
  13. /**
  14. * These are tests for Tree behavior
  15. *
  16. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  17. * @package Gedmo.Tree
  18. * @link http://www.gediminasm.org
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. class TranslatableSluggableTreeTest extends BaseTestCaseORM
  22. {
  23. const CATEGORY = "Tree\\Fixture\\BehavioralCategory";
  24. const ARTICLE = "Tree\\Fixture\\Article";
  25. const COMMENT = "Tree\\Fixture\\Comment";
  26. const TRANSLATION = "Gedmo\\Translatable\\Entity\\Translation";
  27. private $translationListener;
  28. protected function setUp()
  29. {
  30. parent::setUp();
  31. $evm = new EventManager;
  32. $evm->addEventSubscriber(new TreeListener);
  33. $this->translationListener = new TranslationListener;
  34. $this->translationListener->setTranslatableLocale('en_us');
  35. $evm->addEventSubscriber(new SluggableListener);
  36. $evm->addEventSubscriber($this->translationListener);
  37. $this->getMockSqliteEntityManager($evm);
  38. $this->populate();
  39. }
  40. public function testNestedBehaviors()
  41. {
  42. $vegies = $this->em->getRepository(self::CATEGORY)
  43. ->findOneByTitle('Vegitables');
  44. $childCount = $this->em->getRepository(self::CATEGORY)
  45. ->childCount($vegies);
  46. $this->assertEquals(2, $childCount);
  47. // test slug
  48. $this->assertEquals('vegitables', $vegies->getSlug());
  49. // run second translation test
  50. $this->translationListener->setTranslatableLocale('de_de');
  51. $vegies->setTitle('Deutschebles');
  52. $this->em->persist($vegies);
  53. $this->em->flush();
  54. $this->em->clear();
  55. $this->translationListener->setTranslatableLocale('en_us');
  56. $vegies = $this->em->getRepository(self::CATEGORY)
  57. ->find($vegies->getId());
  58. $translations = $this->em->getRepository(self::TRANSLATION)
  59. ->findTranslations($vegies);
  60. $this->assertEquals(2, count($translations));
  61. $this->assertArrayHasKey('de_de', $translations);
  62. $this->assertArrayHasKey('en_us', $translations);
  63. $this->assertArrayHasKey('title', $translations['de_de']);
  64. $this->assertEquals('Deutschebles', $translations['de_de']['title']);
  65. $this->assertArrayHasKey('slug', $translations['de_de']);
  66. $this->assertEquals('deutschebles', $translations['de_de']['slug']);
  67. $this->assertArrayHasKey('title', $translations['en_us']);
  68. $this->assertEquals('Vegitables', $translations['en_us']['title']);
  69. $this->assertArrayHasKey('slug', $translations['en_us']);
  70. $this->assertEquals('vegitables', $translations['en_us']['slug']);
  71. }
  72. public function testTranslations()
  73. {
  74. $this->populateDeTranslations();
  75. $repo = $this->em->getRepository(self::CATEGORY);
  76. $vegies = $repo->find(4);
  77. $this->assertEquals('Vegitables', $vegies->getTitle());
  78. $food = $vegies->getParent();
  79. // test if proxy triggers postLoad event
  80. $this->assertTrue($food instanceof Proxy);
  81. $this->assertEquals('Food', $food->getTitle());
  82. $this->em->clear();
  83. $this->translationListener->setTranslatableLocale('de_de');
  84. $vegies = $repo->find(4);
  85. $this->assertEquals('Gemüse', $vegies->getTitle());
  86. $food = $vegies->getParent();
  87. $this->assertTrue($food instanceof Proxy);
  88. $this->assertEquals('Lebensmittel', $food->getTitle());
  89. }
  90. protected function getUsedEntityFixtures()
  91. {
  92. return array(
  93. self::CATEGORY,
  94. self::ARTICLE,
  95. self::COMMENT,
  96. self::TRANSLATION
  97. );
  98. }
  99. private function populateDeTranslations()
  100. {
  101. $this->translationListener->setTranslatableLocale('de_de');
  102. $repo = $this->em->getRepository(self::CATEGORY);
  103. $food = $repo->findOneByTitle('Food');
  104. $food->setTitle('Lebensmittel');
  105. $vegies = $repo->findOneByTitle('Vegitables');
  106. $vegies->setTitle('Gemüse');
  107. $this->em->persist($food);
  108. $this->em->persist($vegies);
  109. $this->em->flush();
  110. $this->em->clear();
  111. $this->translationListener->setTranslatableLocale('en_us');
  112. }
  113. private function populate()
  114. {
  115. $root = new BehavioralCategory();
  116. $root->setTitle("Food");
  117. $root2 = new BehavioralCategory();
  118. $root2->setTitle("Sports");
  119. $child = new BehavioralCategory();
  120. $child->setTitle("Fruits");
  121. $child->setParent($root);
  122. $child2 = new BehavioralCategory();
  123. $child2->setTitle("Vegitables");
  124. $child2->setParent($root);
  125. $childsChild = new BehavioralCategory();
  126. $childsChild->setTitle("Carrots");
  127. $childsChild->setParent($child2);
  128. $potatoes = new BehavioralCategory();
  129. $potatoes->setTitle("Potatoes");
  130. $potatoes->setParent($child2);
  131. $this->em->persist($root);
  132. $this->em->persist($root2);
  133. $this->em->persist($child);
  134. $this->em->persist($child2);
  135. $this->em->persist($childsChild);
  136. $this->em->persist($potatoes);
  137. $this->em->flush();
  138. $this->em->clear();
  139. }
  140. }