Completed
Push — master ( 2b0446...afde37 )
by Bartko
04:01 queued 01:40
created

testValidateTreeGivenNodeIdIsNotRoot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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 testGetAncestors()
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
            ->getAncestorsQueryBuilder()
212
            ->get(5);
213
        $this->assertEquals($expectedNodeData, $nodeData);
214
    }
215
216
    public function testUpdateCannotCorruptTreeStructure()
217
    {
218
        $excepted = array(
219
            'tree_traversal_id' => 4,
220
            'name' => 'updated',
221
            'lft' => 5,
222
            'rgt' => 6,
223
            'parent_id' => 2,
224
            'level' => 2,
225
            'scope' => 2,
226
        );
227
228
        $data = array(
229
            'tree_traversal_id' => 'corrupt data',
230
            'name' => 'updated',
231
            'lft' => 'corrupt data',
232
            'rgt' => 'corrupt data',
233
            'parent_id' => 'corrupt data',
234
            'level' => 'corrupt data',
235
            'scope' => 'corrupt data',
236
        );
237
        $this->treeAdapter
238
             ->updateNode(4, $data);
239
240
        $this->assertEquals($excepted, $this->treeAdapter->getNode(4));
241
    }
242
243
    public function testIsTreeValid()
244
    {
245
        $this->assertTrue($this->treeAdapter->isValid(1));
246
    }
247
248
    public function testInvalidTree()
249
    {
250
        $this->assertFalse($this->treeAdapter->isValid(1));
251
    }
252
253
    public function testValidateTreeGivenNodeIdIsNotRoot()
254
    {
255
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
256
        $this->expectExceptionMessage('Given node is not root node.');
257
258
        $this->treeAdapter->isValid(2);
259
    }
260
261
    public function testRebuildTree()
262
    {
263
        $this->treeAdapter
264
             ->rebuild(1);
265
266
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testRebuildTree.xml');
267
    }
268
269
    public function testRebuildTreeGivenNodeIdIsNotRoot()
270
    {
271
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
272
        $this->expectExceptionMessage('Given node is not root node.');
273
274
        $this->treeAdapter->rebuild(5);
275
    }
276
277
    public function testIsValidTreeGivenNodeIdIsNotRoot()
278
    {
279
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
280
        $this->expectExceptionMessage('Given node is not root node.');
281
282
        $this->treeAdapter->isValid(4);
283
    }
284
285
    public function testRebuildTreeGivenNodeIdDoesNotExists()
286
    {
287
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
288
        $this->expectExceptionMessage('Node does not exists.');
289
290
        $this->treeAdapter->rebuild(999);
291
    }
292
293
    public function testIsValidTreeGivenNodeIdDoesNotExists()
294
    {
295
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
296
        $this->expectExceptionMessage('Node does not exists.');
297
298
        $this->treeAdapter->isValid(555);
299
    }
300
}
301