MultiInheritanceTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug;
  6. /**
  7. * These are tests for Tree behavior
  8. *
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @link http://www.gediminasm.org
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. class MultiInheritanceTest extends BaseTestCaseORM
  14. {
  15. const NODE = "Tree\\Fixture\\Node";
  16. const BASE_NODE = "Tree\\Fixture\\BaseNode";
  17. const ANODE = "Tree\\Fixture\\ANode";
  18. const TRANSLATION = "Gedmo\\Translatable\\Entity\\Translation";
  19. protected function setUp()
  20. {
  21. parent::setUp();
  22. $this->getMockSqliteEntityManager();
  23. $this->populate();
  24. }
  25. public function testInheritance()
  26. {
  27. $meta = $this->em->getClassMetadata(self::NODE);
  28. $repo = $this->em->getRepository(self::NODE);
  29. $food = $repo->findOneByIdentifier('food');
  30. $left = $meta->getReflectionProperty('lft')->getValue($food);
  31. $right = $meta->getReflectionProperty('rgt')->getValue($food);
  32. $this->assertEquals(1, $left);
  33. $this->assertNotNull($food->getCreated());
  34. $this->assertNotNull($food->getUpdated());
  35. $translationRepo = $this->em->getRepository(self::TRANSLATION);
  36. $translations = $translationRepo->findTranslations($food);
  37. $this->assertCount(0, $translations);
  38. $this->assertEquals('food', $food->getSlug());
  39. }
  40. /**
  41. * Test case for github issue#7
  42. * Child count is invalid resulting in SINGLE_TABLE and JOINED
  43. * inheritance mapping. Also getChildren, getPath results are invalid
  44. */
  45. public function testCaseGithubIssue7()
  46. {
  47. $repo = $this->em->getRepository(self::NODE);
  48. $vegies = $repo->findOneByTitle('Vegitables');
  49. $count = $repo->childCount($vegies, true/*direct*/);
  50. $this->assertEquals(3, $count);
  51. $children = $repo->children($vegies, true);
  52. $this->assertCount(3, $children);
  53. // node repository will not find it
  54. $baseNodeRepo = $this->em->getRepository(self::BASE_NODE);
  55. $cabbage = $baseNodeRepo->findOneByIdentifier('cabbage');
  56. $path = $baseNodeRepo->getPath($cabbage);
  57. $this->assertCount(3, $path);
  58. }
  59. protected function getUsedEntityFixtures()
  60. {
  61. return array(
  62. self::NODE,
  63. self::ANODE,
  64. self::TRANSLATION,
  65. self::BASE_NODE
  66. );
  67. }
  68. private function populate()
  69. {
  70. $root = new \Tree\Fixture\Node();
  71. $root->setTitle("Food");
  72. $root->setIdentifier('food');
  73. $root2 = new \Tree\Fixture\Node();
  74. $root2->setTitle("Sports");
  75. $root2->setIdentifier('sport');
  76. $child = new \Tree\Fixture\Node();
  77. $child->setTitle("Fruits");
  78. $child->setParent($root);
  79. $child->setIdentifier('fruit');
  80. $child2 = new \Tree\Fixture\Node();
  81. $child2->setTitle("Vegitables");
  82. $child2->setParent($root);
  83. $child2->setIdentifier('vegie');
  84. $childsChild = new \Tree\Fixture\Node();
  85. $childsChild->setTitle("Carrots");
  86. $childsChild->setParent($child2);
  87. $childsChild->setIdentifier('carrot');
  88. $potatoes = new \Tree\Fixture\Node();
  89. $potatoes->setTitle("Potatoes");
  90. $potatoes->setParent($child2);
  91. $potatoes->setIdentifier('potatoe');
  92. $cabbages = new \Tree\Fixture\BaseNode();
  93. $cabbages->setIdentifier('cabbage');
  94. $cabbages->setParent($child2);
  95. $this->em->persist($root);
  96. $this->em->persist($root2);
  97. $this->em->persist($child);
  98. $this->em->persist($child2);
  99. $this->em->persist($childsChild);
  100. $this->em->persist($potatoes);
  101. $this->em->persist($cabbages);
  102. $this->em->flush();
  103. $this->em->clear();
  104. }
  105. }