MaterializedPathRepositoryTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 MaterializedPathRepositoryTest 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->getRootNodesQueryBuilder()->sort('title', 'asc')->getQuery()->execute();
  33. $this->assertEquals(2, count($result));
  34. $this->assertEquals('Food', $result->getNext()->getTitle());
  35. $this->assertEquals('Sports', $result->getNext()->getTitle());
  36. }
  37. protected function getUsedEntityFixtures()
  38. {
  39. return array(
  40. self::CATEGORY
  41. );
  42. }
  43. public function createCategory()
  44. {
  45. $class = self::CATEGORY;
  46. return new $class;
  47. }
  48. private function populate()
  49. {
  50. $root = $this->createCategory();
  51. $root->setTitle("Food");
  52. $root2 = $this->createCategory();
  53. $root2->setTitle("Sports");
  54. $child = $this->createCategory();
  55. $child->setTitle("Fruits");
  56. $child->setParent($root);
  57. $child2 = $this->createCategory();
  58. $child2->setTitle("Vegitables");
  59. $child2->setParent($root);
  60. $childsChild = $this->createCategory();
  61. $childsChild->setTitle("Carrots");
  62. $childsChild->setParent($child2);
  63. $potatoes = $this->createCategory();
  64. $potatoes->setTitle("Potatoes");
  65. $potatoes->setParent($child2);
  66. $this->dm->persist($root);
  67. $this->dm->persist($root2);
  68. $this->dm->persist($child);
  69. $this->dm->persist($child2);
  70. $this->dm->persist($childsChild);
  71. $this->dm->persist($potatoes);
  72. $this->dm->flush();
  73. }
  74. }