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