MaterializedPathODMMongoDBRepositoryTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. protected function getUsedEntityFixtures()
  82. {
  83. return array(
  84. self::CATEGORY
  85. );
  86. }
  87. public function createCategory()
  88. {
  89. $class = self::CATEGORY;
  90. return new $class;
  91. }
  92. private function populate()
  93. {
  94. $root = $this->createCategory();
  95. $root->setTitle("Food");
  96. $root2 = $this->createCategory();
  97. $root2->setTitle("Sports");
  98. $child = $this->createCategory();
  99. $child->setTitle("Fruits");
  100. $child->setParent($root);
  101. $child2 = $this->createCategory();
  102. $child2->setTitle("Vegitables");
  103. $child2->setParent($root);
  104. $childsChild = $this->createCategory();
  105. $childsChild->setTitle("Carrots");
  106. $childsChild->setParent($child2);
  107. $potatoes = $this->createCategory();
  108. $potatoes->setTitle("Potatoes");
  109. $potatoes->setParent($child2);
  110. $this->dm->persist($root);
  111. $this->dm->persist($root2);
  112. $this->dm->persist($child);
  113. $this->dm->persist($child2);
  114. $this->dm->persist($childsChild);
  115. $this->dm->persist($potatoes);
  116. $this->dm->flush();
  117. }
  118. }