ManipulatorWithScopeTest   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 93
c 0
b 0
f 0
dl 0
loc 202
rs 10
wmc 19

18 Methods

Rating   Name   Duplication   Size   Complexity  
A testDeleteBranch() 0 6 1
A testGetNodeInfo() 0 11 1
A testInsertDataDoesNotChangeMetadata() 0 17 1
A getManipulator() 0 13 2
A testUpdateDataDoesNotChangeMetadata() 0 15 1
A tearDown() 0 4 1
A testGetDescendants() 0 9 1
A testMoveBranch() 0 6 1
A testGetRoots() 0 7 1
A testUpdateLevels() 0 6 1
A getDataSet() 0 3 1
A testGetPath() 0 9 1
A setUp() 0 5 1
A testMoveRightIndexes() 0 6 1
A testUpdateNodeMetadata() 0 8 1
A testGetRoot() 0 7 1
A testGetChildrenNodeInfo() 0 20 1
A testMoveLeftIndexes() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace StefanoTreeTest\Integration\Manipulator;
6
7
use StefanoTree\NestedSet\Manipulator\Manipulator;
8
use StefanoTree\NestedSet\Manipulator\ManipulatorInterface;
9
use StefanoTree\NestedSet\NodeInfo;
10
use StefanoTree\NestedSet\Options;
11
use StefanoTreeTest\IntegrationTestCase;
12
use StefanoTreeTest\TestUtil;
13
14
/**
15
 * @internal
16
 */
17
class ManipulatorWithScopeTest extends IntegrationTestCase
18
{
19
    /**
20
     * @var ManipulatorInterface
21
     */
22
    protected $manipulator;
23
24
    protected function setUp(): void
25
    {
26
        $this->manipulator = $this->getManipulator();
27
28
        parent::setUp();
29
    }
30
31
    protected function tearDown(): void
32
    {
33
        $this->manipulator = null;
34
        parent::tearDown();
35
    }
36
37
    /**
38
     * @return ManipulatorInterface
39
     */
40
    protected function getManipulator(): ManipulatorInterface
41
    {
42
        $options = new Options(array(
43
            'tableName' => 'tree_traversal_with_scope',
44
            'idColumnName' => 'tree_traversal_id',
45
            'scopeColumnName' => 'scope',
46
        ));
47
48
        if ('pgsql' == TEST_STEFANO_DB_VENDOR) {
0 ignored issues
show
introduced by
The condition 'pgsql' == StefanoTreeTe...\TEST_STEFANO_DB_VENDOR is always false.
Loading history...
49
            $options->setSequenceName('tree_traversal_with_scope_tree_traversal_id_seq');
50
        }
51
52
        return new Manipulator($options, TestUtil::buildAdapter($options));
53
    }
54
55
    protected function getDataSet()
56
    {
57
        return $this->createArrayDataSet(include __DIR__.'/_files/adapter/with_scope/initDataSet.php');
58
    }
59
60
    public function testUpdateDataDoesNotChangeMetadata()
61
    {
62
        $data = array(
63
            'name' => 'changed',
64
            'lft' => 'a',
65
            'rgt' => 'b',
66
            'parent_id' => 'c',
67
            'level' => 'd',
68
            'scope' => 'e',
69
        );
70
71
        $this->manipulator
72
            ->update(2, $data);
73
74
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/adapter/with_scope/testUpdateData.php');
75
    }
76
77
    public function testInsertDataDoesNotChangeMetadata()
78
    {
79
        $nodeInfo = new NodeInfo(null, 6, 1001, 1002, 1003, 1004);
80
81
        $data = array(
82
            'name' => 'some-name',
83
            'lft' => 'a',
84
            'rgt' => 'b',
85
            'parent_id' => 'c',
86
            'level' => 'd',
87
            'scope' => 'e',
88
        );
89
90
        $this->manipulator
91
            ->insert($nodeInfo, $data);
92
93
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/adapter/with_scope/testInsertData.php');
94
    }
95
96
    public function testDeleteBranch()
97
    {
98
        $this->manipulator
99
            ->delete(2);
100
101
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/adapter/with_scope/testDeleteBranch.php');
102
    }
103
104
    public function testMoveLeftIndexes()
105
    {
106
        $this->manipulator
107
            ->moveLeftIndexes(3, 500, 2);
108
109
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/adapter/with_scope/testMoveLeftIndexes.php');
110
    }
111
112
    public function testMoveRightIndexes()
113
    {
114
        $this->manipulator
115
            ->moveRightIndexes(4, 500, 2);
116
117
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/adapter/with_scope/testMoveRightIndexes.php');
118
    }
119
120
    public function testUpdateLevels()
121
    {
122
        $this->manipulator
123
            ->updateLevels(2, 9, 500, 2);
124
125
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/adapter/with_scope/testUpdateLevels.php');
126
    }
127
128
    public function testMoveBranch()
129
    {
130
        $this->manipulator
131
            ->moveBranch(2, 9, 500, 2);
132
133
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/adapter/with_scope/testMoveBranch.php');
134
    }
135
136
    public function testGetRoots()
137
    {
138
        $roots = $this->manipulator
139
            ->getRoots();
140
141
        $expected = include __DIR__.'/_files/adapter/with_scope/testGetRoots.php';
142
        $this->assertEquals($expected, $roots);
143
    }
144
145
    public function testGetRoot()
146
    {
147
        $roots = $this->manipulator
148
            ->getRoot(2);
149
150
        $expected = include __DIR__.'/_files/adapter/with_scope/testGetRoot.php';
151
        $this->assertEquals($expected, $roots);
152
    }
153
154
    public function testGetNodeInfo()
155
    {
156
        $nodeInfo = $this->manipulator
157
            ->getNodeInfo(8);
158
159
        $this->assertEquals($nodeInfo->getId(), 8);
160
        $this->assertEquals($nodeInfo->getParentId(), 7);
161
        $this->assertEquals($nodeInfo->getLeft(), 3);
162
        $this->assertEquals($nodeInfo->getRight(), 8);
163
        $this->assertEquals($nodeInfo->getLevel(), 2);
164
        $this->assertEquals($nodeInfo->getScope(), 1);
165
    }
166
167
    public function testGetChildrenNodeInfo()
168
    {
169
        $nodeInfo = $this->manipulator
170
            ->getChildrenNodeInfo(2);
171
172
        $this->assertCount(3, $nodeInfo);
173
174
        // check first node info
175
        $this->assertEquals($nodeInfo[0]->getId(), 3);
176
        $this->assertEquals($nodeInfo[0]->getParentId(), 2);
177
        $this->assertEquals($nodeInfo[0]->getLeft(), 3);
178
        $this->assertEquals($nodeInfo[0]->getRight(), 4);
179
        $this->assertEquals($nodeInfo[0]->getLevel(), 2);
180
181
        // check last node info
182
        $this->assertEquals($nodeInfo[2]->getId(), 5);
183
        $this->assertEquals($nodeInfo[2]->getParentId(), 2);
184
        $this->assertEquals($nodeInfo[2]->getLeft(), 7);
185
        $this->assertEquals($nodeInfo[2]->getRight(), 8);
186
        $this->assertEquals($nodeInfo[2]->getLevel(), 2);
187
    }
188
189
    public function testUpdateNodeMetadata()
190
    {
191
        $nodeInfo = new NodeInfo(3, 1000, 1001, 1002, 1003, 2);
192
193
        $this->manipulator
194
            ->updateNodeMetadata($nodeInfo);
195
196
        $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/adapter/with_scope/testUpdateNodeMetadata.php');
197
    }
198
199
    public function testGetPath()
200
    {
201
        $path = $this->manipulator
202
            ->getAncestors(5);
203
204
        $this->assertCount(3, $path);
205
206
        $expected = include __DIR__.'/_files/adapter/with_scope/testGetPath.php';
207
        $this->assertEquals($expected, $path);
208
    }
209
210
    public function testGetDescendants()
211
    {
212
        $nodes = $this->manipulator
213
            ->getDescendants(1);
214
215
        $this->assertCount(5, $nodes);
216
217
        $expected = include __DIR__.'/_files/adapter/with_scope/testGetDescendants.php';
218
        $this->assertEquals($expected, $nodes);
219
    }
220
}
221