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