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