ManipulatorJoinTableTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 88
c 0
b 0
f 0
dl 0
loc 151
rs 10
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 4 1
A getDataSet() 0 3 1
A testGetNodeInfo() 0 11 1
A testGetAncestors() 0 28 1
A testGetDescendants() 0 28 1
A testGetChildrenNodeInfo() 0 6 1
A getManipulator() 0 22 2
A setUp() 0 5 1
A testGetNode() 0 18 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\Options;
10
use StefanoTreeTest\IntegrationTestCase;
11
use StefanoTreeTest\TestUtil;
12
13
/**
14
 * @internal
15
 */
16
class ManipulatorJoinTableTest extends IntegrationTestCase
17
{
18
    /**
19
     * @var ManipulatorInterface
20
     */
21
    protected $manipulator;
22
23
    protected function setUp(): void
24
    {
25
        $this->manipulator = $this->getManipulator();
26
27
        parent::setUp();
28
    }
29
30
    protected function tearDown(): void
31
    {
32
        $this->manipulator = null;
33
        parent::tearDown();
34
    }
35
36
    /**
37
     * @return ManipulatorInterface
38
     */
39
    protected function getManipulator(): ManipulatorInterface
40
    {
41
        $options = new Options(array(
42
            'tableName' => 'tree_traversal_with_scope',
43
            'idColumnName' => 'tree_traversal_id',
44
            'scopeColumnName' => 'scope',
45
            'dbSelectBuilder' => function () {
46
                $sql = 'SELECT tree_traversal_with_scope.*, ttm.name AS metadata FROM tree_traversal_with_scope'
47
                    .' LEFT JOIN tree_traversal_metadata AS ttm'
48
                    .' ON ttm.tree_traversal_id = tree_traversal_with_scope.tree_traversal_id';
49
50
                return $sql;
51
            },
52
        ));
53
54
        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...
55
            $options->setSequenceName('tree_traversal_with_scope_tree_traversal_id_seq');
56
        }
57
58
        $manipulator = new Manipulator($options, TestUtil::buildAdapter($options));
59
60
        return $manipulator;
61
    }
62
63
    protected function getDataSet()
64
    {
65
        return $this->createArrayDataSet(include __DIR__.'/_files/adapter/join_table/initDataSet.php');
66
    }
67
68
    public function testGetNode()
69
    {
70
        $nodes = $this->manipulator
71
            ->getDescendants(10);
72
73
        $expected = array(
74
            array(
75
                'tree_traversal_id' => 10,
76
                'name' => null,
77
                'lft' => 5,
78
                'rgt' => 6,
79
                'parent_id' => 9,
80
                'level' => 4,
81
                'scope' => 1,
82
                'metadata' => 'meta-10',
83
            ),
84
        );
85
        $this->assertEquals($expected, $nodes);
86
    }
87
88
    public function testGetAncestors()
89
    {
90
        $nodes = $this->manipulator
91
            ->getAncestors(10, 2, 1);
92
93
        $expected = array(
94
            array(
95
                'tree_traversal_id' => 8,
96
                'name' => null,
97
                'lft' => 3,
98
                'rgt' => 8,
99
                'parent_id' => 7,
100
                'level' => 2,
101
                'scope' => 1,
102
                'metadata' => null,
103
            ),
104
            array(
105
                'tree_traversal_id' => 9,
106
                'name' => null,
107
                'lft' => 4,
108
                'rgt' => 7,
109
                'parent_id' => 8,
110
                'level' => 3,
111
                'scope' => 1,
112
                'metadata' => 'meta-9',
113
            ),
114
        );
115
        $this->assertEquals($expected, $nodes);
116
    }
117
118
    public function testGetDescendants()
119
    {
120
        $nodes = $this->manipulator
121
            ->getDescendants(2, 1, 1, 4);
122
123
        $expected = array(
124
            array(
125
                'tree_traversal_id' => 3,
126
                'name' => null,
127
                'lft' => 3,
128
                'rgt' => 4,
129
                'parent_id' => 2,
130
                'level' => 2,
131
                'scope' => 2,
132
                'metadata' => null,
133
            ),
134
            array(
135
                'tree_traversal_id' => 5,
136
                'name' => null,
137
                'lft' => 7,
138
                'rgt' => 8,
139
                'parent_id' => 2,
140
                'level' => 2,
141
                'scope' => 2,
142
                'metadata' => null,
143
            ),
144
        );
145
        $this->assertEquals($expected, $nodes);
146
    }
147
148
    public function testGetChildrenNodeInfo()
149
    {
150
        $nodes = $this->manipulator
151
            ->getChildrenNodeInfo(2);
152
153
        $this->assertEquals(3, count($nodes));
154
    }
155
156
    public function testGetNodeInfo()
157
    {
158
        $nodeInfo = $this->manipulator
159
            ->getNodeInfo(2);
160
161
        $this->assertEquals($nodeInfo->getId(), 2);
162
        $this->assertEquals($nodeInfo->getParentId(), 1);
163
        $this->assertEquals($nodeInfo->getLeft(), 2);
164
        $this->assertEquals($nodeInfo->getRight(), 9);
165
        $this->assertEquals($nodeInfo->getLevel(), 1);
166
        $this->assertEquals($nodeInfo->getScope(), 2);
167
    }
168
}
169