TranslatableSluggableTreeTest.php 6.7 KB

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