Skip to content

Commit 6a6929c

Browse files
committed
Improve unit tests for Navigation\Nodes\Node class
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 8f395ec commit 6a6929c

4 files changed

Lines changed: 91 additions & 136 deletions

File tree

libraries/classes/Navigation/Nodes/Node.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,20 +277,22 @@ public function hasChildren(bool $countEmptyContainers = true): bool
277277
*/
278278
public function hasSiblings(): bool
279279
{
280-
$retval = false;
280+
if ($this->parent === null) {
281+
return false;
282+
}
283+
281284
$paths = $this->getPaths();
282285
if (count($paths['aPath_clean']) > 3) {
283286
return true;
284287
}
285288

286289
foreach ($this->parent->children as $child) {
287290
if ($child !== $this && ($child->type == self::OBJECT || $child->hasChildren(false))) {
288-
$retval = true;
289-
break;
291+
return true;
290292
}
291293
}
292294

293-
return $retval;
295+
return false;
294296
}
295297

296298
/**

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4760,11 +4760,6 @@ parameters:
47604760
count: 1
47614761
path: libraries/classes/Navigation/NavigationTree.php
47624762

4763-
-
4764-
message: "#^Cannot access property \\$children on PhpMyAdmin\\\\Navigation\\\\Nodes\\\\Node\\|null\\.$#"
4765-
count: 1
4766-
path: libraries/classes/Navigation/Nodes/Node.php
4767-
47684763
-
47694764
message: "#^Parameter \\#2 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
47704765
count: 3

psalm-baseline.xml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7500,24 +7500,17 @@
75007500
<code>$db</code>
75017501
</MixedArgument>
75027502
<MixedAssignment>
7503-
<code>$child</code>
75047503
<code>$db</code>
75057504
<code>$db</code>
75067505
<code>$db</code>
75077506
<code>$db</code>
75087507
<code>$db</code>
75097508
</MixedAssignment>
7510-
<MixedMethodCall>
7511-
<code>hasChildren</code>
7512-
</MixedMethodCall>
75137509
<MixedOperand>
75147510
<code><![CDATA[$GLOBALS['cfg']['Server']['hide_db']]]></code>
75157511
<code>$db</code>
75167512
<code>$db</code>
75177513
</MixedOperand>
7518-
<MixedPropertyFetch>
7519-
<code><![CDATA[$child->type]]></code>
7520-
</MixedPropertyFetch>
75217514
<PossiblyInvalidArgument>
75227515
<code>$databases</code>
75237516
</PossiblyInvalidArgument>
@@ -7537,15 +7530,9 @@
75377530
<code>$prefixMap</code>
75387531
<code>$prefixMap</code>
75397532
</PossiblyNullArrayOffset>
7540-
<PossiblyNullIterator>
7541-
<code><![CDATA[$this->parent->children]]></code>
7542-
</PossiblyNullIterator>
75437533
<PossiblyNullOperand>
75447534
<code>$arr[0]</code>
75457535
</PossiblyNullOperand>
7546-
<PossiblyNullPropertyFetch>
7547-
<code><![CDATA[$this->parent->children]]></code>
7548-
</PossiblyNullPropertyFetch>
75497536
<RedundantCastGivenDocblockType>
75507537
<code>(string) $prefix</code>
75517538
</RedundantCastGivenDocblockType>

test/classes/Navigation/Nodes/NodeTest.php

Lines changed: 85 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)