MultiInheritanceTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\Util\Debug;
  4. /**
  5. * These are tests for Tree behavior
  6. *
  7. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  8. * @package Gedmo.Tree
  9. * @link http://www.gediminasm.org
  10. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  11. */
  12. class MultiInheritanceTest extends \PHPUnit_Framework_TestCase
  13. {
  14. const TEST_ENTITY_CLASS = "Tree\Fixture\Node";
  15. const TEST_BASE_NODE_CLASS = "Tree\Fixture\BaseNode";
  16. const TEST_TREE_NODE_CLASS = "Tree\Fixture\ANode";
  17. const TEST_ENTITY_TRANSLATION = "Gedmo\Translatable\Entity\Translation";
  18. private $em;
  19. public function setUp()
  20. {
  21. $config = new \Doctrine\ORM\Configuration();
  22. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  23. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  24. $config->setProxyDir(__DIR__ . '/Proxy');
  25. $config->setProxyNamespace('Gedmo\Tree\Proxies');
  26. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  27. $conn = array(
  28. 'driver' => 'pdo_sqlite',
  29. 'memory' => true,
  30. );
  31. //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  32. $evm = new \Doctrine\Common\EventManager();
  33. $evm->addEventSubscriber(new \Gedmo\Timestampable\TimestampableListener());
  34. $evm->addEventSubscriber(new \Gedmo\Sluggable\SluggableListener());
  35. $evm->addEventSubscriber(new \Gedmo\Tree\TreeListener());
  36. $translationListener = new \Gedmo\Translatable\TranslationListener();
  37. $translationListener->setTranslatableLocale('en_us');
  38. $evm->addEventSubscriber($translationListener);
  39. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  40. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  41. $schemaTool->dropSchema(array());
  42. $schemaTool->createSchema(array(
  43. $this->em->getClassMetadata(self::TEST_TREE_NODE_CLASS),
  44. $this->em->getClassMetadata(self::TEST_BASE_NODE_CLASS),
  45. $this->em->getClassMetadata(self::TEST_ENTITY_CLASS),
  46. $this->em->getClassMetadata(self::TEST_ENTITY_TRANSLATION)
  47. ));
  48. $this->_populate();
  49. }
  50. public function testInheritance()
  51. {
  52. $meta = $this->em->getClassMetadata(self::TEST_ENTITY_CLASS);
  53. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  54. $food = $repo->findOneByIdentifier('food');
  55. $left = $meta->getReflectionProperty('lft')->getValue($food);
  56. $right = $meta->getReflectionProperty('rgt')->getValue($food);
  57. $this->assertEquals(1, $left);
  58. $this->assertTrue($food->getCreated() !== null);
  59. $this->assertTrue($food->getUpdated() !== null);
  60. $translationRepo = $this->em->getRepository(self::TEST_ENTITY_TRANSLATION);
  61. $translations = $translationRepo->findTranslations($food);
  62. $this->assertEquals(1, count($translations));
  63. $this->assertEquals('food', $food->getSlug());
  64. $this->assertEquals(2, count($translations['en_us']));
  65. }
  66. /**
  67. * Test case for github issue#7
  68. * Child count is invalid resulting in SINGLE_TABLE and JOINED
  69. * inheritance mapping. Also getChildren, getPath results are invalid
  70. */
  71. public function testCaseGithubIssue7()
  72. {
  73. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  74. $vegies = $repo->findOneByTitle('Vegitables');
  75. $count = $repo->childCount($vegies, true/*direct*/);
  76. $this->assertEquals(3, $count);
  77. $children = $repo->children($vegies, true);
  78. $this->assertEquals(3, count($children));
  79. // node repository will not find it
  80. $cabbage = $this->em->getRepository('Tree\Fixture\BaseNode')->findOneByIdentifier('cabbage');
  81. $path = $repo->getPath($cabbage);
  82. $this->assertEquals(3, count($path));
  83. }
  84. private function _populate()
  85. {
  86. $root = new \Tree\Fixture\Node();
  87. $root->setTitle("Food");
  88. $root->setIdentifier('food');
  89. $root2 = new \Tree\Fixture\Node();
  90. $root2->setTitle("Sports");
  91. $root2->setIdentifier('sport');
  92. $child = new \Tree\Fixture\Node();
  93. $child->setTitle("Fruits");
  94. $child->setParent($root);
  95. $child->setIdentifier('fruit');
  96. $child2 = new \Tree\Fixture\Node();
  97. $child2->setTitle("Vegitables");
  98. $child2->setParent($root);
  99. $child2->setIdentifier('vegie');
  100. $childsChild = new \Tree\Fixture\Node();
  101. $childsChild->setTitle("Carrots");
  102. $childsChild->setParent($child2);
  103. $childsChild->setIdentifier('carrot');
  104. $potatoes = new \Tree\Fixture\Node();
  105. $potatoes->setTitle("Potatoes");
  106. $potatoes->setParent($child2);
  107. $potatoes->setIdentifier('potatoe');
  108. $cabbages = new \Tree\Fixture\BaseNode();
  109. $cabbages->setIdentifier('cabbage');
  110. $cabbages->setParent($child2);
  111. $this->em->persist($root);
  112. $this->em->persist($root2);
  113. $this->em->persist($child);
  114. $this->em->persist($child2);
  115. $this->em->persist($childsChild);
  116. $this->em->persist($potatoes);
  117. $this->em->persist($cabbages);
  118. $this->em->flush();
  119. $this->em->clear();
  120. }
  121. }