ClosureTreeRepositoryTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug;
  6. use Tree\Fixture\Closure\Category;
  7. use Tool\Logging\DBAL\QueryAnalyzer;
  8. /**
  9. * These are tests for Tree behavior
  10. *
  11. * @author Gustavo Adrian <comfortablynumb84@gmail.com>
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @package Gedmo.Tree
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class ClosureTreeRepositoryTest extends BaseTestCaseORM
  18. {
  19. const CATEGORY = "Tree\\Fixture\\Closure\\Category";
  20. const CLOSURE = "Tree\\Fixture\\Closure\\CategoryClosure";
  21. protected function setUp()
  22. {
  23. parent::setUp();
  24. $evm = new EventManager;
  25. $evm->addEventSubscriber(new TreeListener);
  26. $this->getMockSqliteEntityManager($evm);
  27. $this->populate();
  28. }
  29. public function test_childCount_returnsNumberOfChilds()
  30. {
  31. $repo = $this->em->getRepository(self::CATEGORY);
  32. $food = $repo->findOneByTitle('Food');
  33. $closureRepo = $this->em->getRepository(self::CLOSURE);
  34. $childCount = $closureRepo->childCount($food);
  35. $this->assertEquals($childCount, 4);
  36. }
  37. public function test_childCount_returnsNumberOfDirectChilds()
  38. {
  39. $repo = $this->em->getRepository(self::CATEGORY);
  40. $food = $repo->findOneByTitle('Food');
  41. $closureRepo = $this->em->getRepository(self::CLOSURE);
  42. $childCount = $closureRepo->childCount($food, true);
  43. $this->assertEquals($childCount, 2);
  44. }
  45. protected function getUsedEntityFixtures()
  46. {
  47. return array(
  48. self::CATEGORY,
  49. self::CLOSURE
  50. );
  51. }
  52. private function populate()
  53. {
  54. $root = new Category();
  55. $root->setTitle("Food");
  56. $this->food = $root;
  57. $root2 = new Category();
  58. $root2->setTitle("Sports");
  59. $this->sports = $root2;
  60. $child = new Category();
  61. $child->setTitle("Fruits");
  62. $child->setParent($root);
  63. $this->fruits = $child;
  64. $child2 = new Category();
  65. $child2->setTitle("Vegitables");
  66. $child2->setParent($root);
  67. $this->vegitables = $child2;
  68. $childsChild = new Category();
  69. $childsChild->setTitle("Carrots");
  70. $childsChild->setParent($child2);
  71. $this->carrots = $childsChild;
  72. $potatoes = new Category();
  73. $potatoes->setTitle("Potatoes");
  74. $potatoes->setParent($child2);
  75. $this->potatoes = $potatoes;
  76. $this->em->persist($this->food);
  77. $this->em->persist($this->sports);
  78. $this->em->persist($this->fruits);
  79. $this->em->persist($this->vegitables);
  80. $this->em->persist($this->carrots);
  81. $this->em->persist($this->potatoes);
  82. $this->em->flush();
  83. $this->em->clear();
  84. }
  85. }