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