TranslatableSluggableTreeTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /**
  11. * These are tests for Tree behavior
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Tree
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class TranslatableSluggableTreeTest extends \PHPUnit_Framework_TestCase
  19. {
  20. const TEST_ENTITY_CATEGORY = "Tree\Fixture\BehavioralCategory";
  21. const TEST_ENTITY_ARTICLE = "Tree\Fixture\Article";
  22. const TEST_ENTITY_COMMENT = "Tree\Fixture\Comment";
  23. const TEST_ENTITY_TRANSLATION = "Gedmo\Translatable\Entity\Translation";
  24. private $em;
  25. private $translationListener;
  26. public function setUp()
  27. {
  28. $classLoader = new \Doctrine\Common\ClassLoader('Tree\Fixture', __DIR__ . '/../');
  29. $classLoader->register();
  30. $config = new \Doctrine\ORM\Configuration();
  31. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  32. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  33. $config->setProxyDir(__DIR__ . '/Proxy');
  34. $config->setProxyNamespace('Gedmo\Tree\Proxies');
  35. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  36. $conn = array(
  37. 'driver' => 'pdo_sqlite',
  38. 'memory' => true,
  39. );
  40. //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  41. $evm = new \Doctrine\Common\EventManager();
  42. $treeListener = new TreeListener();
  43. $sluggableListener = new SluggableListener();
  44. $this->translationListener = new TranslationListener();
  45. $this->translationListener->setTranslatableLocale('en_us');
  46. $evm->addEventSubscriber($treeListener);
  47. $evm->addEventSubscriber($sluggableListener);
  48. $evm->addEventSubscriber($this->translationListener);
  49. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  50. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  51. $schemaTool->dropSchema(array());
  52. $schemaTool->createSchema(array(
  53. $this->em->getClassMetadata(self::TEST_ENTITY_CATEGORY),
  54. $this->em->getClassMetadata(self::TEST_ENTITY_ARTICLE),
  55. $this->em->getClassMetadata(self::TEST_ENTITY_COMMENT),
  56. $this->em->getClassMetadata(self::TEST_ENTITY_TRANSLATION)
  57. ));
  58. $this->_populate();
  59. }
  60. public function testNestedBehaviors()
  61. {
  62. $vegies = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  63. ->findOneByTitle('Vegitables');
  64. $childCount = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  65. ->childCount($vegies);
  66. $this->assertEquals(2, $childCount);
  67. // test slug
  68. $this->assertEquals('vegitables', $vegies->getSlug());
  69. // run second translation test
  70. $this->translationListener->setTranslatableLocale('de_de');
  71. $vegies->setTitle('Deutschebles');
  72. $this->em->persist($vegies);
  73. $this->em->flush();
  74. $this->em->clear();
  75. $this->translationListener->setTranslatableLocale('en_us');
  76. $vegies = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  77. ->find($vegies->getId());
  78. $translations = $this->em->getRepository(self::TEST_ENTITY_TRANSLATION)
  79. ->findTranslations($vegies);
  80. $this->assertEquals(2, count($translations));
  81. $this->assertArrayHasKey('de_de', $translations);
  82. $this->assertArrayHasKey('en_us', $translations);
  83. $this->assertArrayHasKey('title', $translations['de_de']);
  84. $this->assertEquals('Deutschebles', $translations['de_de']['title']);
  85. $this->assertArrayHasKey('slug', $translations['de_de']);
  86. $this->assertEquals('deutschebles', $translations['de_de']['slug']);
  87. $this->assertArrayHasKey('title', $translations['en_us']);
  88. $this->assertEquals('Vegitables', $translations['en_us']['title']);
  89. $this->assertArrayHasKey('slug', $translations['en_us']);
  90. $this->assertEquals('vegitables', $translations['en_us']['slug']);
  91. }
  92. protected function _populate()
  93. {
  94. $root = new BehavioralCategory();
  95. $root->setTitle("Food");
  96. $root2 = new BehavioralCategory();
  97. $root2->setTitle("Sports");
  98. $child = new BehavioralCategory();
  99. $child->setTitle("Fruits");
  100. $child->setParent($root);
  101. $child2 = new BehavioralCategory();
  102. $child2->setTitle("Vegitables");
  103. $child2->setParent($root);
  104. $childsChild = new BehavioralCategory();
  105. $childsChild->setTitle("Carrots");
  106. $childsChild->setParent($child2);
  107. $potatoes = new BehavioralCategory();
  108. $potatoes->setTitle("Potatoes");
  109. $potatoes->setParent($child2);
  110. $this->em->persist($root);
  111. $this->em->persist($root2);
  112. $this->em->persist($child);
  113. $this->em->persist($child2);
  114. $this->em->persist($childsChild);
  115. $this->em->persist($potatoes);
  116. $this->em->flush();
  117. $this->em->clear();
  118. }
  119. }