MaterializedPathODMMongoDBRepositoryTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseMongoODM;
  5. use Tree\Fixture\RootCategory;
  6. /**
  7. * These are tests for Tree behavior
  8. *
  9. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @package Gedmo.Tree
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class MaterializedPathODMMongoDBRepositoryTest extends BaseTestCaseMongoODM
  16. {
  17. const CATEGORY = "Tree\\Fixture\\Document\\Category";
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. $evm = new EventManager;
  22. $evm->addEventSubscriber(new TreeListener);
  23. $this->getMockDocumentManager($evm);
  24. $this->populate();
  25. }
  26. /**
  27. * @test
  28. */
  29. function getRootNodes()
  30. {
  31. $repo = $this->dm->getRepository(self::CATEGORY);
  32. $result = $repo->getRootNodes('title');
  33. $this->assertEquals(2, $result->count());
  34. $this->assertEquals('Food', $result->getNext()->getTitle());
  35. $this->assertEquals('Sports', $result->getNext()->getTitle());
  36. }
  37. /**
  38. * @test
  39. */
  40. function getChildren()
  41. {
  42. $repo = $this->dm->getRepository(self::CATEGORY);
  43. $root = $repo->findOneByTitle('Food');
  44. // Get all children from the root
  45. $result = $repo->getChildren($root, false, 'title');
  46. $this->assertEquals(4, count($result));
  47. $this->assertEquals('Carrots', $result->getNext()->getTitle());
  48. $this->assertEquals('Fruits', $result->getNext()->getTitle());
  49. $this->assertEquals('Potatoes', $result->getNext()->getTitle());
  50. $this->assertEquals('Vegitables', $result->getNext()->getTitle());
  51. // Get direct children from the root
  52. $result = $repo->getChildren($root, true, 'title');
  53. $this->assertEquals(2, $result->count());
  54. $this->assertEquals('Fruits', $result->getNext()->getTitle());
  55. $this->assertEquals('Vegitables', $result->getNext()->getTitle());
  56. // Get ALL nodes
  57. $result = $repo->getChildren(null, false, 'title');
  58. $this->assertEquals(6, $result->count());
  59. $this->assertEquals('Carrots', $result->getNext()->getTitle());
  60. $this->assertEquals('Food', $result->getNext()->getTitle());
  61. $this->assertEquals('Fruits', $result->getNext()->getTitle());
  62. $this->assertEquals('Potatoes', $result->getNext()->getTitle());
  63. $this->assertEquals('Sports', $result->getNext()->getTitle());
  64. $this->assertEquals('Vegitables', $result->getNext()->getTitle());
  65. }
  66. /**
  67. * @test
  68. */
  69. function getTree()
  70. {
  71. $repo = $this->dm->getRepository(self::CATEGORY);
  72. $tree = $repo->getTree();
  73. $this->assertEquals(6, $tree->count());
  74. $this->assertEquals('Food', $tree->getNext()->getTitle());
  75. $this->assertEquals('Fruits', $tree->getNext()->getTitle());
  76. $this->assertEquals('Vegitables', $tree->getNext()->getTitle());
  77. $this->assertEquals('Carrots', $tree->getNext()->getTitle());
  78. $this->assertEquals('Potatoes', $tree->getNext()->getTitle());
  79. $this->assertEquals('Sports', $tree->getNext()->getTitle());
  80. }
  81. /**
  82. * @test
  83. */
  84. function childrenHierarchy()
  85. {
  86. $repo = $this->dm->getRepository(self::CATEGORY);
  87. $roots = $repo->getRootNodes();
  88. $tree = $repo->childrenHierarchy($roots->getNext());
  89. $vegitablesChildren = $tree[0]['__children'][1]['__children'];
  90. $this->assertEquals('Food', $tree[0]['title']);
  91. $this->assertEquals('Fruits', $tree[0]['__children'][0]['title']);
  92. $this->assertEquals('Vegitables', $tree[0]['__children'][1]['title']);
  93. $this->assertEquals('Carrots', $vegitablesChildren[0]['title']);
  94. $this->assertEquals('Potatoes', $vegitablesChildren[1]['title']);
  95. $this->assertEquals('Sports', $tree[1]['title']);
  96. }
  97. protected function getUsedEntityFixtures()
  98. {
  99. return array(
  100. self::CATEGORY
  101. );
  102. }
  103. public function createCategory()
  104. {
  105. $class = self::CATEGORY;
  106. return new $class;
  107. }
  108. private function populate()
  109. {
  110. $root = $this->createCategory();
  111. $root->setTitle("Food");
  112. $root2 = $this->createCategory();
  113. $root2->setTitle("Sports");
  114. $child = $this->createCategory();
  115. $child->setTitle("Fruits");
  116. $child->setParent($root);
  117. $child2 = $this->createCategory();
  118. $child2->setTitle("Vegitables");
  119. $child2->setParent($root);
  120. $childsChild = $this->createCategory();
  121. $childsChild->setTitle("Carrots");
  122. $childsChild->setParent($child2);
  123. $potatoes = $this->createCategory();
  124. $potatoes->setTitle("Potatoes");
  125. $potatoes->setParent($child2);
  126. $this->dm->persist($root);
  127. $this->dm->persist($root2);
  128. $this->dm->persist($child);
  129. $this->dm->persist($child2);
  130. $this->dm->persist($childsChild);
  131. $this->dm->persist($potatoes);
  132. $this->dm->flush();
  133. }
  134. }