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 AdapterWithScopeTestAbstract |
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/with_scope/initDataSet.xml'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testLockTreeDoesNotFail() |
40
|
|
|
{ |
41
|
|
|
$this->adapter |
42
|
|
|
->lockTree(2); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testUpdateDataDoesNotChangeMetadata() |
46
|
|
|
{ |
47
|
|
|
$data = array( |
48
|
|
|
'name' => 'changed', |
49
|
|
|
'lft' => 'a', |
50
|
|
|
'rgt' => 'b', |
51
|
|
|
'parent_id' => 'c', |
52
|
|
|
'level' => 'd', |
53
|
|
|
'scope' => 'e' |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$this->adapter |
57
|
|
|
->update(2, $data); |
58
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
59
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/with_scope/testUpdateData.xml'); |
60
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testInsertDataDoesNotChangeMetadata() |
64
|
|
|
{ |
65
|
|
|
$nodeInfo = new NodeInfo(null, 1000, 1001, 1002, 1003, 1004); |
66
|
|
|
|
67
|
|
|
$data = array( |
68
|
|
|
'name' => 'some-name', |
69
|
|
|
'lft' => 'a', |
70
|
|
|
'rgt' => 'b', |
71
|
|
|
'parent_id' => 'c', |
72
|
|
|
'level' => 'd', |
73
|
|
|
'scope' => 'e', |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$this->adapter |
77
|
|
|
->insert($nodeInfo, $data); |
78
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
79
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/with_scope/testInsertData.xml'); |
80
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testDeleteBranch() |
84
|
|
|
{ |
85
|
|
|
$this->adapter |
86
|
|
|
->delete(2, 9, 2); |
87
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
88
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/with_scope/testDeleteBranch.xml'); |
89
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testMoveLeftIndexes() |
93
|
|
|
{ |
94
|
|
|
$this->adapter |
95
|
|
|
->moveLeftIndexes(3, 500, 2); |
96
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
97
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/with_scope/testMoveLeftIndexes.xml'); |
98
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testMoveRightIndexes() |
102
|
|
|
{ |
103
|
|
|
$this->adapter |
104
|
|
|
->moveRightIndexes(4, 500, 2); |
105
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
106
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/with_scope/testMoveRightIndexes.xml'); |
107
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testUpdateLevels() |
111
|
|
|
{ |
112
|
|
|
$this->adapter |
113
|
|
|
->updateLevels(2, 9, 500, 2); |
114
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
115
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/with_scope/testUpdateLevels.xml'); |
116
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testMoveBranch() |
120
|
|
|
{ |
121
|
|
|
$this->adapter |
122
|
|
|
->moveBranch(2, 9, 500, 2); |
123
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
124
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/with_scope/testMoveBranch.xml'); |
125
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testGetRoots() |
129
|
|
|
{ |
130
|
|
|
$roots = $this->adapter |
131
|
|
|
->getRoots(); |
132
|
|
|
|
133
|
|
|
$expected = include __DIR__ . '/_files/adapter/with_scope/testGetRoots.php'; |
134
|
|
|
$this->assertEquals($expected, $roots); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testGetRoot() |
138
|
|
|
{ |
139
|
|
|
$roots = $this->adapter |
140
|
|
|
->getRoot(2); |
141
|
|
|
|
142
|
|
|
$expected = include __DIR__ . '/_files/adapter/with_scope/testGetRoot.php'; |
143
|
|
|
$this->assertEquals($expected, $roots); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testGetNodeInfo() |
147
|
|
|
{ |
148
|
|
|
$nodeInfo = $this->adapter |
149
|
|
|
->getNodeInfo(8); |
150
|
|
|
|
151
|
|
|
$this->assertEquals($nodeInfo->getId(), 8); |
152
|
|
|
$this->assertEquals($nodeInfo->getParentId(), 7); |
153
|
|
|
$this->assertEquals($nodeInfo->getLeft(), 3); |
154
|
|
|
$this->assertEquals($nodeInfo->getRight(), 8); |
155
|
|
|
$this->assertEquals($nodeInfo->getLevel(), 2); |
156
|
|
|
$this->assertEquals($nodeInfo->getScope(), 1); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testGetChildrenNodeInfo() |
160
|
|
|
{ |
161
|
|
|
$nodeInfo = $this->adapter |
162
|
|
|
->getChildrenNodeInfo(2); |
163
|
|
|
|
164
|
|
|
$this->assertCount(3, $nodeInfo); |
165
|
|
|
|
166
|
|
|
// check first node info |
167
|
|
|
$this->assertEquals($nodeInfo[0]->getId(), 3); |
168
|
|
|
$this->assertEquals($nodeInfo[0]->getParentId(), 2); |
169
|
|
|
$this->assertEquals($nodeInfo[0]->getLeft(), 3); |
170
|
|
|
$this->assertEquals($nodeInfo[0]->getRight(), 4); |
171
|
|
|
$this->assertEquals($nodeInfo[0]->getLevel(), 2); |
172
|
|
|
|
173
|
|
|
// check last node info |
174
|
|
|
$this->assertEquals($nodeInfo[2]->getId(), 5); |
175
|
|
|
$this->assertEquals($nodeInfo[2]->getParentId(), 2); |
176
|
|
|
$this->assertEquals($nodeInfo[2]->getLeft(), 7); |
177
|
|
|
$this->assertEquals($nodeInfo[2]->getRight(), 8); |
178
|
|
|
$this->assertEquals($nodeInfo[2]->getLevel(), 2); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testUpdateNodeMetadata() |
182
|
|
|
{ |
183
|
|
|
$nodeInfo = new NodeInfo(3, 1000, 1001, 1002, 1003, 2); |
184
|
|
|
|
185
|
|
|
$this->adapter |
186
|
|
|
->updateNodeMetadata($nodeInfo); |
187
|
|
|
|
188
|
|
|
$dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope')); |
189
|
|
|
$expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/adapter/with_scope/testUpdateNodeMetadata.xml'); |
190
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testGetPath() |
194
|
|
|
{ |
195
|
|
|
$path = $this->adapter |
196
|
|
|
->getPath(5); |
197
|
|
|
|
198
|
|
|
$this->assertCount(3, $path); |
199
|
|
|
|
200
|
|
|
$expected = include __DIR__ . '/_files/adapter/with_scope/testGetPath.php'; |
201
|
|
|
$this->assertEquals($expected, $path); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function testGetDescendants() |
205
|
|
|
{ |
206
|
|
|
$nodes = $this->adapter |
207
|
|
|
->getDescendants(); |
208
|
|
|
|
209
|
|
|
$this->assertCount(5, $nodes); |
210
|
|
|
|
211
|
|
|
$expected = include __DIR__ . '/_files/adapter/with_scope/testGetDescendants.php'; |
212
|
|
|
$this->assertEquals($expected, $nodes); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|