Skip to content

Commit 10ec8d8

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

File tree

2 files changed

+118
-62
lines changed

2 files changed

+118
-62
lines changed

libraries/classes/Navigation/Nodes/Node.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ public function parents(bool $self = false, bool $containers = false, bool $grou
215215
}
216216

217217
$parent = $this->parent;
218+
if ($parent === null) {
219+
/** @infection-ignore-all */
220+
return $parents;
221+
}
222+
218223
while ($parent !== null) {
219224
if (($parent->type != self::CONTAINER || $containers) && (! $parent->isGroup || $groups)) {
220225
$parents[] = $parent;

test/classes/Navigation/Nodes/NodeTest.php

Lines changed: 113 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,98 @@
1313
use ReflectionMethod;
1414

1515
#[CoversClass(Node::class)]
16-
class NodeTest extends AbstractTestCase
16+
final class NodeTest extends AbstractTestCase
1717
{
18-
/**
19-
* SetUp for AddNode
20-
*/
21-
public function testAddNode(): void
18+
public function testNewNode(): void
2219
{
23-
$parent = new Node('parent');
24-
$child = new Node('child');
25-
$parent->addChild($child);
26-
$this->assertEquals(
27-
$parent->getChild($child->name),
28-
$child,
29-
);
30-
$this->assertEquals(
31-
$parent->getChild($child->realName, true),
32-
$child,
33-
);
20+
$node = new Node('Object Node');
21+
$this->assertSame('Object Node', $node->name);
22+
$this->assertSame('Object Node', $node->realName);
23+
$this->assertSame(Node::OBJECT, $node->type);
24+
$this->assertFalse($node->isGroup);
3425
}
3526

36-
/**
37-
* SetUp for getChild
38-
*/
39-
public function testGetChildError(): void
27+
public function testNewNodeWithEmptyName(): void
28+
{
29+
$node = new Node('');
30+
$this->assertSame('', $node->name);
31+
$this->assertSame('', $node->realName);
32+
$this->assertSame(Node::OBJECT, $node->type);
33+
$this->assertFalse($node->isGroup);
34+
}
35+
36+
public function testNewContainerNode(): void
37+
{
38+
$node = new Node('Container Node', Node::CONTAINER);
39+
$this->assertSame('Container Node', $node->name);
40+
$this->assertSame('Container Node', $node->realName);
41+
$this->assertSame(Node::CONTAINER, $node->type);
42+
$this->assertFalse($node->isGroup);
43+
}
44+
45+
public function testNewGroupNode(): void
46+
{
47+
$node = new Node('Group Node', Node::OBJECT, true);
48+
$this->assertSame('Group Node', $node->name);
49+
$this->assertSame('Group Node', $node->realName);
50+
$this->assertSame(Node::OBJECT, $node->type);
51+
$this->assertTrue($node->isGroup);
52+
}
53+
54+
/** @psalm-suppress DocblockTypeContradiction */
55+
public function testAddChildNode(): void
4056
{
4157
$parent = new Node('parent');
42-
$this->assertNull(
43-
$parent->getChild('foo'),
44-
);
45-
$this->assertNull(
46-
$parent->getChild('foo', true),
47-
);
58+
$childOne = new Node('child one');
59+
$childTwo = new Node('child two');
60+
$this->assertSame([], $parent->children);
61+
$this->assertNull($childOne->parent);
62+
$this->assertNull($childTwo->parent);
63+
$parent->addChild($childOne);
64+
$this->assertSame([$childOne], $parent->children);
65+
$this->assertSame($parent, $childOne->parent);
66+
$this->assertNull($childTwo->parent);
67+
$parent->addChild($childTwo);
68+
$this->assertSame([$childOne, $childTwo], $parent->children);
69+
$this->assertSame($parent, $childOne->parent);
70+
$this->assertSame($parent, $childTwo->parent);
4871
}
4972

50-
/**
51-
* SetUp for getChild
52-
*/
53-
public function testRemoveNode(): void
73+
public function testGetChildNode(): void
5474
{
5575
$parent = new Node('parent');
56-
$child = new Node('child');
76+
$child = new Node('child real name');
77+
$child->name = 'child';
78+
$this->assertNull($parent->getChild('child'));
79+
$this->assertNull($parent->getChild('child', true));
80+
$this->assertNull($parent->getChild('child real name'));
81+
$this->assertNull($parent->getChild('child real name', true));
5782
$parent->addChild($child);
58-
$this->assertEquals(
59-
$parent->getChild($child->name),
60-
$child,
61-
);
62-
$parent->removeChild($child->name);
63-
$this->assertNull(
64-
$parent->getChild($child->name),
65-
);
83+
$this->assertSame($child, $parent->getChild('child'));
84+
$this->assertNull($parent->getChild('child', true));
85+
$this->assertNull($parent->getChild('child real name'));
86+
$this->assertSame($child, $parent->getChild('child real name', true));
87+
$child->isNew = true;
88+
$this->assertNull($parent->getChild('child'));
89+
$this->assertNull($parent->getChild('child', true));
90+
$this->assertNull($parent->getChild('child real name'));
91+
$this->assertSame($child, $parent->getChild('child real name', true));
92+
}
93+
94+
public function testRemoveChildNode(): void
95+
{
96+
$parent = new Node('parent');
97+
$childOne = new Node('child one');
98+
$childTwo = new Node('child two');
99+
$childThree = new Node('child three');
100+
$parent->addChild($childOne);
101+
$parent->addChild($childTwo);
102+
$parent->addChild($childThree);
103+
$parent->addChild($childTwo);
104+
$this->assertSame([0 => $childOne, 1 => $childTwo, 2 => $childThree, 3 => $childTwo], $parent->children);
105+
$parent->removeChild('child two');
106+
/** @psalm-suppress DocblockTypeContradiction */
107+
$this->assertSame([0 => $childOne, 2 => $childThree, 3 => $childTwo], $parent->children);
66108
}
67109

68110
/**
@@ -132,36 +174,45 @@ public function testNumChildren(): void
132174
$this->assertEquals($parent->numChildren(), 3);
133175
}
134176

135-
/**
136-
* SetUp for parents
137-
*/
138177
public function testParents(): void
139178
{
140-
$parent = new Node('default');
141-
$this->assertEquals($parent->parents(), []); // exclude self
142-
$this->assertEquals($parent->parents(true), [$parent]); // include self
143-
144-
$child = new Node('default');
145-
$parent->addChild($child);
146-
147-
$this->assertEquals($child->parents(), [$parent]); // exclude self
148-
$this->assertEquals(
149-
$child->parents(true),
150-
[$child, $parent],
151-
); // include self
179+
$dbContainer = new Node('root', Node::CONTAINER);
180+
$dbGroup = new Node('db_group', Node::CONTAINER, true);
181+
$dbOne = new Node('db_group__one');
182+
$dbTwo = new Node('db_group__two');
183+
$tableContainer = new Node('tables', Node::CONTAINER);
184+
$table = new Node('table');
185+
$dbContainer->addChild($dbGroup);
186+
$dbGroup->addChild($dbOne);
187+
$dbGroup->addChild($dbTwo);
188+
$dbOne->addChild($tableContainer);
189+
$tableContainer->addChild($table);
190+
$this->assertSame([], $dbContainer->parents(false, true));
191+
$this->assertSame([$dbContainer], $dbContainer->parents(true, true));
192+
$this->assertSame([$dbContainer], $dbGroup->parents(false, true, true));
193+
$this->assertSame([$dbGroup, $dbContainer], $dbGroup->parents(true, true, true));
194+
$this->assertSame([$dbOne], $table->parents());
195+
$this->assertSame([$table, $dbOne], $table->parents(true));
196+
$this->assertSame([$tableContainer, $dbOne, $dbContainer], $table->parents(false, true));
197+
$this->assertSame([$table, $tableContainer, $dbOne, $dbContainer], $table->parents(true, true));
198+
$this->assertSame([$dbOne], $table->parents(false, false, true));
199+
$this->assertSame([$tableContainer, $dbOne, $dbGroup, $dbContainer], $table->parents(false, true, true));
200+
$this->assertSame(
201+
[$table, $tableContainer, $dbOne, $dbGroup, $dbContainer],
202+
$table->parents(true, true, true),
203+
);
152204
}
153205

154-
/**
155-
* SetUp for realParent
156-
*/
157206
public function testRealParent(): void
158207
{
159-
$parent = new Node('default');
160-
$this->assertFalse($parent->realParent());
161-
162-
$child = new Node('default');
208+
$parent = new Node('parent');
209+
$child = new Node('child');
210+
$grandchild = new Node('grandchild');
163211
$parent->addChild($child);
164-
$this->assertEquals($child->realParent(), $parent);
212+
$child->addChild($grandchild);
213+
$this->assertFalse($parent->realParent());
214+
$this->assertSame($parent, $child->realParent());
215+
$this->assertSame($child, $grandchild->realParent());
165216
}
166217

167218
/**

0 commit comments

Comments
 (0)