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

ManipulatorTest::testGetDescendantsFixLevels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
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\Manipulator;
6
7
use StefanoTree\NestedSet\Manipulator\Manipulator;
8
use StefanoTree\NestedSet\Manipulator\ManipulatorInterface as ManipulatorInterface;
9
use StefanoTree\NestedSet\NodeInfo;
10
use StefanoTree\NestedSet\Options;
11
use StefanoTreeTest\IntegrationTestCase;
12
use StefanoTreeTest\TestUtil;
13
14
class ManipulatorTest extends IntegrationTestCase
15
{
16
    /**
17
     * @var ManipulatorInterface
18
     */
19
    protected $manipulator;
20
21
    protected function setUp()
22
    {
23
        $this->manipulator = $this->getManipulator();
24
25
        parent::setUp();
26
    }
27
28
    protected function tearDown()
29
    {
30
        $this->manipulator = null;
31
        parent::tearDown();
32
    }
33
34
    /**
35
     * @return ManipulatorInterface
36
     */
37
    protected function getManipulator(): ManipulatorInterface
38
    {
39
        $options = new Options(array(
40
                                   'tableName' => 'tree_traversal',
41
                                   'idColumnName' => 'tree_traversal_id',
42
                               ));
43
44
        if ('pgsql' == TEST_STEFANO_DB_VENDOR) {
45
            $options->setSequenceName('tree_traversal_tree_traversal_id_seq');
46
        }
47
48
        return new Manipulator($options, TestUtil::buildAdapter($options));
49
    }
50
51
    protected function getDataSet()
52
    {
53
        return $this->createMySQLXMLDataSet(__DIR__.'/_files/adapter/initDataSet.xml');
54
    }
55
56
    public function testLockTreeDoesNotFail()
57
    {
58
        $this->manipulator
59
            ->lockTree();
60
    }
61
62
    public function testDbTransactionDoesNotFail()
63
    {
64
        $this->manipulator
65
            ->beginTransaction();
66
        $this->manipulator
67
            ->commitTransaction();
68
69
        $this->manipulator
70
            ->beginTransaction();
71
        $this->manipulator
72
            ->rollbackTransaction();
73
    }
74
75
    public function testUpdateData()
76
    {
77
        $this->manipulator
78
            ->update(2, array('name' => 'changed'));
79
80
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testUpdateData.xml');
81
    }
82
83
    public function testUpdateDataDoesNotChangeMetadata()
84
    {
85
        $data = array(
86
            'name' => 'changed',
87
            'lft' => 'a',
88
            'rgt' => 'b',
89
            'parent_id' => 'c',
90
            'tree_traversal_id' => 1596,
91
            'level' => 'd',
92
        );
93
94
        $this->manipulator
95
            ->update(2, $data);
96
97
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testUpdateData.xml');
98
    }
99
100
    public function testInsertData()
101
    {
102
        $nodeInfo = new NodeInfo(null, 6, 100, 1000, 1001, null);
103
104
        $generatedId = $this->manipulator
105
            ->insert($nodeInfo, array('name' => 'some-name'));
106
107
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testInsertData.xml');
108
        $this->assertEquals(26, $generatedId);
109
    }
110
111
    public function testInsertDataUserDefinedId()
112
    {
113
        $uuid = 753;
114
        $nodeInfo = new NodeInfo(null, 6, 100, 1000, 1001, null);
115
116
        $generatedId = $this->manipulator
117
            ->insert($nodeInfo, array('name' => 'some-name', 'tree_traversal_id' => $uuid));
118
119
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testInsertDataUserDefinedId.xml');
120
        $this->assertEquals($uuid, $generatedId);
121
    }
122
123
    public function testInsertDataDoesNotChangeMetadata()
124
    {
125
        $nodeInfo = new NodeInfo(null, 6, 100, 1000, 1001, null);
126
127
        $data = array(
128
            'name' => 'some-name',
129
            'lft' => 'a',
130
            'rgt' => 'b',
131
            'parent_id' => 'c',
132
            'level' => 'd',
133
        );
134
135
        $generatedId = $this->manipulator
136
            ->insert($nodeInfo, $data);
137
138
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testInsertData.xml');
139
        $this->assertEquals(26, $generatedId);
140
    }
141
142
    public function testDeleteBranch()
143
    {
144
        $this->manipulator
145
            ->delete(3);
146
147
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testDeleteBranch.xml');
148
    }
149
150
    public function testMoveLeftIndexes()
151
    {
152
        $this->manipulator
153
            ->moveLeftIndexes(12, 500);
154
155
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testMoveLeftIndexes.xml');
156
    }
157
158
    public function testMoveRightIndexes()
159
    {
160
        $this->manipulator
161
            ->moveRightIndexes(15, 500);
162
163
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testMoveRightIndexes.xml');
164
    }
165
166
    public function testUpdateParentId()
167
    {
168
        $this->manipulator
169
            ->updateParentId(3, 22);
170
171
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testUpdateParentId.xml');
172
    }
173
174
    public function testUpdateLevels()
175
    {
176
        $this->manipulator
177
            ->updateLevels(16, 35, 500);
178
179
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testUpdateLevels.xml');
180
    }
181
182
    public function testMoveBranch()
183
    {
184
        $this->manipulator
185
            ->moveBranch(17, 32, 500);
186
187
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testMoveBranch.xml');
188
    }
189
190
    public function testGetRoots()
191
    {
192
        $roots = $this->manipulator
193
            ->getRoots();
194
195
        $expected = include __DIR__.'/_files/adapter/testGetRoots.php';
196
        $this->assertEquals($expected, $roots);
197
    }
198
199
    public function testGetRoot()
200
    {
201
        $roots = $this->manipulator
202
            ->getRoot();
203
204
        $expected = include __DIR__.'/_files/adapter/testGetRoot.php';
205
        $this->assertEquals($expected, $roots);
206
    }
207
208
    public function testGetNodeReturnNullIfNodeDoesNotExist()
209
    {
210
        $node = $this->manipulator
211
            ->getNode(1000000);
212
        $this->assertNull($node);
213
    }
214
215
    public function testGetNode()
216
    {
217
        $node = $this->manipulator
218
            ->getNode(11);
219
220
        $expected = include __DIR__.'/_files/adapter/testGetNode.php';
221
        $this->assertEquals($expected, $node);
222
    }
223
224
    public function testGetNodeInfoReturnNullIfNodeInfoDoesNotExist()
225
    {
226
        $nodeInfo = $this->manipulator
227
            ->getNodeInfo(10000000);
228
        $this->assertNull($nodeInfo);
229
    }
230
231
    public function testGetNodeInfo()
232
    {
233
        $nodeInfo = $this->manipulator
234
            ->getNodeInfo(10);
235
236
        $this->assertEquals($nodeInfo->getId(), 10);
237
        $this->assertEquals($nodeInfo->getParentId(), 5);
238
        $this->assertEquals($nodeInfo->getLeft(), 4);
239
        $this->assertEquals($nodeInfo->getRight(), 11);
240
        $this->assertEquals($nodeInfo->getLevel(), 3);
241
    }
242
243
    public function testGetChildrenNodeInfoReturnEmptyArrayIfNodeDoesNotHaveChildrenNodes()
244
    {
245
        $nodeInfo = $this->manipulator
246
            ->getChildrenNodeInfo(7);
247
248
        $this->assertEquals(array(), $nodeInfo);
249
    }
250
251
    public function testGetChildrenNodeInfo()
252
    {
253
        $nodeInfo = $this->manipulator
254
            ->getChildrenNodeInfo(4);
255
256
        $this->assertCount(2, $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...
257
258
        // check first node info
259
        $this->assertEquals($nodeInfo[0]->getId(), 8);
260
        $this->assertEquals($nodeInfo[0]->getParentId(), 4);
261
        $this->assertEquals($nodeInfo[0]->getLeft(), 37);
262
        $this->assertEquals($nodeInfo[0]->getRight(), 38);
263
        $this->assertEquals($nodeInfo[0]->getLevel(), 2);
264
265
        // check second node info
266
        $this->assertEquals($nodeInfo[1]->getId(), 9);
267
        $this->assertEquals($nodeInfo[1]->getParentId(), 4);
268
        $this->assertEquals($nodeInfo[1]->getLeft(), 39);
269
        $this->assertEquals($nodeInfo[1]->getRight(), 48);
270
        $this->assertEquals($nodeInfo[1]->getLevel(), 2);
271
    }
272
273
    public function testUpdateNodeMetadata()
274
    {
275
        $nodeInfo = new NodeInfo(2, 100, 101, 102, 103, null);
276
277
        $this->manipulator
278
            ->updateNodeMetadata($nodeInfo);
279
280
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testUpdateNodeMetadata.xml');
281
    }
282
283
    public function testGetAncestorsReturnEmptyArrayIfNodeDoestNotExist()
284
    {
285
        $path = $this->manipulator
286
            ->getAncestors(1000);
287
288
        $this->assertEquals(array(), $path);
289
    }
290
291
    public function testGetAncestors()
292
    {
293
        $path = $this->manipulator
294
            ->getAncestors(10);
295
296
        $this->assertCount(4, $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...
297
298
        $expected = include __DIR__.'/_files/adapter/testGetAncestors.php';
299
        $this->assertEquals($expected, $path);
300
    }
301
302
    public function testGetAncestorsFromLevel()
303
    {
304
        $path = $this->manipulator
305
            ->getAncestors(10, 2);
306
307
        $this->assertCount(2, $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...
308
309
        $expected = include __DIR__.'/_files/adapter/testGetAncestorsStartFromLevel.php';
310
        $this->assertEquals($expected, $path);
311
    }
312
313
    public function testGetAncestorsExcludeLastNode()
314
    {
315
        // test exclude last node
316
        $path = $this->manipulator
317
            ->getAncestors(10, 0, 1);
318
319
        $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...
320
        $expected = include __DIR__.'/_files/adapter/testGetAncestorsExcludeLastNode.php';
321
        $this->assertEquals($expected, $path);
322
323
        // test exclude last two node
324
        $path = $this->manipulator
325
            ->getAncestors(10, 0, 2);
326
327
        $this->assertCount(2, $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...
328
        $expected = include __DIR__.'/_files/adapter/testGetAncestorsExcludeTwoLastNode.php';
329
        $this->assertEquals($expected, $path);
330
    }
331
332
    public function testGetDescendantsReturnEmptyArrayIfNodeDoesNotExist()
333
    {
334
        $nodes = $this->manipulator
335
            ->getDescendants(1000);
336
337
        $this->assertEquals(array(), $nodes);
338
    }
339
340
    public function testGetDescendants()
341
    {
342
        $nodes = $this->manipulator
343
            ->getDescendants(1);
344
345
        $this->assertCount(25, $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...
346
347
        $expected = include __DIR__.'/_files/adapter/testGetDescendants.php';
348
        $this->assertEquals($expected, $nodes);
349
    }
350
351
    public function testGetDescendantsDefinedNodeId()
352
    {
353
        $nodes = $this->manipulator
354
            ->getDescendants(6);
355
356
        $this->assertCount(8, $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...
357
358
        $expected = include __DIR__.'/_files/adapter/testGetDescendantsDefinedNodeId.php';
359
        $this->assertEquals($expected, $nodes);
360
    }
361
362
    public function testGetDescendantsFromLevel()
363
    {
364
        $nodes = $this->manipulator
365
            ->getDescendants(6, 2);
366
367
        $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...
368
369
        $expected = include __DIR__.'/_files/adapter/testGetDescendantsFromLevel.php';
370
        $this->assertEquals($expected, $nodes);
371
    }
372
373
    public function testGetDescendantsFixLevels()
374
    {
375
        $nodes = $this->manipulator
376
            ->getDescendants(6, 2, 2);
377
378
        $this->assertCount(3, $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...
379
380
        $expected = include __DIR__.'/_files/adapter/testGetDescendantsFixLevels.php';
381
        $this->assertEquals($expected, $nodes);
382
    }
383
384
    public function testGetDescendantsExcludeBranch()
385
    {
386
        $nodes = $this->manipulator
387
            ->getDescendants(1, 0, null, 9);
388
389
        $this->assertCount(20, $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...
390
391
        $expected = include __DIR__.'/_files/adapter/testGetDescendantsExcludeBranch.php';
392
        $this->assertEquals($expected, $nodes);
393
    }
394
}
395