bartko-s /
stefano-tree
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace StefanoTreeTest\Integration; |
||
| 6 | |||
| 7 | use StefanoTree\NestedSet as TreeAdapter; |
||
| 8 | use StefanoTree\NestedSet\Options; |
||
| 9 | use StefanoTreeTest\IntegrationTestCase; |
||
| 10 | use StefanoTreeTest\TestUtil; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @internal |
||
| 14 | */ |
||
| 15 | class NestedSetWithScopeTest extends IntegrationTestCase |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var TreeAdapter |
||
| 19 | */ |
||
| 20 | protected $treeAdapter; |
||
| 21 | |||
| 22 | protected function setUp(): void |
||
| 23 | { |
||
| 24 | $this->treeAdapter = $this->getTreeAdapter(); |
||
| 25 | |||
| 26 | parent::setUp(); |
||
| 27 | } |
||
| 28 | |||
| 29 | protected function tearDown(): void |
||
| 30 | { |
||
| 31 | $this->treeAdapter = null; |
||
| 32 | parent::tearDown(); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @return TreeAdapter |
||
| 37 | */ |
||
| 38 | protected function getTreeAdapter() |
||
| 39 | { |
||
| 40 | $options = new Options(array( |
||
| 41 | 'tableName' => 'tree_traversal_with_scope', |
||
| 42 | 'idColumnName' => 'tree_traversal_id', |
||
| 43 | 'scopeColumnName' => 'scope', |
||
| 44 | )); |
||
| 45 | |||
| 46 | if ('pgsql' == TEST_STEFANO_DB_VENDOR) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 47 | $options->setSequenceName('tree_traversal_with_scope_tree_traversal_id_seq'); |
||
| 48 | } |
||
| 49 | |||
| 50 | return new TreeAdapter($options, TestUtil::buildAdapter($options)); |
||
| 51 | } |
||
| 52 | |||
| 53 | protected function getDataSet() |
||
| 54 | { |
||
| 55 | switch ($this->getName()) { |
||
| 56 | case 'testInvalidTree': |
||
| 57 | case 'testRebuildTree': |
||
| 58 | return $this->createArrayDataSet(include __DIR__.'/_files/NestedSet/with_scope/initDataSetBrokenTreeIndexes.php'); |
||
| 59 | |||
| 60 | default: |
||
| 61 | return $this->createArrayDataSet(include __DIR__.'/_files/NestedSet/with_scope/initDataSet.php'); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | public function testCreateRoot() |
||
| 66 | { |
||
| 67 | $this->treeAdapter |
||
| 68 | ->createRootNode(array(), 10); |
||
| 69 | |||
| 70 | $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testCreateRoot.php'); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function testCreateRootRootWithSomeScopeAlreadyExist() |
||
| 74 | { |
||
| 75 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 76 | $this->expectExceptionMessage('Root node for given scope already exist'); |
||
| 77 | |||
| 78 | $this->treeAdapter |
||
| 79 | ->createRootNode(array(), 123); |
||
| 80 | $this->treeAdapter |
||
| 81 | ->createRootNode(array(), 123); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function testGetRoots() |
||
| 85 | { |
||
| 86 | $expected = array( |
||
| 87 | array( |
||
| 88 | 'tree_traversal_id' => 1, |
||
| 89 | 'name' => null, |
||
| 90 | 'lft' => 1, |
||
| 91 | 'rgt' => 10, |
||
| 92 | 'parent_id' => 0, |
||
| 93 | 'level' => 0, |
||
| 94 | 'scope' => 2, |
||
| 95 | ), |
||
| 96 | array( |
||
| 97 | 'tree_traversal_id' => 6, |
||
| 98 | 'name' => null, |
||
| 99 | 'lft' => 1, |
||
| 100 | 'rgt' => 6, |
||
| 101 | 'parent_id' => 0, |
||
| 102 | 'level' => 0, |
||
| 103 | 'scope' => 1, |
||
| 104 | ), |
||
| 105 | ); |
||
| 106 | |||
| 107 | $roots = $this->treeAdapter |
||
| 108 | ->getRoots(); |
||
| 109 | |||
| 110 | $this->assertEquals($expected, $roots); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function testAddNodePlacementChildTopDefaultPlacement() |
||
| 114 | { |
||
| 115 | $lastGeneratedValue = $this->treeAdapter |
||
| 116 | ->addNode(1); |
||
| 117 | |||
| 118 | $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testAddNodePlacementChildTop.php'); |
||
| 119 | $this->assertEquals(9, $lastGeneratedValue); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function testMoveNodePlacementBottom() |
||
| 123 | { |
||
| 124 | $this->treeAdapter |
||
| 125 | ->moveNode(3, 5, TreeAdapter::PLACEMENT_BOTTOM); |
||
| 126 | |||
| 127 | $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testMoveNodePlacementBottom.php'); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function testCannotMoveNodeBetweenScopes() |
||
| 131 | { |
||
| 132 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 133 | $this->expectExceptionMessage('Cannot move node between scopes.'); |
||
| 134 | |||
| 135 | $this->treeAdapter |
||
| 136 | ->moveNode(4, 8, TreeAdapter::PLACEMENT_CHILD_BOTTOM); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function testDeleteBranch() |
||
| 140 | { |
||
| 141 | $this->treeAdapter |
||
| 142 | ->deleteBranch(2); |
||
| 143 | |||
| 144 | $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testDeleteBranch.php'); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function testGetDescendants() |
||
| 148 | { |
||
| 149 | $expectedNodeData = array( |
||
| 150 | array( |
||
| 151 | 'tree_traversal_id' => '2', |
||
| 152 | 'name' => null, |
||
| 153 | 'lft' => '2', |
||
| 154 | 'rgt' => '9', |
||
| 155 | 'parent_id' => '1', |
||
| 156 | 'level' => '1', |
||
| 157 | 'scope' => '2', |
||
| 158 | ), |
||
| 159 | array( |
||
| 160 | 'tree_traversal_id' => '3', |
||
| 161 | 'name' => null, |
||
| 162 | 'lft' => '3', |
||
| 163 | 'rgt' => '4', |
||
| 164 | 'parent_id' => '2', |
||
| 165 | 'level' => '2', |
||
| 166 | 'scope' => '2', |
||
| 167 | ), |
||
| 168 | array( |
||
| 169 | 'tree_traversal_id' => '4', |
||
| 170 | 'name' => null, |
||
| 171 | 'lft' => '5', |
||
| 172 | 'rgt' => '6', |
||
| 173 | 'parent_id' => '2', |
||
| 174 | 'level' => '2', |
||
| 175 | 'scope' => '2', |
||
| 176 | ), |
||
| 177 | array( |
||
| 178 | 'tree_traversal_id' => '5', |
||
| 179 | 'name' => null, |
||
| 180 | 'lft' => '7', |
||
| 181 | 'rgt' => '8', |
||
| 182 | 'parent_id' => '2', |
||
| 183 | 'level' => '2', |
||
| 184 | 'scope' => '2', |
||
| 185 | ), |
||
| 186 | ); |
||
| 187 | |||
| 188 | $nodeData = $this->treeAdapter |
||
| 189 | ->getDescendantsQueryBuilder() |
||
| 190 | ->get(2); |
||
| 191 | |||
| 192 | $this->assertEquals($expectedNodeData, $nodeData); |
||
| 193 | } |
||
| 194 | |||
| 195 | public function testGetAncestors() |
||
| 196 | { |
||
| 197 | $expectedNodeData = array( |
||
| 198 | array( |
||
| 199 | 'tree_traversal_id' => '1', |
||
| 200 | 'name' => null, |
||
| 201 | 'lft' => '1', |
||
| 202 | 'rgt' => '10', |
||
| 203 | 'parent_id' => null, |
||
| 204 | 'level' => '0', |
||
| 205 | 'scope' => '2', |
||
| 206 | ), |
||
| 207 | array( |
||
| 208 | 'tree_traversal_id' => '2', |
||
| 209 | 'name' => null, |
||
| 210 | 'lft' => '2', |
||
| 211 | 'rgt' => '9', |
||
| 212 | 'parent_id' => '1', |
||
| 213 | 'level' => '1', |
||
| 214 | 'scope' => '2', |
||
| 215 | ), |
||
| 216 | array( |
||
| 217 | 'tree_traversal_id' => '5', |
||
| 218 | 'name' => null, |
||
| 219 | 'lft' => '7', |
||
| 220 | 'rgt' => '8', |
||
| 221 | 'parent_id' => '2', |
||
| 222 | 'level' => '2', |
||
| 223 | 'scope' => '2', |
||
| 224 | ), |
||
| 225 | ); |
||
| 226 | |||
| 227 | $nodeData = $this->treeAdapter |
||
| 228 | ->getAncestorsQueryBuilder() |
||
| 229 | ->get(5); |
||
| 230 | $this->assertEquals($expectedNodeData, $nodeData); |
||
| 231 | } |
||
| 232 | |||
| 233 | public function testUpdateCannotCorruptTreeStructure() |
||
| 234 | { |
||
| 235 | $excepted = array( |
||
| 236 | 'tree_traversal_id' => 4, |
||
| 237 | 'name' => 'updated', |
||
| 238 | 'lft' => 5, |
||
| 239 | 'rgt' => 6, |
||
| 240 | 'parent_id' => 2, |
||
| 241 | 'level' => 2, |
||
| 242 | 'scope' => 2, |
||
| 243 | ); |
||
| 244 | |||
| 245 | $data = array( |
||
| 246 | 'tree_traversal_id' => 'corrupt data', |
||
| 247 | 'name' => 'updated', |
||
| 248 | 'lft' => 'corrupt data', |
||
| 249 | 'rgt' => 'corrupt data', |
||
| 250 | 'parent_id' => 'corrupt data', |
||
| 251 | 'level' => 'corrupt data', |
||
| 252 | 'scope' => 'corrupt data', |
||
| 253 | ); |
||
| 254 | $this->treeAdapter |
||
| 255 | ->updateNode(4, $data); |
||
| 256 | |||
| 257 | $this->assertEquals($excepted, $this->treeAdapter->getNode(4)); |
||
| 258 | } |
||
| 259 | |||
| 260 | public function testIsTreeValid() |
||
| 261 | { |
||
| 262 | $this->assertTrue($this->treeAdapter->isValid(1)); |
||
| 263 | } |
||
| 264 | |||
| 265 | public function testInvalidTree() |
||
| 266 | { |
||
| 267 | $this->assertFalse($this->treeAdapter->isValid(1)); |
||
| 268 | } |
||
| 269 | |||
| 270 | public function testValidateTreeGivenNodeIdIsNotRoot() |
||
| 271 | { |
||
| 272 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 273 | $this->expectExceptionMessage('Given node is not root node.'); |
||
| 274 | |||
| 275 | $this->treeAdapter->isValid(2); |
||
| 276 | } |
||
| 277 | |||
| 278 | public function testRebuildTree() |
||
| 279 | { |
||
| 280 | $this->treeAdapter |
||
| 281 | ->rebuild(1); |
||
| 282 | |||
| 283 | $this->assertCompareDataSet(array('tree_traversal_with_scope'), __DIR__.'/_files/NestedSet/with_scope/testRebuildTree.php'); |
||
| 284 | } |
||
| 285 | |||
| 286 | public function testRebuildTreeGivenNodeIdIsNotRoot() |
||
| 287 | { |
||
| 288 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 289 | $this->expectExceptionMessage('Given node is not root node.'); |
||
| 290 | |||
| 291 | $this->treeAdapter->rebuild(5); |
||
| 292 | } |
||
| 293 | |||
| 294 | public function testIsValidTreeGivenNodeIdIsNotRoot() |
||
| 295 | { |
||
| 296 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 297 | $this->expectExceptionMessage('Given node is not root node.'); |
||
| 298 | |||
| 299 | $this->treeAdapter->isValid(4); |
||
| 300 | } |
||
| 301 | |||
| 302 | public function testRebuildTreeGivenNodeIdDoesNotExists() |
||
| 303 | { |
||
| 304 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 305 | $this->expectExceptionMessage('Node does not exists.'); |
||
| 306 | |||
| 307 | $this->treeAdapter->rebuild(999); |
||
| 308 | } |
||
| 309 | |||
| 310 | public function testIsValidTreeGivenNodeIdDoesNotExists() |
||
| 311 | { |
||
| 312 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 313 | $this->expectExceptionMessage('Node does not exists.'); |
||
| 314 | |||
| 315 | $this->treeAdapter->isValid(555); |
||
| 316 | } |
||
| 317 | } |
||
| 318 |