Completed
Push — master ( 5033a3...4be7fb )
by Bartko
03:45
created

AbstractScopeTest::testMoveNodePlacementBottom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
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
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
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
        switch ($this->getName()) {
36
            case 'testValidateTreeRaiseExceptionIfIdParentIdIsBroken':
37
                return $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/initDataSetBrokenParents.xml');
38
            case 'testInvalidTree':
39
            case 'testRebuildTree':
40
                return $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/initDataSetBrokenTreeIndexes.xml');
41
            default:
42
                return $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/initDataSet.xml');
43
        }
44
    }
45
46
    public function testCreateRoot()
47
    {
48
        $this->treeAdapter
49
             ->createRootNode(array(), 10);
50
51
        $dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope'));
52
        $expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testCreateRoot.xml');
53
        $this->assertDataSetsEqual($expectedDataSet, $dataSet);
54
    }
55
56
    public function testCreateRootRootWithSomeScopeAlreadyExist()
57
    {
58
        $this->expectException('\StefanoTree\Exception\RootNodeAlreadyExistException');
59
        $this->expectExceptionMessage('Root node for scope "123" 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 testAddNodePlacementChildTop()
97
    {
98
        $lastGeneratedValue = $this->treeAdapter
99
            ->addNodePlacementChildTop(1);
100
101
        $dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope'));
102
        $expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testAddNodePlacementChildTop.xml');
103
        $this->assertDataSetsEqual($expectedDataSet, $dataSet);
104
        $this->assertEquals(9, $lastGeneratedValue);
105
    }
106
107
    public function testMoveNodePlacementBottom()
108
    {
109
        $this->treeAdapter
110
             ->moveNodePlacementBottom(3, 5);
111
112
        $dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope'));
113
        $expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testMoveNodePlacementBottom.xml');
114
        $this->assertDataSetsEqual($expectedDataSet, $dataSet);
115
    }
116
117
    public function testCannotMoveNodeBetweenScopes()
118
    {
119
        $this->expectException('\StefanoTree\Exception\InvalidArgumentException');
120
        $this->expectExceptionMessage('Cannot move node between scopes');
121
122
        $this->treeAdapter
123
             ->moveNodePlacementChildBottom(4, 8);
124
    }
125
126
    public function testDeleteBranch()
127
    {
128
        $this->treeAdapter
129
            ->deleteBranch(2);
130
131
        $dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope'));
132
        $expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testDeleteBranch.xml');
133
        $this->assertDataSetsEqual($expectedDataSet, $dataSet);
134
    }
135
136
    public function testGetDescendants()
137
    {
138
        $expectedNodeData = array(
139
            array(
140
                'tree_traversal_id' => '2',
141
                'name' => null,
142
                'lft' => '2',
143
                'rgt' => '9',
144
                'parent_id' => '1',
145
                'level' => '1',
146
                'scope' => '2',
147
            ),
148
            array(
149
                'tree_traversal_id' => '3',
150
                'name' => null,
151
                'lft' => '3',
152
                'rgt' => '4',
153
                'parent_id' => '2',
154
                'level' => '2',
155
                'scope' => '2',
156
            ),
157
            array(
158
                'tree_traversal_id' => '4',
159
                'name' => null,
160
                'lft' => '5',
161
                'rgt' => '6',
162
                'parent_id' => '2',
163
                'level' => '2',
164
                'scope' => '2',
165
            ),
166
            array(
167
                'tree_traversal_id' => '5',
168
                'name' => null,
169
                'lft' => '7',
170
                'rgt' => '8',
171
                'parent_id' => '2',
172
                'level' => '2',
173
                'scope' => '2',
174
            ),
175
        );
176
177
        $nodeData = $this->treeAdapter
178
                       ->getDescendants(2);
179
        $this->assertEquals($expectedNodeData, $nodeData);
180
    }
181
182
    public function testGetPath()
183
    {
184
        $expectedNodeData = array(
185
            array(
186
                'tree_traversal_id' => '1',
187
                'name' => null,
188
                'lft' => '1',
189
                'rgt' => '10',
190
                'parent_id' => NULL,
191
                'level' => '0',
192
                'scope' => '2',
193
            ),
194
            array(
195
                'tree_traversal_id' => '2',
196
                'name' => null,
197
                'lft' => '2',
198
                'rgt' => '9',
199
                'parent_id' => '1',
200
                'level' => '1',
201
                'scope' => '2',
202
            ),
203
            array(
204
                'tree_traversal_id' => '5',
205
                'name' => null,
206
                'lft' => '7',
207
                'rgt' => '8',
208
                'parent_id' => '2',
209
                'level' => '2',
210
                'scope' => '2',
211
            ),
212
        );
213
214
        $nodeData = $this->treeAdapter
215
            ->getPath(5);
216
        $this->assertEquals($expectedNodeData, $nodeData);
217
    }
218
219
    public function testUpdateCannotCorruptTreeStructure()
220
    {
221
        $excepted = array(
222
            'tree_traversal_id' => 4,
223
            'name' => 'updated',
224
            'lft' => 5,
225
            'rgt' => 6,
226
            'parent_id' => 2,
227
            'level' => 2,
228
            'scope' => 2,
229
        );
230
231
        $data = array(
232
            'tree_traversal_id' => 'corrupt data',
233
            'name' => 'updated',
234
            'lft' => 'corrupt data',
235
            'rgt' => 'corrupt data',
236
            'parent_id' => 'corrupt data',
237
            'level' => 'corrupt data',
238
            'scope' => 'corrupt data',
239
        );
240
        $this->treeAdapter
241
             ->updateNode(4, $data);
242
243
        $this->assertEquals($excepted, $this->treeAdapter->getNode(4));
244
    }
245
246
    public function testIsTreeValid()
247
    {
248
        $this->assertTrue($this->treeAdapter->isValid(1));
249
    }
250
251
    public function testInvalidTree()
252
    {
253
        $this->assertFalse($this->treeAdapter->isValid(1));
254
    }
255
256
    public function testValidateTreeGivenNodeIdIsNotRoot()
257
    {
258
        $this->expectException('\StefanoTree\Exception\InvalidArgumentException');
259
        $this->expectExceptionMessage('Given node id "2" is not root id');
260
261
        $this->treeAdapter->isValid(2);
262
    }
263
264
    public function testRebuildTree()
265
    {
266
        $this->treeAdapter
267
             ->rebuild(1);
268
269
        $dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope'));
270
        $expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testRebuildTree.xml');
271
        $this->assertDataSetsEqual($expectedDataSet, $dataSet);
272
    }
273
274
    public function testRebuildTreeGivenNodeIdIsNotRoot()
275
    {
276
        $this->expectException('\StefanoTree\Exception\InvalidArgumentException');
277
        $this->expectExceptionMessage('Given node id "5" is not root id');
278
279
        $this->treeAdapter->isValid(5);
280
    }
281
}
282