ClosureTreeRepositoryTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\Util\Debug;
  4. use Tree\Fixture\Closure\Category;
  5. use Tool\Logging\DBAL\QueryAnalyzer;
  6. /**
  7. * These are tests for Tree behavior
  8. *
  9. * @author Gustavo Adrian <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 ClosureTreeRepositoryTest extends \PHPUnit_Framework_TestCase
  16. {
  17. const TEST_ENTITY_CLASS = "Tree\Fixture\Closure\Category";
  18. const TEST_CLOSURE_CLASS = "Tree\Fixture\Closure\CategoryClosure";
  19. const TEST_BASE_CLOSURE_CLASS = "Gedmo\Tree\Entity\AbstractClosure";
  20. private $em;
  21. public function setUp()
  22. {
  23. $config = new \Doctrine\ORM\Configuration();
  24. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  25. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  26. $config->setProxyDir(__DIR__ . '/Proxy');
  27. $config->setProxyNamespace('Gedmo\Tree\Proxies');
  28. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  29. $conn = array(
  30. 'driver' => 'pdo_sqlite',
  31. 'memory' => true,
  32. );
  33. /*$conn = array(
  34. 'driver' => 'pdo_mysql',
  35. 'host' => '127.0.0.1',
  36. 'dbname' => 'closure',
  37. 'user' => 'root',
  38. 'password' => ''
  39. );*/
  40. $evm = new \Doctrine\Common\EventManager();
  41. $treeListener = new TreeListener(TreeListener::TYPE_CLOSURE);
  42. $evm->addEventSubscriber($treeListener);
  43. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  44. $schema = array(
  45. $this->em->getClassMetadata(self::TEST_BASE_CLOSURE_CLASS),
  46. $this->em->getClassMetadata(self::TEST_CLOSURE_CLASS),
  47. $this->em->getClassMetadata(self::TEST_ENTITY_CLASS),
  48. );
  49. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  50. $schemaTool->dropSchema( $schema );
  51. $schemaTool->createSchema( $schema );
  52. $this->analyzer = new QueryAnalyzer(
  53. $this->em->getConnection()->getDatabasePlatform()
  54. );
  55. $config->setSQLLogger($this->analyzer);
  56. $this->populate();
  57. }
  58. public function test_childCount_returnsNumberOfChilds()
  59. {
  60. $repo = $this->em->getRepository( self::TEST_ENTITY_CLASS );
  61. $food = $repo->findOneByTitle( 'Food' );
  62. $closureRepo = $this->em->getRepository( self::TEST_CLOSURE_CLASS );
  63. $childCount = $closureRepo->childCount( $food );
  64. $this->assertEquals( $childCount, 4 );
  65. }
  66. public function test_childCount_returnsNumberOfDirectChilds()
  67. {
  68. $repo = $this->em->getRepository( self::TEST_ENTITY_CLASS );
  69. $food = $repo->findOneByTitle( 'Food' );
  70. $closureRepo = $this->em->getRepository( self::TEST_CLOSURE_CLASS );
  71. $childCount = $closureRepo->childCount( $food, true );
  72. $this->assertEquals( $childCount, 2 );
  73. }
  74. private function populate()
  75. {
  76. $root = new Category();
  77. $root->setTitle("Food");
  78. $this->food = $root;
  79. $root2 = new Category();
  80. $root2->setTitle("Sports");
  81. $this->sports = $root2;
  82. $child = new Category();
  83. $child->setTitle("Fruits");
  84. $child->setParent($root);
  85. $this->fruits = $child;
  86. $child2 = new Category();
  87. $child2->setTitle("Vegitables");
  88. $child2->setParent($root);
  89. $this->vegitables = $child2;
  90. $childsChild = new Category();
  91. $childsChild->setTitle("Carrots");
  92. $childsChild->setParent($child2);
  93. $this->carrots = $childsChild;
  94. $potatoes = new Category();
  95. $potatoes->setTitle("Potatoes");
  96. $potatoes->setParent($child2);
  97. $this->potatoes = $potatoes;
  98. $this->em->persist($this->food);
  99. $this->em->persist($this->sports);
  100. $this->em->persist($this->fruits);
  101. $this->em->persist($this->vegitables);
  102. $this->em->persist($this->carrots);
  103. $this->em->persist($this->potatoes);
  104. $this->em->flush();
  105. $this->em->clear();
  106. }
  107. }