@@ -160,9 +160,9 @@ public function testFindParent() {
160160 }
161161
162162 /**
163- * Tests adding a node.
163+ * Tests inserting a node below a parent with existing children .
164164 */
165- public function testAddNodeWithExistingChildren () {
165+ public function testInsertNodeBelowWithExistingChildren () {
166166
167167 $ parent = $ this ->nestedSet ->getNode (3 , 1 );
168168 $ child = new Node (12 , 1 );
@@ -182,14 +182,13 @@ public function testAddNodeWithExistingChildren() {
182182 }
183183
184184 /**
185- * Tests adding a node.
185+ * Tests inserting a node below a parent with no children .
186186 */
187- public function testAddNodeWithNoChildren () {
187+ public function testInsertNodeBelowWithNoChildren () {
188+ $ target = $ this ->nestedSet ->getNode (6 , 1 );
189+ $ node = new Node (13 , 1 );
188190
189- $ parent = $ this ->nestedSet ->getNode (6 , 1 );
190- $ child = new Node (13 , 1 );
191-
192- $ newNode = $ this ->nestedSet ->insertNodeBelow ($ parent , $ child );
191+ $ newNode = $ this ->nestedSet ->insertNodeBelow ($ target , $ node );
193192
194193 // Should be inserted below 6 with depth 4.
195194 $ this ->assertEquals (7 , $ newNode ->getLeft ());
@@ -202,6 +201,46 @@ public function testAddNodeWithNoChildren() {
202201 $ this ->assertEquals (9 , $ newParent ->getRight ());
203202 }
204203
204+ /**
205+ * Tests inserting a node before another sibling.
206+ */
207+ public function testInsertNodeBefore () {
208+ $ parent = $ this ->nestedSet ->getNode (6 , 1 );
209+ $ child = new Node (14 , 1 );
210+
211+ $ newNode = $ this ->nestedSet ->insertNodeBefore ($ parent , $ child );
212+
213+ // Should be inserted below 6 with depth 4.
214+ $ this ->assertEquals (6 , $ newNode ->getLeft ());
215+ $ this ->assertEquals (7 , $ newNode ->getRight ());
216+ $ this ->assertEquals (3 , $ newNode ->getDepth ());
217+
218+ // Parent node right should have incremented.
219+ $ newParent = $ this ->nestedSet ->getNode (4 , 1 );
220+ $ this ->assertEquals (3 , $ newParent ->getLeft ());
221+ $ this ->assertEquals (10 , $ newParent ->getRight ());
222+ }
223+
224+ /**
225+ * Tests inserting a node after another sibling.
226+ */
227+ public function testInsertNodeAfter () {
228+ $ parent = $ this ->nestedSet ->getNode (5 , 1 );
229+ $ child = new Node (15 , 1 );
230+
231+ $ newNode = $ this ->nestedSet ->insertNodeAfter ($ parent , $ child );
232+
233+ // Should be inserted below 6 with depth 4.
234+ $ this ->assertEquals (6 , $ newNode ->getLeft ());
235+ $ this ->assertEquals (7 , $ newNode ->getRight ());
236+ $ this ->assertEquals (3 , $ newNode ->getDepth ());
237+
238+ // Parent node right should have incremented.
239+ $ newParent = $ this ->nestedSet ->getNode (4 , 1 );
240+ $ this ->assertEquals (3 , $ newParent ->getLeft ());
241+ $ this ->assertEquals (10 , $ newParent ->getRight ());
242+ }
243+
205244 /**
206245 * Tests deleting a node.
207246 */
@@ -226,7 +265,6 @@ public function testDeleteNode() {
226265 $ this ->assertEquals (2 , $ node ->getDepth ());
227266
228267 $ tree = $ this ->nestedSet ->getTree ();
229- $ this ->printTree ($ tree );
230268 }
231269
232270 /**
@@ -254,19 +292,12 @@ public function testDeleteSubTree() {
254292 * Tests moving a sub-tree under a parent node.
255293 */
256294 public function testMoveSubTreeBelow () {
257- print "BEFORE: " . PHP_EOL ;
258- $ tree = $ this ->nestedSet ->getTree ();
259- $ this ->printTree ($ tree );
260295
261296 $ parent = $ this ->nestedSet ->getNode (1 , 1 );
262297 $ node = $ this ->nestedSet ->getNode (7 , 1 );
263298
264299 $ this ->nestedSet ->moveSubTreeBelow ($ parent , $ node );
265300
266- print "AFTER: " . PHP_EOL ;
267- $ tree = $ this ->nestedSet ->getTree ();
268- $ this ->printTree ($ tree );
269-
270301 // Check node is in new position.
271302 $ node = $ this ->nestedSet ->getNode (7 , 1 );
272303 $ this ->assertEquals (2 , $ node ->getLeft ());
@@ -296,19 +327,12 @@ public function testMoveSubTreeBelow() {
296327 * Tests moving a sub-tree before a target node.
297328 */
298329 public function testMoveSubTreeBefore () {
299- print "BEFORE: " . PHP_EOL ;
300- $ tree = $ this ->nestedSet ->getTree ();
301- $ this ->printTree ($ tree );
302330
303331 $ target = $ this ->nestedSet ->getNode (4 , 1 );
304332 $ node = $ this ->nestedSet ->getNode (7 , 1 );
305333
306334 $ this ->nestedSet ->moveSubTreeBefore ($ target , $ node );
307335
308- print "AFTER: " . PHP_EOL ;
309- $ tree = $ this ->nestedSet ->getTree ();
310- $ this ->printTree ($ tree );
311-
312336 // Check node is in new position.
313337 $ node = $ this ->nestedSet ->getNode (7 , 1 );
314338 $ this ->assertEquals (3 , $ node ->getLeft ());
0 commit comments