MaterializedPathRepositoryTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseMongoODM;
  5. use Doctrine\Common\Util\Debug;
  6. use Tree\Fixture\RootCategory;
  7. /**
  8. * These are tests for Tree behavior
  9. *
  10. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @package Gedmo.Tree
  13. * @link http://www.gediminasm.org
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. class MaterializedPathRepositoryTest extends BaseTestCaseMongoODM
  17. {
  18. const CATEGORY = "Tree\\Fixture\\Document\\Category";
  19. protected function setUp()
  20. {
  21. parent::setUp();
  22. $evm = new EventManager;
  23. $evm->addEventSubscriber(new TreeListener);
  24. $this->getMockDocumentManager($evm);
  25. $this->populate();
  26. }
  27. /**
  28. * @test
  29. */
  30. function getRootNodes()
  31. {
  32. $repo = $this->dm->getRepository(self::CATEGORY);
  33. $result = $repo->getRootNodesQueryBuilder()->sort('title', 'asc')->getQuery()->execute();
  34. $this->assertEquals(2, count($result));
  35. $this->assertEquals('Food', $result->getNext()->getTitle());
  36. $this->assertEquals('Sports', $result->getNext()->getTitle());
  37. }
  38. protected function getUsedEntityFixtures()
  39. {
  40. return array(
  41. self::CATEGORY
  42. );
  43. }
  44. public function createCategory()
  45. {
  46. $class = self::CATEGORY;
  47. return new $class;
  48. }
  49. private function populate()
  50. {
  51. $root = $this->createCategory();
  52. $root->setTitle("Food");
  53. $root2 = $this->createCategory();
  54. $root2->setTitle("Sports");
  55. $child = $this->createCategory();
  56. $child->setTitle("Fruits");
  57. $child->setParent($root);
  58. $child2 = $this->createCategory();
  59. $child2->setTitle("Vegitables");
  60. $child2->setParent($root);
  61. $childsChild = $this->createCategory();
  62. $childsChild->setTitle("Carrots");
  63. $childsChild->setParent($child2);
  64. $potatoes = $this->createCategory();
  65. $potatoes->setTitle("Potatoes");
  66. $potatoes->setParent($child2);
  67. $this->dm->persist($root);
  68. $this->dm->persist($root2);
  69. $this->dm->persist($child);
  70. $this->dm->persist($child2);
  71. $this->dm->persist($childsChild);
  72. $this->dm->persist($potatoes);
  73. $this->dm->flush();
  74. }
  75. }