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