Completed
Push — v4-develop ( 528a0a )
by Bartko
02:29
created

ManipulatorWithScopeTest   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 7
dl 0
loc 206
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A tearDown() 0 5 1
A getManipulator() 0 16 2
A getDataSet() 0 4 1
A testUpdateDataDoesNotChangeMetadata() 0 16 1
A testInsertDataDoesNotChangeMetadata() 0 18 1
A testDeleteBranch() 0 7 1
A testMoveLeftIndexes() 0 7 1
A testMoveRightIndexes() 0 7 1
A testUpdateLevels() 0 7 1
A testMoveBranch() 0 7 1
A testGetRoots() 0 8 1
A testGetRoot() 0 8 1
A testGetNodeInfo() 0 12 1
A testGetChildrenNodeInfo() 0 21 1
A testUpdateNodeMetadata() 0 9 1
A testGetPath() 0 10 1
A testGetDescendants() 0 10 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 as ManipulatorInterface;
9
use StefanoTree\NestedSet\Adapter\Zend2;
10
use StefanoTree\NestedSet\NodeInfo;
11
use StefanoTree\NestedSet\Options;
12
use StefanoTreeTest\IntegrationTestCase;
13
use StefanoTreeTest\TestUtil;
14
15
class ManipulatorWithScopeTest extends IntegrationTestCase
16
{
17
    /**
18
     * @var ManipulatorInterface
19
     */
20
    protected $manipulator;
21
22
    protected function setUp()
23
    {
24
        $this->manipulator = $this->getManipulator();
25
26
        parent::setUp();
27
    }
28
29
    protected function tearDown()
30
    {
31
        $this->manipulator = null;
32
        parent::tearDown();
33
    }
34
35
    /**
36
     * @return ManipulatorInterface
37
     */
38
    protected function getManipulator(): ManipulatorInterface
39
    {
40
        $options = new Options(array(
41
                                   'tableName' => 'tree_traversal_with_scope',
42
                                   'idColumnName' => 'tree_traversal_id',
43
                                   'scopeColumnName' => 'scope',
44
                               ));
45
46
        if ('pgsql' == TEST_STEFANO_DB_ADAPTER) {
47
            $options->setSequenceName('tree_traversal_with_scope_tree_traversal_id_seq');
48
        }
49
50
        $adapter = new Zend2($options, TestUtil::getZend2DbAdapter()); //todo use pdo
51
52
        return new Manipulator($options, $adapter);
53
    }
54
55
    protected function getDataSet()
56
    {
57
        return $this->createMySQLXMLDataSet(__DIR__.'/_files/adapter/with_scope/initDataSet.xml');
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.xml');
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.xml');
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.xml');
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.xml');
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.xml');
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.xml');
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.xml');
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);
0 ignored issues
show
Documentation introduced by
$nodeInfo is of type array<integer,object<Ste...ee\NestedSet\NodeInfo>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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.xml');
197
    }
198
199
    public function testGetPath()
200
    {
201
        $path = $this->manipulator
202
            ->getAncestors(5);
203
204
        $this->assertCount(3, $path);
0 ignored issues
show
Documentation introduced by
$path is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
$nodes is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
216
217
        $expected = include __DIR__.'/_files/adapter/with_scope/testGetDescendants.php';
218
        $this->assertEquals($expected, $nodes);
219
    }
220
}
221