Complex classes like AbstractTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | abstract class AbstractTest extends IntegrationTestCase |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var TreeAdapter |
||
| 14 | */ |
||
| 15 | protected $treeAdapter; |
||
| 16 | |||
| 17 | protected function setUp() |
||
| 18 | { |
||
| 19 | $this->treeAdapter = $this->getTreeAdapter(); |
||
| 20 | |||
| 21 | parent::setUp(); |
||
| 22 | } |
||
| 23 | |||
| 24 | protected function tearDown() |
||
| 25 | { |
||
| 26 | $this->treeAdapter = null; |
||
| 27 | parent::tearDown(); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @return TreeAdapter |
||
| 32 | */ |
||
| 33 | abstract protected function getTreeAdapter(); |
||
| 34 | |||
| 35 | protected function getDataSet() |
||
| 36 | { |
||
| 37 | switch ($this->getName()) { |
||
| 38 | case 'testCreateRootNode': |
||
| 39 | case 'testCreateRootNodeWithCustomData': |
||
| 40 | case 'testGetRootNodeRootDoesNotExist': |
||
| 41 | return $this->createMySQLXMLDataSet(__DIR__.'/_files/NestedSet/initEmptyDataSet.xml'); |
||
| 42 | default: |
||
| 43 | return $this->createMySQLXMLDataSet(__DIR__.'/_files/NestedSet/initDataSet.xml'); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | public function testCreateRootNode() |
||
| 48 | { |
||
| 49 | $newId = $this->treeAdapter |
||
| 50 | ->createRootNode(); |
||
| 51 | |||
| 52 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testCreateRootNode.xml'); |
||
| 53 | $this->assertEquals(1, $newId); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function testCreateRootNodeWithCustomData() |
||
| 57 | { |
||
| 58 | $newId = $this->treeAdapter |
||
| 59 | ->createRootNode(array('name' => 'This is root node')); |
||
| 60 | |||
| 61 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testCreateRootNodeWithCustomData.xml'); |
||
| 62 | $this->assertEquals(1, $newId); |
||
| 63 | } |
||
| 64 | |||
| 65 | public function testCreateRootRootAlreadyExist() |
||
| 66 | { |
||
| 67 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 68 | $this->expectExceptionMessage('Root node already exist'); |
||
| 69 | |||
| 70 | $this->treeAdapter |
||
| 71 | ->createRootNode(); |
||
| 72 | $this->treeAdapter |
||
| 73 | ->createRootNode(); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function testGetNode() |
||
| 77 | { |
||
| 78 | $expectedNodeData = array( |
||
| 79 | 'tree_traversal_id' => '12', |
||
| 80 | 'name' => null, |
||
| 81 | 'lft' => '18', |
||
| 82 | 'rgt' => '29', |
||
| 83 | 'parent_id' => '6', |
||
| 84 | 'level' => '3', |
||
| 85 | ); |
||
| 86 | |||
| 87 | $nodeData = $this->treeAdapter |
||
| 88 | ->getNode(12); |
||
| 89 | |||
| 90 | $this->assertEquals($expectedNodeData, $nodeData); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function testGetNodeNodeDoesNotExist() |
||
| 94 | { |
||
| 95 | $this->assertNull($this->treeAdapter->getNode(123456789)); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function testAddNodeTargetNodeDoesNotExist() |
||
| 99 | { |
||
| 100 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 101 | $this->expectExceptionMessage('Target Node does not exists.'); |
||
| 102 | |||
| 103 | try { |
||
| 104 | $this->treeAdapter |
||
| 105 | ->addNode(123456789, array(), TreeAdapter::PLACEMENT_BOTTOM); |
||
| 106 | } catch (\Exception $e) { |
||
| 107 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.xml'); |
||
| 108 | throw $e; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | public function testCreateNodePlacementStrategyDoesNotExists() |
||
| 113 | { |
||
| 114 | $this->expectException(\StefanoTree\Exception\InvalidArgumentException::class); |
||
| 115 | $this->expectExceptionMessage('Unknown placement "unknown-placement"'); |
||
| 116 | |||
| 117 | $this->treeAdapter |
||
| 118 | ->addNode(1, array(), 'unknown-placement'); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function testAddNodePlacementBottomTargetNodeIsRoot() |
||
| 122 | { |
||
| 123 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 124 | $this->expectExceptionMessage('Cannot create node. Target node is root. Root node cannot have sibling.'); |
||
| 125 | |||
| 126 | try { |
||
| 127 | $this->treeAdapter |
||
| 128 | ->addNode(1, array(), TreeAdapter::PLACEMENT_BOTTOM); |
||
| 129 | } catch (\Exception $e) { |
||
| 130 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.xml'); |
||
| 131 | throw $e; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | public function testAddNodePlacementTopTargetNodeIsRoot() |
||
| 136 | { |
||
| 137 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 138 | $this->expectExceptionMessage('Cannot create node. Target node is root. Root node cannot have sibling.'); |
||
| 139 | |||
| 140 | try { |
||
| 141 | $this->treeAdapter |
||
| 142 | ->addNode(1, array(), TreeAdapter::PLACEMENT_TOP); |
||
| 143 | } catch (\Exception $e) { |
||
| 144 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.xml'); |
||
| 145 | throw $e; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | public function testAddNodePlacementBottom() |
||
| 150 | { |
||
| 151 | //test 1 |
||
| 152 | $lastGeneratedValue = $this->treeAdapter |
||
| 153 | ->addNode(12, array(), TreeAdapter::PLACEMENT_BOTTOM); |
||
| 154 | |||
| 155 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementBottom-1.xml'); |
||
| 156 | $this->assertEquals(26, $lastGeneratedValue); |
||
| 157 | |||
| 158 | //test 2 with data |
||
| 159 | $data = array( |
||
| 160 | 'name' => 'ahoj', |
||
| 161 | ); |
||
| 162 | |||
| 163 | $lastGeneratedValue = $this->treeAdapter |
||
| 164 | ->addNode(19, $data, TreeAdapter::PLACEMENT_BOTTOM); |
||
| 165 | |||
| 166 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementBottom-2.xml'); |
||
| 167 | $this->assertEquals(27, $lastGeneratedValue); |
||
| 168 | } |
||
| 169 | |||
| 170 | public function testAddNodePlacementTop() |
||
| 171 | { |
||
| 172 | //test 1 |
||
| 173 | $lastGeneratedValue = $this->treeAdapter |
||
| 174 | ->addNode(16, array(), TreeAdapter::PLACEMENT_TOP); |
||
| 175 | |||
| 176 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementTop-1.xml'); |
||
| 177 | $this->assertEquals(26, $lastGeneratedValue); |
||
| 178 | |||
| 179 | //test 2 with data |
||
| 180 | $data = array( |
||
| 181 | 'name' => 'ahoj', |
||
| 182 | ); |
||
| 183 | $lastGeneratedValue = $this->treeAdapter |
||
| 184 | ->addNode(3, $data, TreeAdapter::PLACEMENT_TOP); |
||
| 185 | |||
| 186 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementTop-2.xml'); |
||
| 187 | $this->assertEquals(27, $lastGeneratedValue); |
||
| 188 | } |
||
| 189 | |||
| 190 | public function testAddNodePlacementChildBottom() |
||
| 191 | { |
||
| 192 | //test 1 |
||
| 193 | $lastGeneratedValue = $this->treeAdapter |
||
| 194 | ->addNode(21, array(), TreeAdapter::PLACEMENT_CHILD_BOTTOM); |
||
| 195 | |||
| 196 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementChildBottom-1.xml'); |
||
| 197 | $this->assertEquals(26, $lastGeneratedValue); |
||
| 198 | |||
| 199 | //test 2 with data |
||
| 200 | $data = array( |
||
| 201 | 'name' => 'ahoj', |
||
| 202 | ); |
||
| 203 | $lastGeneratedValue = $this->treeAdapter |
||
| 204 | ->addNode(4, $data, TreeAdapter::PLACEMENT_CHILD_BOTTOM); |
||
| 205 | |||
| 206 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementChildBottom-2.xml'); |
||
| 207 | $this->assertEquals(27, $lastGeneratedValue); |
||
| 208 | } |
||
| 209 | |||
| 210 | public function testAddNodePlacementChildTopDefaultPlacement() |
||
| 211 | { |
||
| 212 | //test 1 |
||
| 213 | $lastGeneratedValue = $this->treeAdapter |
||
| 214 | ->addNode(4); |
||
| 215 | |||
| 216 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementChildTop-1.xml'); |
||
| 217 | $this->assertEquals(26, $lastGeneratedValue); |
||
| 218 | |||
| 219 | //test 2 with data |
||
| 220 | $data = array( |
||
| 221 | 'name' => 'ahoj', |
||
| 222 | ); |
||
| 223 | $lastGeneratedValue = $this->treeAdapter |
||
| 224 | ->addNode(10, $data); |
||
| 225 | |||
| 226 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testAddNodePlacementChildTop-2.xml'); |
||
| 227 | $this->assertEquals(27, $lastGeneratedValue); |
||
| 228 | } |
||
| 229 | |||
| 230 | public function testDeleteBranchDoesNotExist() |
||
| 231 | { |
||
| 232 | $this->treeAdapter |
||
| 233 | ->deleteBranch(123456789); |
||
| 234 | |||
| 235 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.xml'); |
||
| 236 | } |
||
| 237 | |||
| 238 | public function testDeleteBranch() |
||
| 239 | { |
||
| 240 | $this->treeAdapter |
||
| 241 | ->deleteBranch(6); |
||
| 242 | |||
| 243 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testDeleteBranch.xml'); |
||
| 244 | } |
||
| 245 | |||
| 246 | public function testMoveNodeCannotMoveTargetNodeIsInsideSourceBranch() |
||
| 247 | { |
||
| 248 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 249 | $this->expectExceptionMessage('Cannot move. Target node is inside source branch.'); |
||
| 250 | |||
| 251 | try { |
||
| 252 | $this->treeAdapter |
||
| 253 | ->moveNode(1, 12); |
||
| 254 | } catch (\Exception $e) { |
||
| 255 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.xml'); |
||
| 256 | throw $e; |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | public function testMoveNodeCannotMoveTargetAndSourceNodeAreEqual() |
||
| 261 | { |
||
| 262 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 263 | $this->expectExceptionMessage('Cannot move. Source node and Target node are equal.'); |
||
| 264 | |||
| 265 | try { |
||
| 266 | $this->treeAdapter |
||
| 267 | ->moveNode(10, 10); |
||
| 268 | } catch (\Exception $e) { |
||
| 269 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.xml'); |
||
| 270 | throw $e; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | public function testMoveNodeCannotMoveTargetNodeDoesNotExist() |
||
| 275 | { |
||
| 276 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 277 | $this->expectExceptionMessage('Cannot move. Target node does not exists.'); |
||
| 278 | |||
| 279 | try { |
||
| 280 | $this->treeAdapter |
||
| 281 | ->moveNode(5, 123456); |
||
| 282 | } catch (\Exception $e) { |
||
| 283 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.xml'); |
||
| 284 | throw $e; |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | public function testMoveNodeCannotMoveSourceNodeDoesNotExist() |
||
| 289 | { |
||
| 290 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 291 | $this->expectExceptionMessage('Cannot move. Source node does not exists.'); |
||
| 292 | |||
| 293 | try { |
||
| 294 | $this->treeAdapter |
||
| 295 | ->moveNode(123456, 6); |
||
| 296 | } catch (\Exception $e) { |
||
| 297 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.xml'); |
||
| 298 | throw $e; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | public function testMoveNodePlacementStrategyDoesNotExists() |
||
| 303 | { |
||
| 304 | $this->expectException(\StefanoTree\Exception\InvalidArgumentException::class); |
||
| 305 | $this->expectExceptionMessage('Unknown placement "unknown-placement"'); |
||
| 306 | |||
| 307 | $this->treeAdapter |
||
| 308 | ->moveNode(11, 1, 'unknown-placement'); |
||
| 309 | } |
||
| 310 | |||
| 311 | public function placementDataProvider() |
||
| 312 | { |
||
| 313 | return [ |
||
| 314 | [\StefanoTree\TreeInterface::PLACEMENT_TOP], |
||
| 315 | [\StefanoTree\TreeInterface::PLACEMENT_BOTTOM], |
||
| 316 | ]; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @dataProvider placementDataProvider |
||
| 321 | * |
||
| 322 | * @param string $placement |
||
| 323 | * |
||
| 324 | * @throws \Exception |
||
| 325 | */ |
||
| 326 | public function testMoveNodeCannotCreateSiblingNodeAtRootNode(string $placement) |
||
| 327 | { |
||
| 328 | $this->expectException(\StefanoTree\Exception\ValidationException::class); |
||
| 329 | $this->expectExceptionMessage('Cannot move. Target node is root. Root node cannot have sibling.'); |
||
| 330 | |||
| 331 | try { |
||
| 332 | $this->treeAdapter |
||
| 333 | ->moveNode(11, 1, $placement); |
||
| 334 | } catch (\Exception $e) { |
||
| 335 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.xml'); |
||
| 336 | throw $e; |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | public function testMoveNodePlacementBottom() |
||
| 341 | { |
||
| 342 | //test source node is already at required position |
||
| 343 | $this->treeAdapter |
||
| 344 | ->moveNode(3, 2, TreeAdapter::PLACEMENT_BOTTOM); |
||
| 345 | |||
| 346 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.xml'); |
||
| 347 | |||
| 348 | //test |
||
| 349 | $this->treeAdapter |
||
| 350 | ->moveNode(14, 18, TreeAdapter::PLACEMENT_BOTTOM); |
||
| 351 | |||
| 352 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementBottom-1.xml'); |
||
| 353 | |||
| 354 | //test |
||
| 355 | $this->treeAdapter |
||
| 356 | ->moveNode(16, 7, TreeAdapter::PLACEMENT_BOTTOM); |
||
| 357 | |||
| 358 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementBottom-2.xml'); |
||
| 359 | |||
| 360 | //test |
||
| 361 | $this->treeAdapter |
||
| 362 | ->moveNode(14, 3, TreeAdapter::PLACEMENT_BOTTOM); |
||
| 363 | |||
| 364 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementBottom-3.xml'); |
||
| 365 | } |
||
| 366 | |||
| 367 | public function testMoveNodePlacementTop() |
||
| 368 | { |
||
| 369 | //test source node is already at required position |
||
| 370 | $this->treeAdapter |
||
| 371 | ->moveNode(3, 4, TreeAdapter::PLACEMENT_TOP); |
||
| 372 | |||
| 373 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.xml'); |
||
| 374 | |||
| 375 | //test |
||
| 376 | $this->treeAdapter |
||
| 377 | ->moveNode(19, 12, TreeAdapter::PLACEMENT_TOP); |
||
| 378 | |||
| 379 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementTop-1.xml'); |
||
| 380 | |||
| 381 | //test |
||
| 382 | $this->treeAdapter |
||
| 383 | ->moveNode(10, 18, TreeAdapter::PLACEMENT_TOP); |
||
| 384 | |||
| 385 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementTop-2.xml'); |
||
| 386 | |||
| 387 | //test |
||
| 388 | $this->treeAdapter |
||
| 389 | ->moveNode(21, 6, TreeAdapter::PLACEMENT_TOP); |
||
| 390 | |||
| 391 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementTop-3.xml'); |
||
| 392 | } |
||
| 393 | |||
| 394 | public function testMoveNodePlacementChildBottom() |
||
| 395 | { |
||
| 396 | //test source node is already at required position |
||
| 397 | $this->treeAdapter |
||
| 398 | ->moveNode(22, 18, TreeAdapter::PLACEMENT_CHILD_BOTTOM); |
||
| 399 | |||
| 400 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.xml'); |
||
| 401 | |||
| 402 | //test |
||
| 403 | $this->treeAdapter |
||
| 404 | ->moveNode(9, 12, TreeAdapter::PLACEMENT_CHILD_BOTTOM); |
||
| 405 | |||
| 406 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementChildBottom-1.xml'); |
||
| 407 | |||
| 408 | //test |
||
| 409 | $this->treeAdapter |
||
| 410 | ->moveNode(10, 3, TreeAdapter::PLACEMENT_CHILD_BOTTOM); |
||
| 411 | |||
| 412 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementChildBottom-2.xml'); |
||
| 413 | |||
| 414 | //test |
||
| 415 | $this->treeAdapter |
||
| 416 | ->moveNode(21, 12, TreeAdapter::PLACEMENT_CHILD_BOTTOM); |
||
| 417 | |||
| 418 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementChildBottom-3.xml'); |
||
| 419 | } |
||
| 420 | |||
| 421 | public function testMoveNodePlacementChildTopDefaultPlacement() |
||
| 422 | { |
||
| 423 | //test source node is already at required position |
||
| 424 | $this->treeAdapter |
||
| 425 | ->moveNode(21, 18); |
||
| 426 | |||
| 427 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/initDataSetWithIds.xml'); |
||
| 428 | |||
| 429 | //test |
||
| 430 | $this->treeAdapter |
||
| 431 | ->moveNode(9, 21); |
||
| 432 | |||
| 433 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementChildTop-1.xml'); |
||
| 434 | |||
| 435 | //test |
||
| 436 | $this->treeAdapter |
||
| 437 | ->moveNode(16, 3); |
||
| 438 | |||
| 439 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementChildTop-2.xml'); |
||
| 440 | |||
| 441 | //test |
||
| 442 | $this->treeAdapter |
||
| 443 | ->moveNode(18, 3); |
||
| 444 | |||
| 445 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testMoveNodePlacementChildTop-3.xml'); |
||
| 446 | } |
||
| 447 | |||
| 448 | public function testGetPathReturnEmptyArrayIfNodeDoesNotExist() |
||
| 449 | { |
||
| 450 | $return = $this->treeAdapter |
||
| 451 | ->getPath(123456789); |
||
| 452 | $this->assertEquals(array(), $return); |
||
| 453 | } |
||
| 454 | |||
| 455 | public function testGetPathReturnEmptyArrayIfNodeExistButHasNoPath() |
||
| 456 | { |
||
| 457 | $return = $this->treeAdapter |
||
| 458 | ->getPath(1, 0, true); |
||
| 459 | $this->assertEquals(array(), $return); |
||
| 460 | } |
||
| 461 | |||
| 462 | public function testGetPath() |
||
| 541 | |||
| 542 | public function testGetDescendantsReturnEmptyArrayIfNodeDoesNotExist() |
||
| 543 | { |
||
| 544 | $return = $this->treeAdapter |
||
| 545 | ->getDescendants(123456789); |
||
| 546 | $this->assertEquals(array(), $return); |
||
| 547 | } |
||
| 548 | |||
| 549 | public function testGetDescendantsReturnEmptyArrayNodeDoesNotHaveDescendants() |
||
| 550 | { |
||
| 551 | $return = $this->treeAdapter |
||
| 552 | ->getDescendants(8, 1); |
||
| 553 | $this->assertEquals(array(), $return); |
||
| 554 | } |
||
| 555 | |||
| 556 | public function testGetDescendants() |
||
| 557 | { |
||
| 558 | //test whole branche |
||
| 559 | $return = $this->treeAdapter |
||
| 560 | ->getDescendants(21); |
||
| 561 | $expected = array( |
||
| 562 | array( |
||
| 563 | 'tree_traversal_id' => '21', |
||
| 564 | 'name' => null, |
||
| 565 | 'lft' => '20', |
||
| 566 | 'rgt' => '25', |
||
| 567 | 'parent_id' => '18', |
||
| 568 | 'level' => '5', |
||
| 569 | ), |
||
| 570 | array( |
||
| 571 | 'tree_traversal_id' => '24', |
||
| 572 | 'name' => null, |
||
| 573 | 'lft' => '21', |
||
| 574 | 'rgt' => '22', |
||
| 575 | 'parent_id' => '21', |
||
| 576 | 'level' => '6', |
||
| 577 | ), |
||
| 578 | array( |
||
| 579 | 'tree_traversal_id' => '25', |
||
| 580 | 'name' => null, |
||
| 581 | 'lft' => '23', |
||
| 582 | 'rgt' => '24', |
||
| 583 | 'parent_id' => '21', |
||
| 584 | 'level' => '6', |
||
| 585 | ), |
||
| 586 | ); |
||
| 587 | $this->assertEquals($expected, $return); |
||
| 588 | |||
| 589 | //test different start node |
||
| 590 | $return = $this->treeAdapter |
||
| 591 | ->getDescendants(6, 3); |
||
| 592 | $expected = array( |
||
| 593 | array( |
||
| 594 | 'tree_traversal_id' => '21', |
||
| 595 | 'name' => null, |
||
| 596 | 'lft' => '20', |
||
| 597 | 'rgt' => '25', |
||
| 598 | 'parent_id' => '18', |
||
| 599 | 'level' => '5', |
||
| 600 | ), |
||
| 601 | array( |
||
| 602 | 'tree_traversal_id' => '24', |
||
| 603 | 'name' => null, |
||
| 604 | 'lft' => '21', |
||
| 605 | 'rgt' => '22', |
||
| 606 | 'parent_id' => '21', |
||
| 607 | 'level' => '6', |
||
| 608 | ), |
||
| 609 | array( |
||
| 610 | 'tree_traversal_id' => '25', |
||
| 611 | 'name' => null, |
||
| 612 | 'lft' => '23', |
||
| 613 | 'rgt' => '24', |
||
| 614 | 'parent_id' => '21', |
||
| 615 | 'level' => '6', |
||
| 616 | ), |
||
| 617 | array( |
||
| 618 | 'tree_traversal_id' => '22', |
||
| 619 | 'name' => null, |
||
| 620 | 'lft' => '26', |
||
| 621 | 'rgt' => '27', |
||
| 622 | 'parent_id' => '18', |
||
| 623 | 'level' => '5', |
||
| 624 | ), |
||
| 625 | ); |
||
| 626 | $this->assertEquals($expected, $return); |
||
| 627 | |||
| 628 | //test custom levels |
||
| 629 | $return = $this->treeAdapter |
||
| 630 | ->getDescendants(18, 0, 2); |
||
| 631 | $expected = array( |
||
| 632 | array( |
||
| 633 | 'tree_traversal_id' => '18', |
||
| 634 | 'name' => null, |
||
| 635 | 'lft' => '19', |
||
| 636 | 'rgt' => '28', |
||
| 637 | 'parent_id' => '12', |
||
| 638 | 'level' => '4', |
||
| 639 | ), |
||
| 640 | array( |
||
| 641 | 'tree_traversal_id' => '21', |
||
| 642 | 'name' => null, |
||
| 643 | 'lft' => '20', |
||
| 644 | 'rgt' => '25', |
||
| 645 | 'parent_id' => '18', |
||
| 646 | 'level' => '5', |
||
| 647 | ), |
||
| 648 | array( |
||
| 649 | 'tree_traversal_id' => '22', |
||
| 650 | 'name' => null, |
||
| 651 | 'lft' => '26', |
||
| 652 | 'rgt' => '27', |
||
| 653 | 'parent_id' => '18', |
||
| 654 | 'level' => '5', |
||
| 655 | ), |
||
| 656 | ); |
||
| 657 | $this->assertEquals($expected, $return); |
||
| 658 | |||
| 659 | //test exclude node |
||
| 660 | $return = $this->treeAdapter |
||
| 661 | ->getDescendants(12, 0, null, 21); |
||
| 662 | $expected = array( |
||
| 663 | array( |
||
| 664 | 'tree_traversal_id' => '12', |
||
| 665 | 'name' => null, |
||
| 666 | 'lft' => '18', |
||
| 667 | 'rgt' => '29', |
||
| 668 | 'parent_id' => '6', |
||
| 669 | 'level' => '3', |
||
| 670 | ), |
||
| 671 | array( |
||
| 672 | 'tree_traversal_id' => '18', |
||
| 673 | 'name' => null, |
||
| 674 | 'lft' => '19', |
||
| 675 | 'rgt' => '28', |
||
| 676 | 'parent_id' => '12', |
||
| 677 | 'level' => '4', |
||
| 678 | ), |
||
| 679 | array( |
||
| 680 | 'tree_traversal_id' => '22', |
||
| 681 | 'name' => null, |
||
| 682 | 'lft' => '26', |
||
| 683 | 'rgt' => '27', |
||
| 684 | 'parent_id' => '18', |
||
| 685 | 'level' => '5', |
||
| 686 | ), |
||
| 687 | ); |
||
| 688 | $this->assertEquals($expected, $return); |
||
| 689 | } |
||
| 690 | |||
| 691 | public function testGetChildrenReturnEmptyArrayIfNodeDoesNotExist() |
||
| 692 | { |
||
| 693 | $return = $this->treeAdapter |
||
| 694 | ->getChildren(123456789); |
||
| 695 | $this->assertEquals(array(), $return); |
||
| 696 | } |
||
| 697 | |||
| 698 | public function testGetChildrenReturnEmptyArrayIfNodeDoesNotHaveChildren() |
||
| 699 | { |
||
| 700 | $return = $this->treeAdapter |
||
| 701 | ->getChildren(8); |
||
| 702 | $this->assertEquals(array(), $return); |
||
| 703 | } |
||
| 704 | |||
| 705 | public function testGetChildren() |
||
| 706 | { |
||
| 707 | //test exclude node |
||
| 708 | $return = $this->treeAdapter |
||
| 709 | ->getChildren(18); |
||
| 710 | $expected = array( |
||
| 711 | array( |
||
| 712 | 'tree_traversal_id' => '21', |
||
| 713 | 'name' => null, |
||
| 714 | 'lft' => '20', |
||
| 715 | 'rgt' => '25', |
||
| 716 | 'parent_id' => '18', |
||
| 717 | 'level' => '5', |
||
| 718 | ), |
||
| 719 | array( |
||
| 720 | 'tree_traversal_id' => '22', |
||
| 721 | 'name' => null, |
||
| 722 | 'lft' => '26', |
||
| 723 | 'rgt' => '27', |
||
| 724 | 'parent_id' => '18', |
||
| 725 | 'level' => '5', |
||
| 726 | ), |
||
| 727 | ); |
||
| 728 | $this->assertEquals($expected, $return); |
||
| 729 | } |
||
| 730 | |||
| 731 | public function testUpdateNode() |
||
| 732 | { |
||
| 733 | //test |
||
| 734 | $data = array( |
||
| 735 | 'name' => 'ahoj', |
||
| 736 | ); |
||
| 737 | $this->treeAdapter |
||
| 738 | ->updateNode(3, $data); |
||
| 739 | |||
| 740 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testUpdateNode-1.xml'); |
||
| 741 | |||
| 742 | //test |
||
| 743 | $data = array( |
||
| 744 | 'name' => 'ahoj', |
||
| 745 | 'lft' => '123456', |
||
| 746 | 'rgt' => '123456', |
||
| 747 | 'tree_traversal_id' => '123456', |
||
| 748 | 'level' => '123456', |
||
| 749 | 'parent_id' => '123456', |
||
| 750 | ); |
||
| 751 | $this->treeAdapter |
||
| 752 | ->updateNode(3, $data); |
||
| 753 | |||
| 754 | $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/NestedSet/testUpdateNode-1.xml'); |
||
| 755 | } |
||
| 756 | |||
| 757 | public function testGetRootNodeRootDoesNotExist() |
||
| 764 | |||
| 765 | public function testGetRootNode() |
||
| 780 | } |
||
| 781 |