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