|
5 | 5 | use Doctrine\Common\EventManager; |
6 | 6 | use Doctrine\DBAL\Logging\DebugStack; |
7 | 7 | use Doctrine\ORM\Query; |
| 8 | +use Gedmo\Tree\Entity\Repository\NestedTreeRepository; |
8 | 9 | use Tool\BaseTestCaseORM; |
9 | 10 | use Tree\Fixture\RootCategory; |
10 | 11 |
|
@@ -35,13 +36,15 @@ protected function setUp() |
35 | 36 | public function testFullTreeHydration() |
36 | 37 | { |
37 | 38 | $this->populate(); |
| 39 | + $this->em->clear(); |
38 | 40 |
|
39 | 41 | $stack = new DebugStack(); |
40 | 42 | $this->em->getConfiguration()->setSQLLogger($stack); |
41 | 43 |
|
42 | 44 | $repo = $this->em->getRepository(self::ROOT_CATEGORY); |
43 | 45 |
|
44 | 46 | $result = $repo->createQueryBuilder('node') |
| 47 | + ->orderBy('node.lft', 'ASC') |
45 | 48 | ->getQuery() |
46 | 49 | ->setHint(Query::HINT_INCLUDE_META_COLUMNS, true) |
47 | 50 | ->getResult('tree'); |
@@ -80,6 +83,86 @@ public function testFullTreeHydration() |
80 | 83 | $this->assertEquals(count($stack->queries), 1); |
81 | 84 | } |
82 | 85 |
|
| 86 | + public function testPartialTreeHydration() |
| 87 | + { |
| 88 | + $this->populate(); |
| 89 | + $this->em->clear(); |
| 90 | + |
| 91 | + $stack = new DebugStack(); |
| 92 | + $this->em->getConfiguration()->setSQLLogger($stack); |
| 93 | + |
| 94 | + /** @var NestedTreeRepository $repo */ |
| 95 | + $repo = $this->em->getRepository(self::ROOT_CATEGORY); |
| 96 | + |
| 97 | + $fruits = $repo->findOneBy(array('title' => 'Fruits')); |
| 98 | + |
| 99 | + $result = $repo->getChildrenQuery($fruits, false, null, 'ASC', true) |
| 100 | + ->setHint(Query::HINT_INCLUDE_META_COLUMNS, true) |
| 101 | + ->getResult('tree'); |
| 102 | + |
| 103 | + $this->assertEquals(count($result), 1); |
| 104 | + |
| 105 | + $fruits = $result[0]; |
| 106 | + $this->assertEquals($fruits->getTitle(), 'Fruits'); |
| 107 | + $this->assertEquals(count($fruits->getChildren()), 2); |
| 108 | + |
| 109 | + $oranges = $fruits->getChildren()->get(0); |
| 110 | + $this->assertEquals($oranges->getTitle(), 'Oranges'); |
| 111 | + $this->assertEquals(count($oranges->getChildren()), 0); |
| 112 | + |
| 113 | + $citrons = $fruits->getChildren()->get(1); |
| 114 | + $this->assertEquals($citrons->getTitle(), 'Citrons'); |
| 115 | + $this->assertEquals(count($citrons->getChildren()), 0); |
| 116 | + |
| 117 | + $this->assertEquals(count($stack->queries), 2); |
| 118 | + } |
| 119 | + |
| 120 | + public function testMultipleRootNodesTreeHydration() |
| 121 | + { |
| 122 | + $this->populate(); |
| 123 | + $this->em->clear(); |
| 124 | + |
| 125 | + $stack = new DebugStack(); |
| 126 | + $this->em->getConfiguration()->setSQLLogger($stack); |
| 127 | + |
| 128 | + /** @var NestedTreeRepository $repo */ |
| 129 | + $repo = $this->em->getRepository(self::ROOT_CATEGORY); |
| 130 | + |
| 131 | + $food = $repo->findOneBy(array('title' => 'Food')); |
| 132 | + |
| 133 | + $result = $repo->getChildrenQuery($food) |
| 134 | + ->setHint(Query::HINT_INCLUDE_META_COLUMNS, true) |
| 135 | + ->getResult('tree'); |
| 136 | + |
| 137 | + $this->assertEquals(count($result), 4); |
| 138 | + |
| 139 | + $fruits = $result[0]; |
| 140 | + $this->assertEquals($fruits->getTitle(), 'Fruits'); |
| 141 | + $this->assertEquals(count($fruits->getChildren()), 2); |
| 142 | + |
| 143 | + $vegetables = $result[1]; |
| 144 | + $this->assertEquals($vegetables->getTitle(), 'Vegetables'); |
| 145 | + $this->assertEquals(count($vegetables->getChildren()), 0); |
| 146 | + |
| 147 | + $milk = $result[2]; |
| 148 | + $this->assertEquals($milk->getTitle(), 'Milk'); |
| 149 | + $this->assertEquals(count($milk->getChildren()), 0); |
| 150 | + |
| 151 | + $meat = $result[3]; |
| 152 | + $this->assertEquals($meat->getTitle(), 'Meat'); |
| 153 | + $this->assertEquals(count($meat->getChildren()), 0); |
| 154 | + |
| 155 | + $oranges = $fruits->getChildren()->get(0); |
| 156 | + $this->assertEquals($oranges->getTitle(), 'Oranges'); |
| 157 | + $this->assertEquals(count($oranges->getChildren()), 0); |
| 158 | + |
| 159 | + $citrons = $fruits->getChildren()->get(1); |
| 160 | + $this->assertEquals($citrons->getTitle(), 'Citrons'); |
| 161 | + $this->assertEquals(count($citrons->getChildren()), 0); |
| 162 | + |
| 163 | + $this->assertEquals(count($stack->queries), 2); |
| 164 | + } |
| 165 | + |
83 | 166 | private function populate() |
84 | 167 | { |
85 | 168 | $repo = $this->em->getRepository(self::ROOT_CATEGORY); |
@@ -107,12 +190,12 @@ private function populate() |
107 | 190 |
|
108 | 191 | $repo |
109 | 192 | ->persistAsFirstChild($food) |
110 | | - ->persistAsFirstChildOf($fruits, $food) |
111 | | - ->persistAsFirstChildOf($vegetables, $food) |
| 193 | + ->persistAsLastChildOf($fruits, $food) |
| 194 | + ->persistAsLastChildOf($vegetables, $food) |
112 | 195 | ->persistAsLastChildOf($milk, $food) |
113 | 196 | ->persistAsLastChildOf($meat, $food) |
114 | | - ->persistAsFirstChildOf($oranges, $fruits) |
115 | | - ->persistAsFirstChildOf($citrons, $fruits); |
| 197 | + ->persistAsLastChildOf($oranges, $fruits) |
| 198 | + ->persistAsLastChildOf($citrons, $fruits); |
116 | 199 |
|
117 | 200 | $this->em->flush(); |
118 | 201 | } |
|
0 commit comments