testGetDescendantsReturnEmptyArrayIfNodeDoesNotExist()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
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 ManipulatorTest 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',
44
            'idColumnName' => 'tree_traversal_id',
45
        ));
46
47
        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...
48
            $options->setSequenceName('tree_traversal_tree_traversal_id_seq');
49
        }
50
51
        return new Manipulator($options, TestUtil::buildAdapter($options));
52
    }
53
54
    protected function getDataSet()
55
    {
56
        return $this->createArrayDataSet(include __DIR__.'/_files/adapter/initDataSet.php');
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.php');
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.php');
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.php');
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.php');
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.php');
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.php');
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.php');
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.php');
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.php');
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.php');
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.php');
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);
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.php');
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);
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);
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);
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);
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);
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);
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);
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);
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);
393
394
        $expected = include __DIR__.'/_files/adapter/testGetDescendantsExcludeBranch.php';
395
        $this->assertEquals($expected, $nodes);
396
    }
397
}
398