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