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
|
|
|
return $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/initDataSet.xml'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testCreateRoot() |
39
|
|
|
{ |
40
|
|
|
$this->treeAdapter |
41
|
|
|
->createRootNode(array(), 10); |
42
|
|
|
|
43
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
44
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testCreateRoot.xml'); |
45
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testCreateRootRootWithSomeScopeAlreadyExist() |
49
|
|
|
{ |
50
|
|
|
$this->expectException( |
51
|
|
|
'\StefanoTree\Exception\RootNodeAlreadyExistException' |
52
|
|
|
); |
53
|
|
|
$this->expectExceptionMessage('Root node already exist'); |
54
|
|
|
|
55
|
|
|
$this->treeAdapter |
56
|
|
|
->createRootNode(array(), 123); |
57
|
|
|
$this->treeAdapter |
58
|
|
|
->createRootNode(array(), 123); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testGetRoots() |
62
|
|
|
{ |
63
|
|
|
$expected = array( |
64
|
|
|
array( |
65
|
|
|
'tree_traversal_id' => 1, |
66
|
|
|
'name' => null, |
67
|
|
|
'lft' => 1, |
68
|
|
|
'rgt' => 10, |
69
|
|
|
'parent_id' => 0, |
70
|
|
|
'level' => 0, |
71
|
|
|
'scope' => 2, |
72
|
|
|
), |
73
|
|
|
array( |
74
|
|
|
'tree_traversal_id' => 6, |
75
|
|
|
'name' => null, |
76
|
|
|
'lft' => 1, |
77
|
|
|
'rgt' => 6, |
78
|
|
|
'parent_id' => 0, |
79
|
|
|
'level' => 0, |
80
|
|
|
'scope' => 1, |
81
|
|
|
), |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$roots = $this->treeAdapter |
85
|
|
|
->getRoots(); |
86
|
|
|
|
87
|
|
|
$this->assertEquals($expected, $roots); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testAddNodePlacementChildTop() |
91
|
|
|
{ |
92
|
|
|
$lastGeneratedValue = $this->treeAdapter |
93
|
|
|
->addNodePlacementChildTop(1); |
94
|
|
|
|
95
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
96
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testAddNodePlacementChildTop.xml'); |
97
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
98
|
|
|
$this->assertEquals(9, $lastGeneratedValue); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testMoveNodePlacementBottom() |
102
|
|
|
{ |
103
|
|
|
$this->treeAdapter |
104
|
|
|
->moveNodePlacementBottom(3, 5); |
105
|
|
|
|
106
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
107
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testMoveNodePlacementBottom.xml'); |
108
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testCannotMoveNodeBetweenScopes() |
112
|
|
|
{ |
113
|
|
|
$this->expectException( |
114
|
|
|
'\StefanoTree\Exception\InvalidArgumentException', |
115
|
|
|
'Cannot move node between scopes' |
|
|
|
|
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$this->treeAdapter |
119
|
|
|
->moveNodePlacementChildBottom(4, 8); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testDeleteBranch() |
123
|
|
|
{ |
124
|
|
|
$this->treeAdapter |
125
|
|
|
->deleteBranch(2); |
126
|
|
|
|
127
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
128
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testDeleteBranch.xml'); |
129
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testGetDescendants() |
133
|
|
|
{ |
134
|
|
|
$expectedNodeData = array( |
135
|
|
|
array( |
136
|
|
|
'tree_traversal_id' => '2', |
137
|
|
|
'name' => null, |
138
|
|
|
'lft' => '2', |
139
|
|
|
'rgt' => '9', |
140
|
|
|
'parent_id' => '1', |
141
|
|
|
'level' => '1', |
142
|
|
|
'scope' => '2', |
143
|
|
|
), |
144
|
|
|
array( |
145
|
|
|
'tree_traversal_id' => '3', |
146
|
|
|
'name' => null, |
147
|
|
|
'lft' => '3', |
148
|
|
|
'rgt' => '4', |
149
|
|
|
'parent_id' => '2', |
150
|
|
|
'level' => '2', |
151
|
|
|
'scope' => '2', |
152
|
|
|
), |
153
|
|
|
array( |
154
|
|
|
'tree_traversal_id' => '4', |
155
|
|
|
'name' => null, |
156
|
|
|
'lft' => '5', |
157
|
|
|
'rgt' => '6', |
158
|
|
|
'parent_id' => '2', |
159
|
|
|
'level' => '2', |
160
|
|
|
'scope' => '2', |
161
|
|
|
), |
162
|
|
|
array( |
163
|
|
|
'tree_traversal_id' => '5', |
164
|
|
|
'name' => null, |
165
|
|
|
'lft' => '7', |
166
|
|
|
'rgt' => '8', |
167
|
|
|
'parent_id' => '2', |
168
|
|
|
'level' => '2', |
169
|
|
|
'scope' => '2', |
170
|
|
|
), |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
$nodeData = $this->treeAdapter |
174
|
|
|
->getDescendants(2); |
175
|
|
|
$this->assertEquals($expectedNodeData, $nodeData); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testGetPath() |
179
|
|
|
{ |
180
|
|
|
$expectedNodeData = array( |
181
|
|
|
array( |
182
|
|
|
'tree_traversal_id' => '1', |
183
|
|
|
'name' => null, |
184
|
|
|
'lft' => '1', |
185
|
|
|
'rgt' => '10', |
186
|
|
|
'parent_id' => '0', |
187
|
|
|
'level' => '0', |
188
|
|
|
'scope' => '2', |
189
|
|
|
), |
190
|
|
|
array( |
191
|
|
|
'tree_traversal_id' => '2', |
192
|
|
|
'name' => null, |
193
|
|
|
'lft' => '2', |
194
|
|
|
'rgt' => '9', |
195
|
|
|
'parent_id' => '1', |
196
|
|
|
'level' => '1', |
197
|
|
|
'scope' => '2', |
198
|
|
|
), |
199
|
|
|
array( |
200
|
|
|
'tree_traversal_id' => '5', |
201
|
|
|
'name' => null, |
202
|
|
|
'lft' => '7', |
203
|
|
|
'rgt' => '8', |
204
|
|
|
'parent_id' => '2', |
205
|
|
|
'level' => '2', |
206
|
|
|
'scope' => '2', |
207
|
|
|
), |
208
|
|
|
); |
209
|
|
|
|
210
|
|
|
$nodeData = $this->treeAdapter |
211
|
|
|
->getPath(5); |
212
|
|
|
$this->assertEquals($expectedNodeData, $nodeData); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function testUpdateCannotCorruptTreeStructure() |
216
|
|
|
{ |
217
|
|
|
$excepted = array( |
218
|
|
|
'tree_traversal_id' => 4, |
219
|
|
|
'name' => 'updated', |
220
|
|
|
'lft' => 5, |
221
|
|
|
'rgt' => 6, |
222
|
|
|
'parent_id' => 2, |
223
|
|
|
'level' => 2, |
224
|
|
|
'scope' => 2, |
225
|
|
|
); |
226
|
|
|
|
227
|
|
|
$data = array( |
228
|
|
|
'tree_traversal_id' => 'corrupt data', |
229
|
|
|
'name' => 'updated', |
230
|
|
|
'lft' => 'corrupt data', |
231
|
|
|
'rgt' => 'corrupt data', |
232
|
|
|
'parent_id' => 'corrupt data', |
233
|
|
|
'level' => 'corrupt data', |
234
|
|
|
'scope' => 'corrupt data', |
235
|
|
|
); |
236
|
|
|
$this->treeAdapter |
237
|
|
|
->updateNode(4, $data); |
238
|
|
|
|
239
|
|
|
$this->assertEquals($excepted, $this->treeAdapter->getNode(4)); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|