Completed
Push — develop ( d0fbe3...729812 )
by Bartko
14:43
created

AbstractScopeTest::testGetDescendants()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 45
rs 8.8571
cc 1
eloc 37
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
        return $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/initDataSet.xml');
36
    }
37
38
    public function testCreateRoot()
39
    {
40
        $this->treeAdapter
41
             ->createRootNode(array(), 10);
42
43
        $dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope'));
44
        $expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testCreateRoot.xml');
45
        $this->assertDataSetsEqual($expectedDataSet, $dataSet);
46
    }
47
48
    public function testCreateRootRootWithSomeScopeAlreadyExist()
49
    {
50
        $this->expectException(
51
            '\StefanoTree\Exception\RootNodeAlreadyExistException'
52
        );
53
        $this->expectExceptionMessage('Root node already exist');
54
55
        $this->treeAdapter
56
            ->createRootNode(array(), 123);
57
        $this->treeAdapter
58
            ->createRootNode(array(), 123);
59
    }
60
61
    public function testGetRoots()
62
    {
63
        $expected = array(
64
            array(
65
                'tree_traversal_id' => 1,
66
                'name' => null,
67
                'lft' => 1,
68
                'rgt' => 10,
69
                'parent_id' => 0,
70
                'level' => 0,
71
                'scope' => 2,
72
            ),
73
            array(
74
                'tree_traversal_id' => 6,
75
                'name' => null,
76
                'lft' => 1,
77
                'rgt' => 6,
78
                'parent_id' => 0,
79
                'level' => 0,
80
                'scope' => 1,
81
            ),
82
        );
83
84
        $roots = $this->treeAdapter
85
                      ->getRoots();
86
87
        $this->assertEquals($expected, $roots);
88
    }
89
90
    public function testAddNodePlacementChildTop()
91
    {
92
        $lastGeneratedValue = $this->treeAdapter
93
            ->addNodePlacementChildTop(1);
94
95
        $dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope'));
96
        $expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testAddNodePlacementChildTop.xml');
97
        $this->assertDataSetsEqual($expectedDataSet, $dataSet);
98
        $this->assertEquals(9, $lastGeneratedValue);
99
    }
100
101
    public function testMoveNodePlacementBottom()
102
    {
103
        $this->treeAdapter
104
             ->moveNodePlacementBottom(3, 5);
105
106
        $dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope'));
107
        $expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testMoveNodePlacementBottom.xml');
108
        $this->assertDataSetsEqual($expectedDataSet, $dataSet);
109
    }
110
111
    public function testCannotMoveNodeBetweenScopes()
112
    {
113
        $this->expectException(
114
            '\StefanoTree\Exception\InvalidArgumentException',
115
            'Cannot move node between scopes'
0 ignored issues
show
Unused Code introduced by
The call to AbstractScopeTest::expectException() has too many arguments starting with 'Cannot move node between scopes'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
116
        );
117
118
        $this->treeAdapter
119
             ->moveNodePlacementChildBottom(4, 8);
120
    }
121
122
    public function testDeleteBranch()
123
    {
124
        $this->treeAdapter
125
            ->deleteBranch(2);
126
127
        $dataSet = $this->getConnection()->createDataSet(array('tree_traversal_with_scope'));
128
        $expectedDataSet = $this->createMySQLXMLDataSet(__DIR__ . '/_files/NestedSet/with_scope/testDeleteBranch.xml');
129
        $this->assertDataSetsEqual($expectedDataSet, $dataSet);
130
    }
131
132
    public function testGetDescendants()
133
    {
134
        $expectedNodeData = array(
135
            array(
136
                'tree_traversal_id' => '2',
137
                'name' => null,
138
                'lft' => '2',
139
                'rgt' => '9',
140
                'parent_id' => '1',
141
                'level' => '1',
142
                'scope' => '2',
143
            ),
144
            array(
145
                'tree_traversal_id' => '3',
146
                'name' => null,
147
                'lft' => '3',
148
                'rgt' => '4',
149
                'parent_id' => '2',
150
                'level' => '2',
151
                'scope' => '2',
152
            ),
153
            array(
154
                'tree_traversal_id' => '4',
155
                'name' => null,
156
                'lft' => '5',
157
                'rgt' => '6',
158
                'parent_id' => '2',
159
                'level' => '2',
160
                'scope' => '2',
161
            ),
162
            array(
163
                'tree_traversal_id' => '5',
164
                'name' => null,
165
                'lft' => '7',
166
                'rgt' => '8',
167
                'parent_id' => '2',
168
                'level' => '2',
169
                'scope' => '2',
170
            ),
171
        );
172
173
        $nodeData = $this->treeAdapter
174
                       ->getDescendants(2);
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' => '0',
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