1
|
|
|
<?php |
2
|
|
|
namespace StefanoTreeTest\Integration\Adapter; |
3
|
|
|
|
4
|
|
|
use StefanoTree\NestedSet\Adapter\AdapterInterface as TreeAdapterInterface; |
5
|
|
|
use StefanoTree\NestedSet\NodeInfo; |
6
|
|
|
use StefanoTreeTest\IntegrationTestCase; |
7
|
|
|
|
8
|
|
|
abstract class AdapterTestAbstract |
9
|
|
|
extends IntegrationTestCase |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var TreeAdapterInterface |
13
|
|
|
*/ |
14
|
|
|
protected $adapter; |
15
|
|
|
|
16
|
|
|
protected function setUp() |
17
|
|
|
{ |
18
|
|
|
$this->adapter = $this->getAdapter(); |
19
|
|
|
|
20
|
|
|
parent::setUp(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function tearDown() |
24
|
|
|
{ |
25
|
|
|
$this->adapter = null; |
26
|
|
|
parent::tearDown(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return TreeAdapterInterface |
31
|
|
|
*/ |
32
|
|
|
abstract protected function getAdapter(); |
33
|
|
|
|
34
|
|
|
public function testLockTreeDoesNotFail() |
35
|
|
|
{ |
36
|
|
|
$this->adapter |
37
|
|
|
->lockTree(null); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testDbTransactionDoesNotFail() |
41
|
|
|
{ |
42
|
|
|
$this->adapter |
43
|
|
|
->beginTransaction(); |
44
|
|
|
$this->adapter |
45
|
|
|
->commitTransaction(); |
46
|
|
|
|
47
|
|
|
$this->adapter |
48
|
|
|
->beginTransaction(); |
49
|
|
|
$this->adapter |
50
|
|
|
->rollbackTransaction(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testUpdateData() |
54
|
|
|
{ |
55
|
|
|
$this->adapter |
56
|
|
|
->update(2, array('name' => 'changed')); |
57
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal')); |
58
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/testUpdateData.xml'); |
59
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testUpdateDataDoesNotChangeMetadata() |
63
|
|
|
{ |
64
|
|
|
$data = array( |
65
|
|
|
'name' => 'changed', |
66
|
|
|
'lft' => 'a', |
67
|
|
|
'rgt' => 'b', |
68
|
|
|
'parent_id' => 'c', |
69
|
|
|
'level' => 'd', |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$this->adapter |
73
|
|
|
->update(2, $data); |
74
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal')); |
75
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/testUpdateData.xml'); |
76
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testInsertData() |
80
|
|
|
{ |
81
|
|
|
$nodeInfo = new NodeInfo(null, 6, 100, 1000, 1001, null); |
82
|
|
|
|
83
|
|
|
$this->adapter |
84
|
|
|
->insert($nodeInfo, array('name' => 'some-name')); |
85
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal')); |
86
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/testInsertData.xml'); |
87
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testInsertDataDoesNotChangeMetadata() |
91
|
|
|
{ |
92
|
|
|
$nodeInfo = new NodeInfo(null, 6, 100, 1000, 1001, null); |
93
|
|
|
|
94
|
|
|
$data = array( |
95
|
|
|
'name' => 'some-name', |
96
|
|
|
'lft' => 'a', |
97
|
|
|
'rgt' => 'b', |
98
|
|
|
'parent_id' => 'c', |
99
|
|
|
'level' => 'd', |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$this->adapter |
103
|
|
|
->insert($nodeInfo, $data); |
104
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal')); |
105
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/testInsertData.xml'); |
106
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testDeleteBranch() |
110
|
|
|
{ |
111
|
|
|
$this->adapter |
112
|
|
|
->delete(16, 35); |
113
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal')); |
114
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/testDeleteBranch.xml'); |
115
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testMoveLeftIndexes() |
119
|
|
|
{ |
120
|
|
|
$this->adapter |
121
|
|
|
->moveLeftIndexes(12, 500); |
122
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal')); |
123
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/testMoveLeftIndexes.xml'); |
124
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testMoveRightIndexes() |
128
|
|
|
{ |
129
|
|
|
$this->adapter |
130
|
|
|
->moveRightIndexes(15, 500); |
131
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal')); |
132
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/testMoveRightIndexes.xml'); |
133
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testUpdateParentId() |
137
|
|
|
{ |
138
|
|
|
$this->adapter |
139
|
|
|
->updateParentId(3, 22); |
140
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal')); |
141
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/testUpdateParentId.xml'); |
142
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testUpdateLevels() |
146
|
|
|
{ |
147
|
|
|
$this->adapter |
148
|
|
|
->updateLevels(16, 35, 500); |
149
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal')); |
150
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/testUpdateLevels.xml'); |
151
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testMoveBranch() |
155
|
|
|
{ |
156
|
|
|
$this->adapter |
157
|
|
|
->moveBranch(17, 32, 500); |
158
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal')); |
159
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/testMoveBranch.xml'); |
160
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testGetRoots() |
164
|
|
|
{ |
165
|
|
|
$roots = $this->adapter |
166
|
|
|
->getRoots(); |
167
|
|
|
|
168
|
|
|
$expected = include __DIR__ . '/_files/adapter/testGetRoots.php'; |
169
|
|
|
$this->assertEquals($expected, $roots); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testGetRoot() |
173
|
|
|
{ |
174
|
|
|
$roots = $this->adapter |
175
|
|
|
->getRoot(); |
176
|
|
|
|
177
|
|
|
$expected = include __DIR__ . '/_files/adapter/testGetRoot.php'; |
178
|
|
|
$this->assertEquals($expected, $roots); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testGetNodeReturnNullIfNodeDoesNotExist() |
182
|
|
|
{ |
183
|
|
|
$node = $this->adapter |
184
|
|
|
->getNode(1000000); |
185
|
|
|
$this->assertNull($node); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testGetNode() |
189
|
|
|
{ |
190
|
|
|
$node = $this->adapter |
191
|
|
|
->getNode(11); |
192
|
|
|
|
193
|
|
|
$expected = include __DIR__ . '/_files/adapter/testGetNode.php'; |
194
|
|
|
$this->assertEquals($expected, $node); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function testGetNodeInfoReturnNullIfNodeInfoDoesNotExist() |
198
|
|
|
{ |
199
|
|
|
$nodeInfo = $this->adapter |
200
|
|
|
->getNodeInfo(10000000); |
201
|
|
|
$this->assertNull($nodeInfo); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function testGetNodeInfo() |
205
|
|
|
{ |
206
|
|
|
$nodeInfo = $this->adapter |
207
|
|
|
->getNodeInfo(10); |
208
|
|
|
|
209
|
|
|
$this->assertEquals($nodeInfo->getId(), 10); |
210
|
|
|
$this->assertEquals($nodeInfo->getParentId(), 5); |
211
|
|
|
$this->assertEquals($nodeInfo->getLeft(), 4); |
212
|
|
|
$this->assertEquals($nodeInfo->getRight(), 11); |
213
|
|
|
$this->assertEquals($nodeInfo->getLevel(), 3); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testGetChildrenNodeInfoReturnEmptyArrayIfNodeDoesNotHaveChildrenNodes() |
217
|
|
|
{ |
218
|
|
|
$nodeInfo = $this->adapter |
219
|
|
|
->getChildrenNodeInfo(7); |
220
|
|
|
|
221
|
|
|
$this->assertEquals(array(), $nodeInfo); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function testGetChildrenNodeInfo() |
225
|
|
|
{ |
226
|
|
|
$nodeInfo = $this->adapter |
227
|
|
|
->getChildrenNodeInfo(4); |
228
|
|
|
|
229
|
|
|
$this->assertCount(2, $nodeInfo); |
230
|
|
|
|
231
|
|
|
// check first node info |
232
|
|
|
$this->assertEquals($nodeInfo[0]->getId(), 8); |
233
|
|
|
$this->assertEquals($nodeInfo[0]->getParentId(), 4); |
234
|
|
|
$this->assertEquals($nodeInfo[0]->getLeft(), 37); |
235
|
|
|
$this->assertEquals($nodeInfo[0]->getRight(), 38); |
236
|
|
|
$this->assertEquals($nodeInfo[0]->getLevel(), 2); |
237
|
|
|
|
238
|
|
|
// check second node info |
239
|
|
|
$this->assertEquals($nodeInfo[1]->getId(), 9); |
240
|
|
|
$this->assertEquals($nodeInfo[1]->getParentId(), 4); |
241
|
|
|
$this->assertEquals($nodeInfo[1]->getLeft(), 39); |
242
|
|
|
$this->assertEquals($nodeInfo[1]->getRight(), 48); |
243
|
|
|
$this->assertEquals($nodeInfo[1]->getLevel(), 2); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function testUpdateNodeMetadata() |
247
|
|
|
{ |
248
|
|
|
$nodeInfo = new NodeInfo(2, 100, 101, 102, 103, null); |
249
|
|
|
|
250
|
|
|
$this->adapter |
251
|
|
|
->updateNodeMetadata($nodeInfo); |
252
|
|
|
|
253
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal')); |
254
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/testUpdateNodeMetadata.xml'); |
255
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testGetPathReturnNullEmptyIfNodeDoestNotExist() // todo return array |
259
|
|
|
{ |
260
|
|
|
$path = $this->adapter |
261
|
|
|
->getPath(1000); |
262
|
|
|
|
263
|
|
|
$this->assertNull($path); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function testGetPath() |
267
|
|
|
{ |
268
|
|
|
$path = $this->adapter |
269
|
|
|
->getPath(10); |
270
|
|
|
|
271
|
|
|
$this->assertCount(4, $path); |
272
|
|
|
|
273
|
|
|
$expected = include __DIR__ . '/_files/adapter/testGetPath.php'; |
274
|
|
|
$this->assertEquals($expected, $path); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function testGetPathFromLevel() |
278
|
|
|
{ |
279
|
|
|
$path = $this->adapter |
280
|
|
|
->getPath(10, 2); |
281
|
|
|
|
282
|
|
|
$this->assertCount(2, $path); |
283
|
|
|
|
284
|
|
|
$expected = include __DIR__ . '/_files/adapter/testGetPathStartFromLevel.php'; |
285
|
|
|
$this->assertEquals($expected, $path); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public function testGetPathExcludeLastNode() |
289
|
|
|
{ |
290
|
|
|
$path = $this->adapter |
291
|
|
|
->getPath(10, 0, True); |
292
|
|
|
|
293
|
|
|
$this->assertCount(3, $path); |
294
|
|
|
|
295
|
|
|
$expected = include __DIR__ . '/_files/adapter/testGetPathExcludeLastNode.php'; |
296
|
|
|
$this->assertEquals($expected, $path); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function testGetDescendantsReturnNullIfNodeDoesNotExist() // todo return array |
300
|
|
|
{ |
301
|
|
|
$nodes = $this->adapter |
302
|
|
|
->getDescendants(1000); |
303
|
|
|
|
304
|
|
|
$this->assertNull($nodes); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function testGetDescendants() |
308
|
|
|
{ |
309
|
|
|
$nodes = $this->adapter |
310
|
|
|
->getDescendants(); |
311
|
|
|
|
312
|
|
|
$this->assertCount(25, $nodes); |
313
|
|
|
|
314
|
|
|
$expected = include __DIR__ . '/_files/adapter/testGetDescendants.php'; |
315
|
|
|
$this->assertEquals($expected, $nodes); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
public function testGetDescendantsDefinedNodeId() |
319
|
|
|
{ |
320
|
|
|
$nodes = $this->adapter |
321
|
|
|
->getDescendants(6); |
322
|
|
|
|
323
|
|
|
$this->assertCount(8, $nodes); |
324
|
|
|
|
325
|
|
|
$expected = include __DIR__ . '/_files/adapter/testGetDescendantsDefinedNodeId.php'; |
326
|
|
|
$this->assertEquals($expected, $nodes); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function testGetDescendantsFromLevel() |
330
|
|
|
{ |
331
|
|
|
$nodes = $this->adapter |
332
|
|
|
->getDescendants(6, 2); |
333
|
|
|
|
334
|
|
|
$this->assertCount(5, $nodes); |
335
|
|
|
|
336
|
|
|
$expected = include __DIR__ . '/_files/adapter/testGetDescendantsFromLevel.php'; |
337
|
|
|
$this->assertEquals($expected, $nodes); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
public function testGetDescendantsFixLevels() |
341
|
|
|
{ |
342
|
|
|
$nodes = $this->adapter |
343
|
|
|
->getDescendants(6, 2, 2); |
344
|
|
|
|
345
|
|
|
$this->assertCount(3, $nodes); |
346
|
|
|
|
347
|
|
|
$expected = include __DIR__ . '/_files/adapter/testGetDescendantsFixLevels.php'; |
348
|
|
|
$this->assertEquals($expected, $nodes); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
public function testGetDescendantsExcludeBranch() |
352
|
|
|
{ |
353
|
|
|
$nodes = $this->adapter |
354
|
|
|
->getDescendants(1, 0, null, 9); |
355
|
|
|
|
356
|
|
|
$this->assertCount(20, $nodes); |
357
|
|
|
|
358
|
|
|
$expected = include __DIR__ . '/_files/adapter/testGetDescendantsExcludeBranch.php'; |
359
|
|
|
$this->assertEquals($expected, $nodes); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|