1
|
|
|
<?php |
2
|
|
|
namespace StefanoTreeTest\Integration; |
3
|
|
|
|
4
|
|
|
use StefanoTree\NestedSet as TreeAdapter; |
5
|
|
|
use StefanoTreeTest\IntegrationTestCase; |
6
|
|
|
|
7
|
|
|
abstract class AbstractScopeTest |
8
|
|
|
extends IntegrationTestCase |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var TreeAdapter |
12
|
|
|
*/ |
13
|
|
|
protected $treeAdapter; |
14
|
|
|
|
15
|
|
|
protected function setUp() |
16
|
|
|
{ |
17
|
|
|
$this->treeAdapter = $this->getTreeAdapter(); |
18
|
|
|
|
19
|
|
|
parent::setUp(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function tearDown() |
23
|
|
|
{ |
24
|
|
|
$this->treeAdapter = null; |
25
|
|
|
parent::tearDown(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return TreeAdapter |
30
|
|
|
*/ |
31
|
|
|
abstract protected function getTreeAdapter(); |
32
|
|
|
|
33
|
|
|
protected function getDataSet() |
34
|
|
|
{ |
35
|
|
|
switch ($this->getName()) { |
36
|
|
|
case 'testInvalidTree': |
37
|
|
|
case 'testRebuildTree': |
38
|
|
|
return $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/initDataSetBrokenTreeIndexes.xml'); |
39
|
|
|
default: |
40
|
|
|
return $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/initDataSet.xml'); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testCreateRoot() |
45
|
|
|
{ |
46
|
|
|
$this->treeAdapter |
47
|
|
|
->createRootNode(array(), 10); |
48
|
|
|
|
49
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
50
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testCreateRoot.xml'); |
51
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testCreateRootRootWithSomeScopeAlreadyExist() |
55
|
|
|
{ |
56
|
|
|
$this->expectException( |
57
|
|
|
'\StefanoTree\Exception\RootNodeAlreadyExistException' |
58
|
|
|
); |
59
|
|
|
$this->expectExceptionMessage('Root node already exist'); |
60
|
|
|
|
61
|
|
|
$this->treeAdapter |
62
|
|
|
->createRootNode(array(), 123); |
63
|
|
|
$this->treeAdapter |
64
|
|
|
->createRootNode(array(), 123); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testGetRoots() |
68
|
|
|
{ |
69
|
|
|
$expected = array( |
70
|
|
|
array( |
71
|
|
|
'tree_traversal_id' => 1, |
72
|
|
|
'name' => null, |
73
|
|
|
'lft' => 1, |
74
|
|
|
'rgt' => 10, |
75
|
|
|
'parent_id' => 0, |
76
|
|
|
'level' => 0, |
77
|
|
|
'scope' => 2, |
78
|
|
|
), |
79
|
|
|
array( |
80
|
|
|
'tree_traversal_id' => 6, |
81
|
|
|
'name' => null, |
82
|
|
|
'lft' => 1, |
83
|
|
|
'rgt' => 6, |
84
|
|
|
'parent_id' => 0, |
85
|
|
|
'level' => 0, |
86
|
|
|
'scope' => 1, |
87
|
|
|
), |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$roots = $this->treeAdapter |
91
|
|
|
->getRoots(); |
92
|
|
|
|
93
|
|
|
$this->assertEquals($expected, $roots); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testAddNodePlacementChildTop() |
97
|
|
|
{ |
98
|
|
|
$lastGeneratedValue = $this->treeAdapter |
99
|
|
|
->addNodePlacementChildTop(1); |
100
|
|
|
|
101
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
102
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testAddNodePlacementChildTop.xml'); |
103
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
104
|
|
|
$this->assertEquals(9, $lastGeneratedValue); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testMoveNodePlacementBottom() |
108
|
|
|
{ |
109
|
|
|
$this->treeAdapter |
110
|
|
|
->moveNodePlacementBottom(3, 5); |
111
|
|
|
|
112
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
113
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testMoveNodePlacementBottom.xml'); |
114
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testCannotMoveNodeBetweenScopes() |
118
|
|
|
{ |
119
|
|
|
$this->expectException( |
120
|
|
|
'\StefanoTree\Exception\InvalidArgumentException', |
121
|
|
|
'Cannot move node between scopes' |
|
|
|
|
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$this->treeAdapter |
125
|
|
|
->moveNodePlacementChildBottom(4, 8); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testDeleteBranch() |
129
|
|
|
{ |
130
|
|
|
$this->treeAdapter |
131
|
|
|
->deleteBranch(2); |
132
|
|
|
|
133
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
134
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testDeleteBranch.xml'); |
135
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testGetDescendants() |
139
|
|
|
{ |
140
|
|
|
$expectedNodeData = array( |
141
|
|
|
array( |
142
|
|
|
'tree_traversal_id' => '2', |
143
|
|
|
'name' => null, |
144
|
|
|
'lft' => '2', |
145
|
|
|
'rgt' => '9', |
146
|
|
|
'parent_id' => '1', |
147
|
|
|
'level' => '1', |
148
|
|
|
'scope' => '2', |
149
|
|
|
), |
150
|
|
|
array( |
151
|
|
|
'tree_traversal_id' => '3', |
152
|
|
|
'name' => null, |
153
|
|
|
'lft' => '3', |
154
|
|
|
'rgt' => '4', |
155
|
|
|
'parent_id' => '2', |
156
|
|
|
'level' => '2', |
157
|
|
|
'scope' => '2', |
158
|
|
|
), |
159
|
|
|
array( |
160
|
|
|
'tree_traversal_id' => '4', |
161
|
|
|
'name' => null, |
162
|
|
|
'lft' => '5', |
163
|
|
|
'rgt' => '6', |
164
|
|
|
'parent_id' => '2', |
165
|
|
|
'level' => '2', |
166
|
|
|
'scope' => '2', |
167
|
|
|
), |
168
|
|
|
array( |
169
|
|
|
'tree_traversal_id' => '5', |
170
|
|
|
'name' => null, |
171
|
|
|
'lft' => '7', |
172
|
|
|
'rgt' => '8', |
173
|
|
|
'parent_id' => '2', |
174
|
|
|
'level' => '2', |
175
|
|
|
'scope' => '2', |
176
|
|
|
), |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
$nodeData = $this->treeAdapter |
180
|
|
|
->getDescendants(2); |
181
|
|
|
$this->assertEquals($expectedNodeData, $nodeData); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function testGetPath() |
185
|
|
|
{ |
186
|
|
|
$expectedNodeData = array( |
187
|
|
|
array( |
188
|
|
|
'tree_traversal_id' => '1', |
189
|
|
|
'name' => null, |
190
|
|
|
'lft' => '1', |
191
|
|
|
'rgt' => '10', |
192
|
|
|
'parent_id' => '0', |
193
|
|
|
'level' => '0', |
194
|
|
|
'scope' => '2', |
195
|
|
|
), |
196
|
|
|
array( |
197
|
|
|
'tree_traversal_id' => '2', |
198
|
|
|
'name' => null, |
199
|
|
|
'lft' => '2', |
200
|
|
|
'rgt' => '9', |
201
|
|
|
'parent_id' => '1', |
202
|
|
|
'level' => '1', |
203
|
|
|
'scope' => '2', |
204
|
|
|
), |
205
|
|
|
array( |
206
|
|
|
'tree_traversal_id' => '5', |
207
|
|
|
'name' => null, |
208
|
|
|
'lft' => '7', |
209
|
|
|
'rgt' => '8', |
210
|
|
|
'parent_id' => '2', |
211
|
|
|
'level' => '2', |
212
|
|
|
'scope' => '2', |
213
|
|
|
), |
214
|
|
|
); |
215
|
|
|
|
216
|
|
|
$nodeData = $this->treeAdapter |
217
|
|
|
->getPath(5); |
218
|
|
|
$this->assertEquals($expectedNodeData, $nodeData); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testUpdateCannotCorruptTreeStructure() |
222
|
|
|
{ |
223
|
|
|
$excepted = array( |
224
|
|
|
'tree_traversal_id' => 4, |
225
|
|
|
'name' => 'updated', |
226
|
|
|
'lft' => 5, |
227
|
|
|
'rgt' => 6, |
228
|
|
|
'parent_id' => 2, |
229
|
|
|
'level' => 2, |
230
|
|
|
'scope' => 2, |
231
|
|
|
); |
232
|
|
|
|
233
|
|
|
$data = array( |
234
|
|
|
'tree_traversal_id' => 'corrupt data', |
235
|
|
|
'name' => 'updated', |
236
|
|
|
'lft' => 'corrupt data', |
237
|
|
|
'rgt' => 'corrupt data', |
238
|
|
|
'parent_id' => 'corrupt data', |
239
|
|
|
'level' => 'corrupt data', |
240
|
|
|
'scope' => 'corrupt data', |
241
|
|
|
); |
242
|
|
|
$this->treeAdapter |
243
|
|
|
->updateNode(4, $data); |
244
|
|
|
|
245
|
|
|
$this->assertEquals($excepted, $this->treeAdapter->getNode(4)); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function testIsTreeValid() |
249
|
|
|
{ |
250
|
|
|
$this->assertTrue($this->treeAdapter->isValid(1)); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function testInvalidTree() |
254
|
|
|
{ |
255
|
|
|
$this->assertFalse($this->treeAdapter->isValid(1)); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testRebuildTree() |
259
|
|
|
{ |
260
|
|
|
$this->treeAdapter |
261
|
|
|
->rebuild(1); |
262
|
|
|
|
263
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
264
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testRebuildTree.xml'); |
265
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|