Completed
Push — develop ( f20a8e...81b85b )
by Bartko
01:51
created

testUpdateCannotCorruptTreeStructure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
c 0
b 0
f 0
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace StefanoTreeTest\Integration;
6
7
use StefanoTree\NestedSet as TreeAdapter;
8
use StefanoTreeTest\IntegrationTestCase;
9
10
abstract class AbstractScopeTest extends IntegrationTestCase
11
{
12
    /**
13
     * @var TreeAdapter
14
     */
15
    protected $treeAdapter;
16
17
    protected function setUp()
18
    {
19
        $this->treeAdapter = $this->getTreeAdapter();
20
21
        parent::setUp();
22
    }
23
24
    protected function tearDown()
25
    {
26
        $this->treeAdapter = null;
27
        parent::tearDown();
28
    }
29
30
    /**
31
     * @return TreeAdapter
32
     */
33
    abstract protected function getTreeAdapter();
34
35
    protected function getDataSet()
36
    {
37
        switch ($this->getName()) {
38
            case 'testValidateTreeRaiseExceptionIfIdParentIdIsBroken':
39
                return $this->createMySQLXMLDataSet(__DIR__.'/_files/NestedSet/with_scope/initDataSetBrokenParents.xml');
40
            case 'testInvalidTree':
41
            case 'testRebuildTree':
42
                return $this->createMySQLXMLDataSet(__DIR__.'/_files/NestedSet/with_scope/initDataSetBrokenTreeIndexes.xml');
43
            default:
44
                return $this->createMySQLXMLDataSet(__DIR__.'/_files/NestedSet/with_scope/initDataSet.xml');
45
        }
46
    }
47
48
    public function testCreateRoot()
49
    {
50
        $this->treeAdapter
51
             ->createRootNode(array(), 10);
52
53
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testCreateRoot.xml');
54
    }
55
56
    public function testCreateRootRootWithSomeScopeAlreadyExist()
57
    {
58
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
59
        $this->expectExceptionMessage('Root node for given scope 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 testAddNodePlacementChildTopDefaultPlacement()
97
    {
98
        $lastGeneratedValue = $this->treeAdapter
99
            ->addNode(1);
100
101
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testAddNodePlacementChildTop.xml');
102
        $this->assertEquals(9, $lastGeneratedValue);
103
    }
104
105
    public function testMoveNodePlacementBottom()
106
    {
107
        $this->treeAdapter
108
             ->moveNode(3, 5, TreeAdapter::PLACEMENT_BOTTOM);
109
110
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testMoveNodePlacementBottom.xml');
111
    }
112
113
    public function testCannotMoveNodeBetweenScopes()
114
    {
115
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
116
        $this->expectExceptionMessage('Cannot move node between scopes.');
117
118
        $this->treeAdapter
119
             ->moveNode(4, 8, TreeAdapter::PLACEMENT_CHILD_BOTTOM);
120
    }
121
122
    public function testDeleteBranch()
123
    {
124
        $this->treeAdapter
125
            ->deleteBranch(2);
126
127
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testDeleteBranch.xml');
128
    }
129
130
    public function testGetDescendants()
131
    {
132
        $expectedNodeData = array(
133
            array(
134
                'tree_traversal_id' => '2',
135
                'name' => null,
136
                'lft' => '2',
137
                'rgt' => '9',
138
                'parent_id' => '1',
139
                'level' => '1',
140
                'scope' => '2',
141
            ),
142
            array(
143
                'tree_traversal_id' => '3',
144
                'name' => null,
145
                'lft' => '3',
146
                'rgt' => '4',
147
                'parent_id' => '2',
148
                'level' => '2',
149
                'scope' => '2',
150
            ),
151
            array(
152
                'tree_traversal_id' => '4',
153
                'name' => null,
154
                'lft' => '5',
155
                'rgt' => '6',
156
                'parent_id' => '2',
157
                'level' => '2',
158
                'scope' => '2',
159
            ),
160
            array(
161
                'tree_traversal_id' => '5',
162
                'name' => null,
163
                'lft' => '7',
164
                'rgt' => '8',
165
                'parent_id' => '2',
166
                'level' => '2',
167
                'scope' => '2',
168
            ),
169
        );
170
171
        $nodeData = $this->treeAdapter
172
                       ->getDescendantsQueryBuilder()
173
                       ->get(2);
174
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' => null,
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
    public function testIsTreeValid()
243
    {
244
        $this->assertTrue($this->treeAdapter->isValid(1));
245
    }
246
247
    public function testInvalidTree()
248
    {
249
        $this->assertFalse($this->treeAdapter->isValid(1));
250
    }
251
252
    public function testValidateTreeGivenNodeIdIsNotRoot()
253
    {
254
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
255
        $this->expectExceptionMessage('Given node is not root node.');
256
257
        $this->treeAdapter->isValid(2);
258
    }
259
260
    public function testRebuildTree()
261
    {
262
        $this->treeAdapter
263
             ->rebuild(1);
264
265
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testRebuildTree.xml');
266
    }
267
268
    public function testRebuildTreeGivenNodeIdIsNotRoot()
269
    {
270
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
271
        $this->expectExceptionMessage('Given node is not root node.');
272
273
        $this->treeAdapter->rebuild(5);
274
    }
275
276
    public function testIsValidTreeGivenNodeIdIsNotRoot()
277
    {
278
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
279
        $this->expectExceptionMessage('Given node is not root node.');
280
281
        $this->treeAdapter->isValid(4);
282
    }
283
284
    public function testRebuildTreeGivenNodeIdDoesNotExists()
285
    {
286
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
287
        $this->expectExceptionMessage('Node does not exists.');
288
289
        $this->treeAdapter->rebuild(999);
290
    }
291
292
    public function testIsValidTreeGivenNodeIdDoesNotExists()
293
    {
294
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
295
        $this->expectExceptionMessage('Node does not exists.');
296
297
        $this->treeAdapter->isValid(555);
298
    }
299
}
300