1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace StefanoTreeTest\Integration; |
||
6 | |||
7 | use StefanoTree\NestedSet as TreeAdapter; |
||
8 | use StefanoTree\NestedSet\Options; |
||
9 | use StefanoTreeTest\IntegrationTestCase; |
||
10 | use StefanoTreeTest\TestUtil; |
||
11 | |||
12 | /** |
||
13 | * @internal |
||
14 | */ |
||
15 | class NestedSetTest extends IntegrationTestCase |
||
16 | { |
||
17 | /** |
||
18 | * @var TreeAdapter |
||
19 | */ |
||
20 | protected $treeAdapter; |
||
21 | |||
22 | protected function setUp(): void |
||
23 | { |
||
24 | $this->treeAdapter = $this->getTreeAdapter(); |
||
25 | |||
26 | parent::setUp(); |
||
27 | } |
||
28 | |||
29 | protected function tearDown(): void |
||
30 | { |
||
31 | $this->treeAdapter = null; |
||
32 | parent::tearDown(); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @return TreeAdapter |
||
37 | */ |
||
38 | protected function getTreeAdapter() |
||
39 | { |
||
40 | $options = new Options(array( |
||
41 | 'tableName' => 'tree_traversal', |
||
42 | 'idColumnName' => 'tree_traversal_id', |
||
43 | )); |
||
44 | |||
45 | if ('pgsql' == TEST_STEFANO_DB_VENDOR) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
46 | $options->setSequenceName('tree_traversal_tree_traversal_id_seq'); |
||
47 | } |
||
48 | |||
49 | return new TreeAdapter($options, TestUtil::buildAdapter($options)); |
||
50 | } |
||
51 | |||
52 | protected function getDataSet() |
||
53 | { |
||
54 | switch ($this->getName()) { |
||
55 | case 'testCreateRootNode': |
||
56 | case 'testCreateRootNodeWithCustomData': |
||
57 | case 'testGetRootNodeRootDoesNotExist': |
||
58 | return $this->createArrayDataSet(include __DIR__.'/_files/NestedSet/initEmptyDataSet.php'); |
||
59 | |||
60 | default: |
||
61 | return $this->createArrayDataSet(include __DIR__.'/_files/NestedSet/initDataSet.php'); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | public function testCreateRootNode() |
||
66 | { |
||
67 | $newId = $this->treeAdapter |
||
68 | ->createRootNode(); |
||
69 | |||
70 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testCreateRootNode.php'); |
||
71 | $this->assertEquals(1, $newId); |
||
72 | } |
||
73 | |||
74 | public function testCreateRootNodeWithCustomData() |
||
75 | { |
||
76 | $newId = $this->treeAdapter |
||
77 | ->createRootNode(array('name' => 'This is root node')); |
||
78 | |||
79 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testCreateRootNodeWithCustomData.php'); |
||
80 | $this->assertEquals(1, $newId); |
||
81 | } |
||
82 | |||
83 | public function testCreateRootRootAlreadyExist() |
||
84 | { |
||
85 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
86 | $this->expectExceptionMessage('Root node already exist'); |
||
87 | |||
88 | $this->treeAdapter |
||
89 | ->createRootNode(); |
||
90 | $this->treeAdapter |
||
91 | ->createRootNode(); |
||
92 | } |
||
93 | |||
94 | public function testGetNode() |
||
95 | { |
||
96 | $expectedNodeData = array( |
||
97 | 'tree_traversal_id' => '12', |
||
98 | 'name' => null, |
||
99 | 'lft' => '18', |
||
100 | 'rgt' => '29', |
||
101 | 'parent_id' => '6', |
||
102 | 'level' => '3', |
||
103 | ); |
||
104 | |||
105 | $nodeData = $this->treeAdapter |
||
106 | ->getNode(12); |
||
107 | |||
108 | $this->assertEquals($expectedNodeData, $nodeData); |
||
109 | } |
||
110 | |||
111 | public function testGetNodeNodeDoesNotExist() |
||
112 | { |
||
113 | $this->assertNull($this->treeAdapter->getNode(123456789)); |
||
114 | } |
||
115 | |||
116 | public function testAddNodeTargetNodeDoesNotExist() |
||
117 | { |
||
118 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
119 | $this->expectExceptionMessage('Target Node does not exists.'); |
||
120 | |||
121 | try { |
||
122 | $this->treeAdapter |
||
123 | ->addNode(123456789, array(), TreeAdapter::PLACEMENT_BOTTOM); |
||
124 | } catch (\Exception $e) { |
||
125 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.php'); |
||
126 | |||
127 | throw $e; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | public function testCreateNodePlacementStrategyDoesNotExists() |
||
132 | { |
||
133 | $this->expectException(\StefanoTree\Exception\InvalidArgumentException::class); |
||
134 | $this->expectExceptionMessage('Unknown placement "unknown-placement"'); |
||
135 | |||
136 | $this->treeAdapter |
||
137 | ->addNode(1, array(), 'unknown-placement'); |
||
138 | } |
||
139 | |||
140 | public function testAddNodePlacementBottomTargetNodeIsRoot() |
||
141 | { |
||
142 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
143 | $this->expectExceptionMessage('Cannot create node. Target node is root. Root node cannot have sibling.'); |
||
144 | |||
145 | try { |
||
146 | $this->treeAdapter |
||
147 | ->addNode(1, array(), TreeAdapter::PLACEMENT_BOTTOM); |
||
148 | } catch (\Exception $e) { |
||
149 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.php'); |
||
150 | |||
151 | throw $e; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | public function testAddNodePlacementTopTargetNodeIsRoot() |
||
156 | { |
||
157 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
158 | $this->expectExceptionMessage('Cannot create node. Target node is root. Root node cannot have sibling.'); |
||
159 | |||
160 | try { |
||
161 | $this->treeAdapter |
||
162 | ->addNode(1, array(), TreeAdapter::PLACEMENT_TOP); |
||
163 | } catch (\Exception $e) { |
||
164 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.php'); |
||
165 | |||
166 | throw $e; |
||
167 | } |
||
168 | } |
||
169 | |||
170 | public function testAddNodePlacementBottom() |
||
171 | { |
||
172 | // test 1 |
||
173 | $lastGeneratedValue = $this->treeAdapter |
||
174 | ->addNode(12, array(), TreeAdapter::PLACEMENT_BOTTOM); |
||
175 | |||
176 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementBottom-1.php'); |
||
177 | $this->assertEquals(26, $lastGeneratedValue); |
||
178 | |||
179 | // test 2 with data |
||
180 | $data = array( |
||
181 | 'name' => 'ahoj', |
||
182 | ); |
||
183 | |||
184 | $lastGeneratedValue = $this->treeAdapter |
||
185 | ->addNode(19, $data, TreeAdapter::PLACEMENT_BOTTOM); |
||
186 | |||
187 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementBottom-2.php'); |
||
188 | $this->assertEquals(27, $lastGeneratedValue); |
||
189 | } |
||
190 | |||
191 | public function testAddNodeUserDefinedId() |
||
192 | { |
||
193 | $uuid = 652; |
||
194 | |||
195 | $lastGeneratedValue = $this->treeAdapter |
||
196 | ->addNode( |
||
197 | 12, |
||
198 | array( |
||
199 | 'tree_traversal_id' => $uuid, |
||
200 | ), |
||
201 | TreeAdapter::PLACEMENT_BOTTOM |
||
202 | ); |
||
203 | |||
204 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodeUserDefinedId.php'); |
||
205 | $this->assertEquals($uuid, $lastGeneratedValue); |
||
206 | } |
||
207 | |||
208 | public function testAddNodePlacementTop() |
||
209 | { |
||
210 | // test 1 |
||
211 | $lastGeneratedValue = $this->treeAdapter |
||
212 | ->addNode(16, array(), TreeAdapter::PLACEMENT_TOP); |
||
213 | |||
214 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementTop-1.php'); |
||
215 | $this->assertEquals(26, $lastGeneratedValue); |
||
216 | |||
217 | // test 2 with data |
||
218 | $data = array( |
||
219 | 'name' => 'ahoj', |
||
220 | ); |
||
221 | $lastGeneratedValue = $this->treeAdapter |
||
222 | ->addNode(3, $data, TreeAdapter::PLACEMENT_TOP); |
||
223 | |||
224 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementTop-2.php'); |
||
225 | $this->assertEquals(27, $lastGeneratedValue); |
||
226 | } |
||
227 | |||
228 | public function testAddNodePlacementChildBottom() |
||
229 | { |
||
230 | // test 1 |
||
231 | $lastGeneratedValue = $this->treeAdapter |
||
232 | ->addNode(21, array(), TreeAdapter::PLACEMENT_CHILD_BOTTOM); |
||
233 | |||
234 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementChildBottom-1.php'); |
||
235 | $this->assertEquals(26, $lastGeneratedValue); |
||
236 | |||
237 | // test 2 with data |
||
238 | $data = array( |
||
239 | 'name' => 'ahoj', |
||
240 | ); |
||
241 | $lastGeneratedValue = $this->treeAdapter |
||
242 | ->addNode(4, $data, TreeAdapter::PLACEMENT_CHILD_BOTTOM); |
||
243 | |||
244 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementChildBottom-2.php'); |
||
245 | $this->assertEquals(27, $lastGeneratedValue); |
||
246 | } |
||
247 | |||
248 | public function testAddNodePlacementChildTopDefaultPlacement() |
||
249 | { |
||
250 | // test 1 |
||
251 | $lastGeneratedValue = $this->treeAdapter |
||
252 | ->addNode(4); |
||
253 | |||
254 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementChildTop-1.php'); |
||
255 | $this->assertEquals(26, $lastGeneratedValue); |
||
256 | |||
257 | // test 2 with data |
||
258 | $data = array( |
||
259 | 'name' => 'ahoj', |
||
260 | ); |
||
261 | $lastGeneratedValue = $this->treeAdapter |
||
262 | ->addNode(10, $data); |
||
263 | |||
264 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementChildTop-2.php'); |
||
265 | $this->assertEquals(27, $lastGeneratedValue); |
||
266 | } |
||
267 | |||
268 | public function testDeleteBranchDoesNotExist() |
||
269 | { |
||
270 | $this->treeAdapter |
||
271 | ->deleteBranch(123456789); |
||
272 | |||
273 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.php'); |
||
274 | } |
||
275 | |||
276 | public function testDeleteBranch() |
||
277 | { |
||
278 | $this->treeAdapter |
||
279 | ->deleteBranch(6); |
||
280 | |||
281 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testDeleteBranch.php'); |
||
282 | } |
||
283 | |||
284 | public function testMoveNodeCannotMoveTargetNodeIsInsideSourceBranch() |
||
285 | { |
||
286 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
287 | $this->expectExceptionMessage('Cannot move. Target node is inside source branch.'); |
||
288 | |||
289 | try { |
||
290 | $this->treeAdapter |
||
291 | ->moveNode(1, 12); |
||
292 | } catch (\Exception $e) { |
||
293 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.php'); |
||
294 | |||
295 | throw $e; |
||
296 | } |
||
297 | } |
||
298 | |||
299 | public function testMoveNodeCannotMoveTargetAndSourceNodeAreEqual() |
||
300 | { |
||
301 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
302 | $this->expectExceptionMessage('Cannot move. Source node and Target node are equal.'); |
||
303 | |||
304 | try { |
||
305 | $this->treeAdapter |
||
306 | ->moveNode(10, 10); |
||
307 | } catch (\Exception $e) { |
||
308 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.php'); |
||
309 | |||
310 | throw $e; |
||
311 | } |
||
312 | } |
||
313 | |||
314 | public function testMoveNodeCannotMoveTargetNodeDoesNotExist() |
||
315 | { |
||
316 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
317 | $this->expectExceptionMessage('Cannot move. Target node does not exists.'); |
||
318 | |||
319 | try { |
||
320 | $this->treeAdapter |
||
321 | ->moveNode(5, 123456); |
||
322 | } catch (\Exception $e) { |
||
323 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.php'); |
||
324 | |||
325 | throw $e; |
||
326 | } |
||
327 | } |
||
328 | |||
329 | public function testMoveNodeCannotMoveSourceNodeDoesNotExist() |
||
330 | { |
||
331 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
332 | $this->expectExceptionMessage('Cannot move. Source node does not exists.'); |
||
333 | |||
334 | try { |
||
335 | $this->treeAdapter |
||
336 | ->moveNode(123456, 6); |
||
337 | } catch (\Exception $e) { |
||
338 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.php'); |
||
339 | |||
340 | throw $e; |
||
341 | } |
||
342 | } |
||
343 | |||
344 | public function testMoveNodePlacementStrategyDoesNotExists() |
||
345 | { |
||
346 | $this->expectException(\StefanoTree\Exception\InvalidArgumentException::class); |
||
347 | $this->expectExceptionMessage('Unknown placement "unknown-placement"'); |
||
348 | |||
349 | $this->treeAdapter |
||
350 | ->moveNode(11, 1, 'unknown-placement'); |
||
351 | } |
||
352 | |||
353 | public function placementDataProvider() |
||
354 | { |
||
355 | return array( |
||
356 | array(\StefanoTree\TreeInterface::PLACEMENT_TOP), |
||
357 | array(\StefanoTree\TreeInterface::PLACEMENT_BOTTOM), |
||
358 | ); |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @dataProvider placementDataProvider |
||
363 | * |
||
364 | * @param string $placement |
||
365 | * |
||
366 | * @throws \Exception |
||
367 | */ |
||
368 | public function testMoveNodeCannotCreateSiblingNodeAtRootNode(string $placement) |
||
369 | { |
||
370 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
371 | $this->expectExceptionMessage('Cannot move. Target node is root. Root node cannot have sibling.'); |
||
372 | |||
373 | try { |
||
374 | $this->treeAdapter |
||
375 | ->moveNode(11, 1, $placement); |
||
376 | } catch (\Exception $e) { |
||
377 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.php'); |
||
378 | |||
379 | throw $e; |
||
380 | } |
||
381 | } |
||
382 | |||
383 | public function testMoveNodePlacementBottom() |
||
384 | { |
||
385 | // test source node is already at required position |
||
386 | $this->treeAdapter |
||
387 | ->moveNode(3, 2, TreeAdapter::PLACEMENT_BOTTOM); |
||
388 | |||
389 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.php'); |
||
390 | |||
391 | // test |
||
392 | $this->treeAdapter |
||
393 | ->moveNode(14, 18, TreeAdapter::PLACEMENT_BOTTOM); |
||
394 | |||
395 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementBottom-1.php'); |
||
396 | |||
397 | // test |
||
398 | $this->treeAdapter |
||
399 | ->moveNode(16, 7, TreeAdapter::PLACEMENT_BOTTOM); |
||
400 | |||
401 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementBottom-2.php'); |
||
402 | |||
403 | // test |
||
404 | $this->treeAdapter |
||
405 | ->moveNode(14, 3, TreeAdapter::PLACEMENT_BOTTOM); |
||
406 | |||
407 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementBottom-3.php'); |
||
408 | } |
||
409 | |||
410 | public function testMoveNodePlacementTop() |
||
411 | { |
||
412 | // test source node is already at required position |
||
413 | $this->treeAdapter |
||
414 | ->moveNode(3, 4, TreeAdapter::PLACEMENT_TOP); |
||
415 | |||
416 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.php'); |
||
417 | |||
418 | // test |
||
419 | $this->treeAdapter |
||
420 | ->moveNode(19, 12, TreeAdapter::PLACEMENT_TOP); |
||
421 | |||
422 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementTop-1.php'); |
||
423 | |||
424 | // test |
||
425 | $this->treeAdapter |
||
426 | ->moveNode(10, 18, TreeAdapter::PLACEMENT_TOP); |
||
427 | |||
428 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementTop-2.php'); |
||
429 | |||
430 | // test |
||
431 | $this->treeAdapter |
||
432 | ->moveNode(21, 6, TreeAdapter::PLACEMENT_TOP); |
||
433 | |||
434 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementTop-3.php'); |
||
435 | } |
||
436 | |||
437 | public function testMoveNodePlacementChildBottom() |
||
438 | { |
||
439 | // test source node is already at required position |
||
440 | $this->treeAdapter |
||
441 | ->moveNode(22, 18, TreeAdapter::PLACEMENT_CHILD_BOTTOM); |
||
442 | |||
443 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.php'); |
||
444 | |||
445 | // test |
||
446 | $this->treeAdapter |
||
447 | ->moveNode(9, 12, TreeAdapter::PLACEMENT_CHILD_BOTTOM); |
||
448 | |||
449 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementChildBottom-1.php'); |
||
450 | |||
451 | // test |
||
452 | $this->treeAdapter |
||
453 | ->moveNode(10, 3, TreeAdapter::PLACEMENT_CHILD_BOTTOM); |
||
454 | |||
455 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementChildBottom-2.php'); |
||
456 | |||
457 | // test |
||
458 | $this->treeAdapter |
||
459 | ->moveNode(21, 12, TreeAdapter::PLACEMENT_CHILD_BOTTOM); |
||
460 | |||
461 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementChildBottom-3.php'); |
||
462 | } |
||
463 | |||
464 | public function testMoveNodePlacementChildTopDefaultPlacement() |
||
465 | { |
||
466 | // test source node is already at required position |
||
467 | $this->treeAdapter |
||
468 | ->moveNode(21, 18); |
||
469 | |||
470 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.php'); |
||
471 | |||
472 | // test |
||
473 | $this->treeAdapter |
||
474 | ->moveNode(9, 21); |
||
475 | |||
476 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementChildTop-1.php'); |
||
477 | |||
478 | // test |
||
479 | $this->treeAdapter |
||
480 | ->moveNode(16, 3); |
||
481 | |||
482 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementChildTop-2.php'); |
||
483 | |||
484 | // test |
||
485 | $this->treeAdapter |
||
486 | ->moveNode(18, 3); |
||
487 | |||
488 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementChildTop-3.php'); |
||
489 | } |
||
490 | |||
491 | public function testGetAncestorsReturnEmptyArrayIfNodeDoesNotExist() |
||
492 | { |
||
493 | $return = $this->treeAdapter |
||
494 | ->getAncestorsQueryBuilder() |
||
495 | ->get(123456789); |
||
496 | |||
497 | $this->assertEquals(array(), $return); |
||
498 | } |
||
499 | |||
500 | public function testGetAncestorsReturnEmptyArrayIfNodeExistButHasNoPath() |
||
501 | { |
||
502 | $return = $this->treeAdapter |
||
503 | ->getAncestorsQueryBuilder() |
||
504 | ->excludeLastNLevel(1) |
||
505 | ->get(1); |
||
506 | |||
507 | $this->assertEquals(array(), $return); |
||
508 | } |
||
509 | |||
510 | public function testGetAncestor() |
||
511 | { |
||
512 | // test |
||
513 | $return = $this->treeAdapter |
||
514 | ->getAncestorsQueryBuilder() |
||
515 | ->get(6); |
||
516 | |||
517 | $expected = array( |
||
518 | array( |
||
519 | 'tree_traversal_id' => '1', |
||
520 | 'name' => null, |
||
521 | 'lft' => '1', |
||
522 | 'rgt' => '50', |
||
523 | 'parent_id' => null, |
||
524 | 'level' => '0', |
||
525 | ), |
||
526 | array( |
||
527 | 'tree_traversal_id' => '3', |
||
528 | 'name' => null, |
||
529 | 'lft' => '16', |
||
530 | 'rgt' => '35', |
||
531 | 'parent_id' => '1', |
||
532 | 'level' => '1', |
||
533 | ), |
||
534 | array( |
||
535 | 'tree_traversal_id' => '6', |
||
536 | 'name' => null, |
||
537 | 'lft' => '17', |
||
538 | 'rgt' => '32', |
||
539 | 'parent_id' => '3', |
||
540 | 'level' => '2', |
||
541 | ), |
||
542 | ); |
||
543 | $this->assertEquals($expected, $return); |
||
544 | |||
545 | // test |
||
546 | $return = $this->treeAdapter |
||
547 | ->getAncestorsQueryBuilder() |
||
548 | ->excludeFirstNLevel(1) |
||
549 | ->get(6); |
||
550 | |||
551 | $expected = array( |
||
552 | array( |
||
553 | 'tree_traversal_id' => '3', |
||
554 | 'name' => null, |
||
555 | 'lft' => '16', |
||
556 | 'rgt' => '35', |
||
557 | 'parent_id' => '1', |
||
558 | 'level' => '1', |
||
559 | ), |
||
560 | array( |
||
561 | 'tree_traversal_id' => '6', |
||
562 | 'name' => null, |
||
563 | 'lft' => '17', |
||
564 | 'rgt' => '32', |
||
565 | 'parent_id' => '3', |
||
566 | 'level' => '2', |
||
567 | ), |
||
568 | ); |
||
569 | $this->assertEquals($expected, $return); |
||
570 | |||
571 | // test |
||
572 | $return = $this->treeAdapter |
||
573 | ->getAncestorsQueryBuilder() |
||
574 | ->excludeLastNLevel(1) |
||
575 | ->get(6); |
||
576 | |||
577 | $expected = array( |
||
578 | array( |
||
579 | 'tree_traversal_id' => '1', |
||
580 | 'name' => null, |
||
581 | 'lft' => '1', |
||
582 | 'rgt' => '50', |
||
583 | 'parent_id' => null, |
||
584 | 'level' => '0', |
||
585 | ), |
||
586 | array( |
||
587 | 'tree_traversal_id' => '3', |
||
588 | 'name' => null, |
||
589 | 'lft' => '16', |
||
590 | 'rgt' => '35', |
||
591 | 'parent_id' => '1', |
||
592 | 'level' => '1', |
||
593 | ), |
||
594 | ); |
||
595 | $this->assertEquals($expected, $return); |
||
596 | } |
||
597 | |||
598 | public function testGetAncestorAsNestedArray() |
||
599 | { |
||
600 | $return = $this->treeAdapter |
||
601 | ->getAncestorsQueryBuilder() |
||
602 | ->get(6, true); |
||
603 | |||
604 | $expected = array( |
||
605 | array( |
||
606 | 'tree_traversal_id' => '1', |
||
607 | 'name' => null, |
||
608 | 'lft' => '1', |
||
609 | 'rgt' => '50', |
||
610 | 'parent_id' => null, |
||
611 | 'level' => '0', |
||
612 | '_children' => array( |
||
613 | array( |
||
614 | 'tree_traversal_id' => '3', |
||
615 | 'name' => null, |
||
616 | 'lft' => '16', |
||
617 | 'rgt' => '35', |
||
618 | 'parent_id' => '1', |
||
619 | 'level' => '1', |
||
620 | '_children' => array( |
||
621 | array( |
||
622 | 'tree_traversal_id' => '6', |
||
623 | 'name' => null, |
||
624 | 'lft' => '17', |
||
625 | 'rgt' => '32', |
||
626 | 'parent_id' => '3', |
||
627 | 'level' => '2', |
||
628 | '_children' => array(), |
||
629 | ), |
||
630 | ), |
||
631 | ), |
||
632 | ), |
||
633 | ), |
||
634 | ); |
||
635 | $this->assertEquals($expected, $return); |
||
636 | } |
||
637 | |||
638 | public function testGetDescendantsReturnEmptyArrayIfNodeDoesNotExist() |
||
639 | { |
||
640 | $return = $this->treeAdapter |
||
641 | ->getDescendantsQueryBuilder() |
||
642 | ->get(123456789); |
||
643 | $this->assertEquals(array(), $return); |
||
644 | } |
||
645 | |||
646 | public function testGetDescendantsReturnEmptyArrayNodeDoesNotHaveDescendants() |
||
647 | { |
||
648 | $return = $this->treeAdapter |
||
649 | ->getDescendantsQueryBuilder() |
||
650 | ->excludeFirstNLevel(1) |
||
651 | ->get(8); |
||
652 | |||
653 | $this->assertEquals(array(), $return); |
||
654 | } |
||
655 | |||
656 | public function testGetDescendants() |
||
657 | { |
||
658 | // test whole branch |
||
659 | $return = $this->treeAdapter |
||
660 | ->getDescendantsQueryBuilder() |
||
661 | ->get(21); |
||
662 | |||
663 | $expected = array( |
||
664 | array( |
||
665 | 'tree_traversal_id' => '21', |
||
666 | 'name' => null, |
||
667 | 'lft' => '20', |
||
668 | 'rgt' => '25', |
||
669 | 'parent_id' => '18', |
||
670 | 'level' => '5', |
||
671 | ), |
||
672 | array( |
||
673 | 'tree_traversal_id' => '24', |
||
674 | 'name' => null, |
||
675 | 'lft' => '21', |
||
676 | 'rgt' => '22', |
||
677 | 'parent_id' => '21', |
||
678 | 'level' => '6', |
||
679 | ), |
||
680 | array( |
||
681 | 'tree_traversal_id' => '25', |
||
682 | 'name' => null, |
||
683 | 'lft' => '23', |
||
684 | 'rgt' => '24', |
||
685 | 'parent_id' => '21', |
||
686 | 'level' => '6', |
||
687 | ), |
||
688 | ); |
||
689 | $this->assertEquals($expected, $return); |
||
690 | |||
691 | // test exclude fist 3 levels |
||
692 | $return = $this->treeAdapter |
||
693 | ->getDescendantsQueryBuilder() |
||
694 | ->excludeFirstNLevel(3) |
||
695 | ->get(6); |
||
696 | |||
697 | $expected = array( |
||
698 | array( |
||
699 | 'tree_traversal_id' => '21', |
||
700 | 'name' => null, |
||
701 | 'lft' => '20', |
||
702 | 'rgt' => '25', |
||
703 | 'parent_id' => '18', |
||
704 | 'level' => '5', |
||
705 | ), |
||
706 | array( |
||
707 | 'tree_traversal_id' => '24', |
||
708 | 'name' => null, |
||
709 | 'lft' => '21', |
||
710 | 'rgt' => '22', |
||
711 | 'parent_id' => '21', |
||
712 | 'level' => '6', |
||
713 | ), |
||
714 | array( |
||
715 | 'tree_traversal_id' => '25', |
||
716 | 'name' => null, |
||
717 | 'lft' => '23', |
||
718 | 'rgt' => '24', |
||
719 | 'parent_id' => '21', |
||
720 | 'level' => '6', |
||
721 | ), |
||
722 | array( |
||
723 | 'tree_traversal_id' => '22', |
||
724 | 'name' => null, |
||
725 | 'lft' => '26', |
||
726 | 'rgt' => '27', |
||
727 | 'parent_id' => '18', |
||
728 | 'level' => '5', |
||
729 | ), |
||
730 | ); |
||
731 | $this->assertEquals($expected, $return); |
||
732 | |||
733 | // test limit depth |
||
734 | $return = $this->treeAdapter |
||
735 | ->getDescendantsQueryBuilder() |
||
736 | ->levelLimit(2) |
||
737 | ->get(18); |
||
738 | |||
739 | $expected = array( |
||
740 | array( |
||
741 | 'tree_traversal_id' => '18', |
||
742 | 'name' => null, |
||
743 | 'lft' => '19', |
||
744 | 'rgt' => '28', |
||
745 | 'parent_id' => '12', |
||
746 | 'level' => '4', |
||
747 | ), |
||
748 | array( |
||
749 | 'tree_traversal_id' => '21', |
||
750 | 'name' => null, |
||
751 | 'lft' => '20', |
||
752 | 'rgt' => '25', |
||
753 | 'parent_id' => '18', |
||
754 | 'level' => '5', |
||
755 | ), |
||
756 | array( |
||
757 | 'tree_traversal_id' => '22', |
||
758 | 'name' => null, |
||
759 | 'lft' => '26', |
||
760 | 'rgt' => '27', |
||
761 | 'parent_id' => '18', |
||
762 | 'level' => '5', |
||
763 | ), |
||
764 | ); |
||
765 | $this->assertEquals($expected, $return); |
||
766 | |||
767 | // test exclude node |
||
768 | $return = $this->treeAdapter |
||
769 | ->getDescendantsQueryBuilder() |
||
770 | ->excludeBranch(21) |
||
771 | ->get(12); |
||
772 | |||
773 | $expected = array( |
||
774 | array( |
||
775 | 'tree_traversal_id' => '12', |
||
776 | 'name' => null, |
||
777 | 'lft' => '18', |
||
778 | 'rgt' => '29', |
||
779 | 'parent_id' => '6', |
||
780 | 'level' => '3', |
||
781 | ), |
||
782 | array( |
||
783 | 'tree_traversal_id' => '18', |
||
784 | 'name' => null, |
||
785 | 'lft' => '19', |
||
786 | 'rgt' => '28', |
||
787 | 'parent_id' => '12', |
||
788 | 'level' => '4', |
||
789 | ), |
||
790 | array( |
||
791 | 'tree_traversal_id' => '22', |
||
792 | 'name' => null, |
||
793 | 'lft' => '26', |
||
794 | 'rgt' => '27', |
||
795 | 'parent_id' => '18', |
||
796 | 'level' => '5', |
||
797 | ), |
||
798 | ); |
||
799 | $this->assertEquals($expected, $return); |
||
800 | } |
||
801 | |||
802 | public function testGetDescendantsAsNestedArray() |
||
803 | { |
||
804 | $return = $this->treeAdapter |
||
805 | ->getDescendantsQueryBuilder() |
||
806 | ->get(21, true); |
||
807 | |||
808 | $expected = array( |
||
809 | array( |
||
810 | 'tree_traversal_id' => '21', |
||
811 | 'name' => null, |
||
812 | 'lft' => '20', |
||
813 | 'rgt' => '25', |
||
814 | 'parent_id' => '18', |
||
815 | 'level' => '5', |
||
816 | '_children' => array( |
||
817 | array( |
||
818 | 'tree_traversal_id' => '24', |
||
819 | 'name' => null, |
||
820 | 'lft' => '21', |
||
821 | 'rgt' => '22', |
||
822 | 'parent_id' => '21', |
||
823 | 'level' => '6', |
||
824 | '_children' => array(), |
||
825 | ), |
||
826 | array( |
||
827 | 'tree_traversal_id' => '25', |
||
828 | 'name' => null, |
||
829 | 'lft' => '23', |
||
830 | 'rgt' => '24', |
||
831 | 'parent_id' => '21', |
||
832 | 'level' => '6', |
||
833 | '_children' => array(), |
||
834 | ), |
||
835 | ), |
||
836 | ), |
||
837 | ); |
||
838 | $this->assertEquals($expected, $return); |
||
839 | } |
||
840 | |||
841 | public function testGetChildrenReturnEmptyArrayIfNodeDoesNotExist() |
||
842 | { |
||
843 | $return = $this->treeAdapter |
||
844 | ->getDescendantsQueryBuilder() |
||
845 | ->excludeFirstNLevel(1) |
||
846 | ->levelLimit(1) |
||
847 | ->get(123456789); |
||
848 | |||
849 | $this->assertEquals(array(), $return); |
||
850 | } |
||
851 | |||
852 | public function testGetChildrenReturnEmptyArrayIfNodeDoesNotHaveChildren() |
||
853 | { |
||
854 | $return = $this->treeAdapter |
||
855 | ->getDescendantsQueryBuilder() |
||
856 | ->excludeFirstNLevel(1) |
||
857 | ->levelLimit(1) |
||
858 | ->get(8); |
||
859 | |||
860 | $this->assertEquals(array(), $return); |
||
861 | } |
||
862 | |||
863 | public function testGetChildren() |
||
864 | { |
||
865 | // test exclude node |
||
866 | $return = $this->treeAdapter |
||
867 | ->getDescendantsQueryBuilder() |
||
868 | ->levelLimit(1) |
||
869 | ->excludeFirstNLevel(1) |
||
870 | ->get(18); |
||
871 | |||
872 | $expected = array( |
||
873 | array( |
||
874 | 'tree_traversal_id' => '21', |
||
875 | 'name' => null, |
||
876 | 'lft' => '20', |
||
877 | 'rgt' => '25', |
||
878 | 'parent_id' => '18', |
||
879 | 'level' => '5', |
||
880 | ), |
||
881 | array( |
||
882 | 'tree_traversal_id' => '22', |
||
883 | 'name' => null, |
||
884 | 'lft' => '26', |
||
885 | 'rgt' => '27', |
||
886 | 'parent_id' => '18', |
||
887 | 'level' => '5', |
||
888 | ), |
||
889 | ); |
||
890 | $this->assertEquals($expected, $return); |
||
891 | } |
||
892 | |||
893 | public function testUpdateNode() |
||
894 | { |
||
895 | // test |
||
896 | $data = array( |
||
897 | 'name' => 'ahoj', |
||
898 | ); |
||
899 | $this->treeAdapter |
||
900 | ->updateNode(3, $data); |
||
901 | |||
902 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testUpdateNode-1.php'); |
||
903 | |||
904 | // test |
||
905 | $data = array( |
||
906 | 'name' => 'ahoj', |
||
907 | 'lft' => '123456', |
||
908 | 'rgt' => '123456', |
||
909 | 'tree_traversal_id' => '123456', |
||
910 | 'level' => '123456', |
||
911 | 'parent_id' => '123456', |
||
912 | ); |
||
913 | $this->treeAdapter |
||
914 | ->updateNode(3, $data); |
||
915 | |||
916 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testUpdateNode-1.php'); |
||
917 | } |
||
918 | |||
919 | public function testGetRootNodeRootDoesNotExist() |
||
920 | { |
||
921 | $return = $this->treeAdapter |
||
922 | ->getRootNode(); |
||
923 | |||
924 | $this->assertEquals(array(), $return); |
||
925 | } |
||
926 | |||
927 | public function testGetRootNode() |
||
928 | { |
||
929 | $return = $this->treeAdapter |
||
930 | ->getRootNode(); |
||
931 | |||
932 | $expected = array( |
||
933 | 'tree_traversal_id' => '1', |
||
934 | 'name' => '', |
||
935 | 'lft' => '1', |
||
936 | 'rgt' => '50', |
||
937 | 'parent_id' => null, |
||
938 | 'level' => '0', |
||
939 | ); |
||
940 | $this->assertEquals($expected, $return); |
||
941 | } |
||
942 | } |
||
943 |