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

ManipulatorTest::testGetNodeInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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