@@ -107,73 +107,6 @@ public function testRemoveChildNode(): void
107107 $ this ->assertSame ([0 => $ childOne , 2 => $ childThree , 3 => $ childTwo ], $ parent ->children );
108108 }
109109
110- /**
111- * SetUp for hasChildren
112- */
113- public function testNodeHasChildren (): void
114- {
115- $ parent = new Node ('default ' );
116- $ emptyContainer = new Node ('empty ' , Node::CONTAINER );
117- $ child = new Node ('default ' );
118- // test with no children
119- $ this ->assertEquals (
120- $ parent ->hasChildren (true ),
121- false ,
122- );
123- $ this ->assertEquals (
124- $ parent ->hasChildren (false ),
125- false ,
126- );
127- // test with an empty container
128- $ parent ->addChild ($ emptyContainer );
129- $ this ->assertEquals (
130- $ parent ->hasChildren (true ),
131- true ,
132- );
133- $ this ->assertEquals (
134- $ parent ->hasChildren (false ),
135- false ,
136- );
137- // test with a real child
138- $ parent ->addChild ($ child );
139- $ this ->assertEquals (
140- $ parent ->hasChildren (true ),
141- true ,
142- );
143- $ this ->assertEquals (
144- $ parent ->hasChildren (false ),
145- true ,
146- );
147- }
148-
149- /**
150- * SetUp for numChildren
151- */
152- public function testNumChildren (): void
153- {
154- // start with root node only
155- $ parent = new Node ('default ' );
156- $ this ->assertEquals ($ parent ->numChildren (), 0 );
157- // add a child
158- $ child = new Node ('default ' );
159- $ parent ->addChild ($ child );
160- $ this ->assertEquals ($ parent ->numChildren (), 1 );
161- // add a direct grandchild, this one doesn't count as
162- // it's not enclosed in a CONTAINER
163- $ child ->addChild (new Node ('default ' ));
164- $ this ->assertEquals ($ parent ->numChildren (), 1 );
165- // add a container, this one doesn't count wither
166- $ container = new Node ('default ' , Node::CONTAINER );
167- $ parent ->addChild ($ container );
168- $ this ->assertEquals ($ parent ->numChildren (), 1 );
169- // add a grandchild to container, this one counts
170- $ container ->addChild (new Node ('default ' ));
171- $ this ->assertEquals ($ parent ->numChildren (), 2 );
172- // add another grandchild to container, this one counts
173- $ container ->addChild (new Node ('default ' ));
174- $ this ->assertEquals ($ parent ->numChildren (), 3 );
175- }
176-
177110 public function testParents (): void
178111 {
179112 $ dbContainer = new Node ('root ' , Node::CONTAINER );
@@ -215,64 +148,102 @@ public function testRealParent(): void
215148 $ this ->assertSame ($ child , $ grandchild ->realParent ());
216149 }
217150
218- /**
219- * Tests whether Node->hasSiblings() method returns false
220- * when the node does not have any siblings.
221- */
222- public function testHasSiblingsWithNoSiblings (): void
151+ public function testNodeHasChildren (): void
223152 {
224- $ parent = new Node ('default ' );
225- $ child = new Node ('default ' );
153+ $ parent = new Node ('parent ' );
154+ $ child = new Node ('child ' );
155+ $ this ->assertFalse ($ parent ->hasChildren (true ));
156+ $ this ->assertFalse ($ parent ->hasChildren (false ));
226157 $ parent ->addChild ($ child );
227- $ this ->assertFalse ($ child ->hasSiblings ());
158+ $ this ->assertTrue ($ parent ->hasChildren (true ));
159+ $ this ->assertTrue ($ parent ->hasChildren (false ));
228160 }
229161
230- /**
231- * Tests whether Node->hasSiblings() method returns true
232- * when it actually has siblings.
233- */
234- public function testHasSiblingsWithSiblings (): void
162+ public function testNodeHasChildrenWithContainers (): void
235163 {
236- $ parent = new Node ('default ' );
237- $ firstChild = new Node ('default ' );
238- $ parent ->addChild ($ firstChild );
239- $ secondChild = new Node ('default ' );
240- $ parent ->addChild ($ secondChild );
241- // Normal case; two Node:NODE type siblings
242- $ this ->assertTrue ($ firstChild ->hasSiblings ());
243-
244- $ parent = new Node ('default ' );
245- $ firstChild = new Node ('default ' );
246- $ parent ->addChild ($ firstChild );
247- $ secondChild = new Node ('default ' , Node::CONTAINER );
248- $ parent ->addChild ($ secondChild );
249- // Empty Node::CONTAINER type node should not be considered in hasSiblings()
250- $ this ->assertFalse ($ firstChild ->hasSiblings ());
251-
252- $ grandChild = new Node ('default ' );
253- $ secondChild ->addChild ($ grandChild );
254- // Node::CONTAINER type nodes with children are counted for hasSiblings()
255- $ this ->assertTrue ($ firstChild ->hasSiblings ());
164+ $ parent = new Node ('parent ' );
165+ $ containerOne = new Node ('container 1 ' , Node::CONTAINER );
166+ $ containerTwo = new Node ('container 2 ' , Node::CONTAINER );
167+ $ child = new Node ('child ' );
168+ $ this ->assertFalse ($ parent ->hasChildren ());
169+ $ this ->assertFalse ($ parent ->hasChildren (false ));
170+ $ parent ->addChild ($ containerOne );
171+ $ this ->assertTrue ($ parent ->hasChildren ());
172+ $ this ->assertFalse ($ parent ->hasChildren (false ));
173+ $ containerOne ->addChild ($ containerTwo );
174+ $ this ->assertTrue ($ parent ->hasChildren ());
175+ $ this ->assertFalse ($ parent ->hasChildren (false ));
176+ $ containerTwo ->addChild ($ child );
177+ $ this ->assertTrue ($ parent ->hasChildren ());
178+ $ this ->assertTrue ($ parent ->hasChildren (false ));
256179 }
257180
258- /**
259- * It is expected that Node->hasSiblings() method always return true
260- * for Nodes that are 3 levels deep (columns and indexes).
261- */
262- public function testHasSiblingsForNodesAtLevelThree (): void
181+ public function testNodeHasSiblings (): void
263182 {
264- $ parent = new Node ('default ' );
265- $ child = new Node ('default ' );
266- $ parent ->addChild ($ child );
267- $ grandChild = new Node ('default ' );
268- $ child ->addChild ($ grandChild );
269- $ greatGrandChild = new Node ('default ' );
270- $ grandChild ->addChild ($ greatGrandChild );
183+ $ parent = new Node ('parent ' );
184+ $ childOne = new Node ('child one ' );
185+ $ childTwo = new Node ('child two ' );
186+ $ parent ->addChild ($ childOne );
187+ $ this ->assertFalse ($ parent ->hasSiblings ());
188+ $ this ->assertFalse ($ childOne ->hasSiblings ());
189+ $ parent ->addChild ($ childTwo );
190+ $ this ->assertTrue ($ childOne ->hasSiblings ());
191+ }
192+
193+ public function testNodeHasSiblingsWithContainers (): void
194+ {
195+ $ parent = new Node ('parent ' );
196+ $ childOne = new Node ('child one ' );
197+ $ containerOne = new Node ('container 1 ' , Node::CONTAINER );
198+ $ containerTwo = new Node ('container 2 ' , Node::CONTAINER );
199+ $ childTwo = new Node ('child two ' );
200+ $ parent ->addChild ($ childOne );
201+ $ parent ->addChild ($ containerOne );
202+ $ this ->assertFalse ($ childOne ->hasSiblings (), 'An empty container node should not be considered a sibling. ' );
203+ $ containerOne ->addChild ($ containerTwo );
204+ $ this ->assertFalse (
205+ $ childOne ->hasSiblings (),
206+ 'A container node with empty children should not be considered a sibling. ' ,
207+ );
208+ $ containerOne ->addChild ($ childTwo );
209+ $ this ->assertTrue ($ childOne ->hasSiblings (), 'A container node with children should be considered a sibling. ' );
210+ }
271211
212+ public function testNodeHasSiblingsForNodesAtLevelThree (): void
213+ {
214+ $ parent = new Node ('parent ' );
215+ $ child = new Node ('child ' );
216+ $ grandchild = new Node ('grandchild ' );
217+ $ greatGrandchild = new Node ('great grandchild ' );
218+ $ parent ->addChild ($ child );
219+ $ child ->addChild ($ grandchild );
220+ $ grandchild ->addChild ($ greatGrandchild );
272221 // Should return false for node that are two levels deeps
273- $ this ->assertFalse ($ grandChild ->hasSiblings ());
222+ $ this ->assertFalse ($ grandchild ->hasSiblings ());
274223 // Should return true for node that are three levels deeps
275- $ this ->assertTrue ($ greatGrandChild ->hasSiblings ());
224+ $ this ->assertTrue ($ greatGrandchild ->hasSiblings ());
225+ }
226+
227+ public function testNumChildren (): void
228+ {
229+ $ parent = new Node ('parent ' );
230+ $ this ->assertSame (0 , $ parent ->numChildren ());
231+ $ child = new Node ('child one ' );
232+ $ parent ->addChild ($ child );
233+ $ this ->assertSame (1 , $ parent ->numChildren ());
234+ // add a direct grandchild, this one doesn't count as it's not enclosed in a CONTAINER
235+ $ child ->addChild (new Node ('child two ' ));
236+ $ this ->assertSame (1 , $ parent ->numChildren ());
237+ // add a container, this one doesn't count wither
238+ $ container = new Node ('container ' , Node::CONTAINER );
239+ $ parent ->addChild ($ container );
240+ $ this ->assertSame (1 , $ parent ->numChildren ());
241+ // add a grandchild to container, this one counts
242+ $ container ->addChild (new Node ('child three ' ));
243+ $ this ->assertSame (2 , $ parent ->numChildren ());
244+ // add another grandchild to container, this one counts
245+ $ container ->addChild (new Node ('child four ' ));
246+ $ this ->assertSame (3 , $ parent ->numChildren ());
276247 }
277248
278249 /**
0 commit comments