Completed
Push — master ( b97768...587712 )
by Bartko
31:36
created

NestedSetWithScopeTest::testGetDescendants()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 9.1563
c 0
b 0
f 0
cc 1
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 StefanoTree\NestedSet\Options;
9
use StefanoTreeTest\IntegrationTestCase;
10
use StefanoTreeTest\TestUtil;
11
12
class NestedSetWithScopeTest extends IntegrationTestCase
13
{
14
    /**
15
     * @var TreeAdapter
16
     */
17
    protected $treeAdapter;
18
19
    protected function setUp()
20
    {
21
        $this->treeAdapter = $this->getTreeAdapter();
22
23
        parent::setUp();
24
    }
25
26
    protected function tearDown()
27
    {
28
        $this->treeAdapter = null;
29
        parent::tearDown();
30
    }
31
32
    /**
33
     * @return TreeAdapter
34
     */
35
    protected function getTreeAdapter()
36
    {
37
        $options = new Options(array(
38
                                   'tableName' => 'tree_traversal_with_scope',
39
                                   'idColumnName' => 'tree_traversal_id',
40
                                   'scopeColumnName' => 'scope',
41
                               ));
42
43
        if ('pgsql' == TEST_STEFANO_DB_VENDOR) {
44
            $options->setSequenceName('tree_traversal_with_scope_tree_traversal_id_seq');
45
        }
46
47
        return new TreeAdapter($options, TestUtil::buildAdapter($options));
48
    }
49
50
    protected function getDataSet()
51
    {
52
        switch ($this->getName()) {
53
            case 'testValidateTreeRaiseExceptionIfIdParentIdIsBroken':
54
                return $this->createMySQLXMLDataSet(__DIR__.'/_files/NestedSet/with_scope/initDataSetBrokenParents.xml');
55
            case 'testInvalidTree':
56
            case 'testRebuildTree':
57
                return $this->createMySQLXMLDataSet(__DIR__.'/_files/NestedSet/with_scope/initDataSetBrokenTreeIndexes.xml');
58
            default:
59
                return $this->createMySQLXMLDataSet(__DIR__.'/_files/NestedSet/with_scope/initDataSet.xml');
60
        }
61
    }
62
63
    public function testCreateRoot()
64
    {
65
        $this->treeAdapter
66
            ->createRootNode(array(), 10);
67
68
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testCreateRoot.xml');
69
    }
70
71
    public function testCreateRootRootWithSomeScopeAlreadyExist()
72
    {
73
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
74
        $this->expectExceptionMessage('Root node for given scope already exist');
75
76
        $this->treeAdapter
77
            ->createRootNode(array(), 123);
78
        $this->treeAdapter
79
            ->createRootNode(array(), 123);
80
    }
81
82
    public function testGetRoots()
83
    {
84
        $expected = array(
85
            array(
86
                'tree_traversal_id' => 1,
87
                'name' => null,
88
                'lft' => 1,
89
                'rgt' => 10,
90
                'parent_id' => 0,
91
                'level' => 0,
92
                'scope' => 2,
93
            ),
94
            array(
95
                'tree_traversal_id' => 6,
96
                'name' => null,
97
                'lft' => 1,
98
                'rgt' => 6,
99
                'parent_id' => 0,
100
                'level' => 0,
101
                'scope' => 1,
102
            ),
103
        );
104
105
        $roots = $this->treeAdapter
106
            ->getRoots();
107
108
        $this->assertEquals($expected, $roots);
109
    }
110
111
    public function testAddNodePlacementChildTopDefaultPlacement()
112
    {
113
        $lastGeneratedValue = $this->treeAdapter
114
            ->addNode(1);
115
116
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testAddNodePlacementChildTop.xml');
117
        $this->assertEquals(9, $lastGeneratedValue);
118
    }
119
120
    public function testMoveNodePlacementBottom()
121
    {
122
        $this->treeAdapter
123
            ->moveNode(3, 5, TreeAdapter::PLACEMENT_BOTTOM);
124
125
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testMoveNodePlacementBottom.xml');
126
    }
127
128
    public function testCannotMoveNodeBetweenScopes()
129
    {
130
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
131
        $this->expectExceptionMessage('Cannot move node between scopes.');
132
133
        $this->treeAdapter
134
            ->moveNode(4, 8, TreeAdapter::PLACEMENT_CHILD_BOTTOM);
135
    }
136
137
    public function testDeleteBranch()
138
    {
139
        $this->treeAdapter
140
            ->deleteBranch(2);
141
142
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testDeleteBranch.xml');
143
    }
144
145
    public function testGetDescendants()
146
    {
147
        $expectedNodeData = array(
148
            array(
149
                'tree_traversal_id' => '2',
150
                'name' => null,
151
                'lft' => '2',
152
                'rgt' => '9',
153
                'parent_id' => '1',
154
                'level' => '1',
155
                'scope' => '2',
156
            ),
157
            array(
158
                'tree_traversal_id' => '3',
159
                'name' => null,
160
                'lft' => '3',
161
                'rgt' => '4',
162
                'parent_id' => '2',
163
                'level' => '2',
164
                'scope' => '2',
165
            ),
166
            array(
167
                'tree_traversal_id' => '4',
168
                'name' => null,
169
                'lft' => '5',
170
                'rgt' => '6',
171
                'parent_id' => '2',
172
                'level' => '2',
173
                'scope' => '2',
174
            ),
175
            array(
176
                'tree_traversal_id' => '5',
177
                'name' => null,
178
                'lft' => '7',
179
                'rgt' => '8',
180
                'parent_id' => '2',
181
                'level' => '2',
182
                'scope' => '2',
183
            ),
184
        );
185
186
        $nodeData = $this->treeAdapter
187
            ->getDescendantsQueryBuilder()
188
            ->get(2);
189
190
        $this->assertEquals($expectedNodeData, $nodeData);
191
    }
192
193
    public function testGetAncestors()
194
    {
195
        $expectedNodeData = array(
196
            array(
197
                'tree_traversal_id' => '1',
198
                'name' => null,
199
                'lft' => '1',
200
                'rgt' => '10',
201
                'parent_id' => null,
202
                'level' => '0',
203
                'scope' => '2',
204
            ),
205
            array(
206
                'tree_traversal_id' => '2',
207
                'name' => null,
208
                'lft' => '2',
209
                'rgt' => '9',
210
                'parent_id' => '1',
211
                'level' => '1',
212
                'scope' => '2',
213
            ),
214
            array(
215
                'tree_traversal_id' => '5',
216
                'name' => null,
217
                'lft' => '7',
218
                'rgt' => '8',
219
                'parent_id' => '2',
220
                'level' => '2',
221
                'scope' => '2',
222
            ),
223
        );
224
225
        $nodeData = $this->treeAdapter
226
            ->getAncestorsQueryBuilder()
227
            ->get(5);
228
        $this->assertEquals($expectedNodeData, $nodeData);
229
    }
230
231
    public function testUpdateCannotCorruptTreeStructure()
232
    {
233
        $excepted = array(
234
            'tree_traversal_id' => 4,
235
            'name' => 'updated',
236
            'lft' => 5,
237
            'rgt' => 6,
238
            'parent_id' => 2,
239
            'level' => 2,
240
            'scope' => 2,
241
        );
242
243
        $data = array(
244
            'tree_traversal_id' => 'corrupt data',
245
            'name' => 'updated',
246
            'lft' => 'corrupt data',
247
            'rgt' => 'corrupt data',
248
            'parent_id' => 'corrupt data',
249
            'level' => 'corrupt data',
250
            'scope' => 'corrupt data',
251
        );
252
        $this->treeAdapter
253
            ->updateNode(4, $data);
254
255
        $this->assertEquals($excepted, $this->treeAdapter->getNode(4));
256
    }
257
258
    public function testIsTreeValid()
259
    {
260
        $this->assertTrue($this->treeAdapter->isValid(1));
261
    }
262
263
    public function testInvalidTree()
264
    {
265
        $this->assertFalse($this->treeAdapter->isValid(1));
266
    }
267
268
    public function testValidateTreeGivenNodeIdIsNotRoot()
269
    {
270
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
271
        $this->expectExceptionMessage('Given node is not root node.');
272
273
        $this->treeAdapter->isValid(2);
274
    }
275
276
    public function testRebuildTree()
277
    {
278
        $this->treeAdapter
279
            ->rebuild(1);
280
281
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testRebuildTree.xml');
282
    }
283
284
    public function testRebuildTreeGivenNodeIdIsNotRoot()
285
    {
286
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
287
        $this->expectExceptionMessage('Given node is not root node.');
288
289
        $this->treeAdapter->rebuild(5);
290
    }
291
292
    public function testIsValidTreeGivenNodeIdIsNotRoot()
293
    {
294
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
295
        $this->expectExceptionMessage('Given node is not root node.');
296
297
        $this->treeAdapter->isValid(4);
298
    }
299
300
    public function testRebuildTreeGivenNodeIdDoesNotExists()
301
    {
302
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
303
        $this->expectExceptionMessage('Node does not exists.');
304
305
        $this->treeAdapter->rebuild(999);
306
    }
307
308
    public function testIsValidTreeGivenNodeIdDoesNotExists()
309
    {
310
        $this->expectException(\StefanoTree\Exception\ValidationException::class);
311
        $this->expectExceptionMessage('Node does not exists.');
312
313
        $this->treeAdapter->isValid(555);
314
    }
315
}
316