TranslatableSluggableTreeTest.php 5.1 KB

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