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