@@ -447,6 +447,117 @@ public function testMoveSubTreeBefore() {
447447 $ this ->assertNodeMovedBefore ();
448448 }
449449
450+ /**
451+ * Tests swapping parent node.
452+ */
453+ public function testAdoptChildren () {
454+
455+ $ oldParent = $ this ->nestedSet ->getNode (new NodeKey (7 , 1 ));
456+ $ newParent = $ this ->nestedSet ->getNode (new NodeKey (4 , 1 ));
457+
458+ $ this ->nestedSet ->adoptChildren ($ oldParent , $ newParent );
459+
460+ // Check new parent has all children.
461+ $ node = $ this ->nestedSet ->getNode (new NodeKey (4 , 1 ));
462+ $ this ->assertEquals (3 , $ node ->getLeft ());
463+ $ this ->assertEquals (12 , $ node ->getRight ());
464+ $ this ->assertEquals (2 , $ node ->getDepth ());
465+
466+ // Check old parent has been updated.
467+ $ node = $ this ->nestedSet ->getNode (new NodeKey (7 , 1 ));
468+ $ this ->assertEquals (15 , $ node ->getLeft ());
469+ $ this ->assertEquals (16 , $ node ->getRight ());
470+ $ this ->assertEquals (2 , $ node ->getDepth ());
471+
472+ // Check first child is in correct postion.
473+ $ node = $ this ->nestedSet ->getNode (new NodeKey (5 , 1 ));
474+ $ this ->assertEquals (4 , $ node ->getLeft ());
475+ $ this ->assertEquals (5 , $ node ->getRight ());
476+ $ this ->assertEquals (3 , $ node ->getDepth ());
477+
478+ // Check last child is in correct postion.
479+ $ node = $ this ->nestedSet ->getNode (new NodeKey (11 , 1 ));
480+ $ this ->assertEquals (10 , $ node ->getLeft ());
481+ $ this ->assertEquals (11 , $ node ->getRight ());
482+ $ this ->assertEquals (3 , $ node ->getDepth ());
483+ }
484+
485+ /**
486+ * Tests swapping parent node.
487+ */
488+ public function testAdoptChildrenWithDecendents () {
489+
490+ $ oldParent = $ this ->nestedSet ->getNode (new NodeKey (3 , 1 ));
491+ $ newParent = $ this ->nestedSet ->getNode (new NodeKey (4 , 1 ));
492+
493+ $ this ->nestedSet ->adoptChildren ($ oldParent , $ newParent );
494+
495+ // Check new parent has all children.
496+ $ node = $ this ->nestedSet ->getNode (new NodeKey (4 , 1 ));
497+ $ this ->assertEquals (3 , $ node ->getLeft ());
498+ $ this ->assertEquals (18 , $ node ->getRight ());
499+ $ this ->assertEquals (2 , $ node ->getDepth ());
500+
501+ // Check old parent has been updated.
502+ $ node = $ this ->nestedSet ->getNode (new NodeKey (3 , 1 ));
503+ $ this ->assertEquals (20 , $ node ->getLeft ());
504+ $ this ->assertEquals (21 , $ node ->getRight ());
505+ $ this ->assertEquals (1 , $ node ->getDepth ());
506+
507+ // Check first child is in correct postion.
508+ $ node = $ this ->nestedSet ->getNode (new NodeKey (5 , 1 ));
509+ $ this ->assertEquals (4 , $ node ->getLeft ());
510+ $ this ->assertEquals (5 , $ node ->getRight ());
511+ $ this ->assertEquals (3 , $ node ->getDepth ());
512+
513+ // Check last child is in correct postion.
514+ $ node = $ this ->nestedSet ->getNode (new NodeKey (9 , 1 ));
515+ $ this ->assertEquals (16 , $ node ->getLeft ());
516+ $ this ->assertEquals (17 , $ node ->getRight ());
517+ $ this ->assertEquals (3 , $ node ->getDepth ());
518+ }
519+
520+ /**
521+ * Tests swapping parent node.
522+ */
523+ public function testAdoptChildrenNoExisting () {
524+
525+ $ oldParent = $ this ->nestedSet ->getNode (new NodeKey (7 , 1 ));
526+ $ newParent = $ this ->nestedSet ->getNode (new NodeKey (8 , 1 ));
527+
528+ echo PHP_EOL . "Before: " ;
529+ $ this ->printTree ($ this ->nestedSet ->getTree ());
530+
531+ $ this ->nestedSet ->adoptChildren ($ oldParent , $ newParent );
532+
533+ echo PHP_EOL . "After: " ;
534+ $ this ->printTree ($ this ->nestedSet ->getTree ());
535+
536+ // Check new parent has all children.
537+ $ node = $ this ->nestedSet ->getNode (new NodeKey (8 , 1 ));
538+ $ this ->assertEquals (13 , $ node ->getLeft ());
539+ $ this ->assertEquals (18 , $ node ->getRight ());
540+ $ this ->assertEquals (2 , $ node ->getDepth ());
541+
542+ // Check old parent has been updated.
543+ $ node = $ this ->nestedSet ->getNode (new NodeKey (7 , 1 ));
544+ $ this ->assertEquals (11 , $ node ->getLeft ());
545+ $ this ->assertEquals (12 , $ node ->getRight ());
546+ $ this ->assertEquals (2 , $ node ->getDepth ());
547+
548+ // Check first child is in correct position.
549+ $ node = $ this ->nestedSet ->getNode (new NodeKey (10 , 1 ));
550+ $ this ->assertEquals (14 , $ node ->getLeft ());
551+ $ this ->assertEquals (15 , $ node ->getRight ());
552+ $ this ->assertEquals (3 , $ node ->getDepth ());
553+
554+ // Check last child is in correct position.
555+ $ node = $ this ->nestedSet ->getNode (new NodeKey (11 , 1 ));
556+ $ this ->assertEquals (16 , $ node ->getLeft ());
557+ $ this ->assertEquals (17 , $ node ->getRight ());
558+ $ this ->assertEquals (3 , $ node ->getDepth ());
559+ }
560+
450561 /**
451562 * Tests inserting a root node to an empty tree.
452563 */
0 commit comments