Skip to content

Commit f53504a

Browse files
jtattevinfabpot
authored andcommitted
[Console] Fix TreeHelper::addChild when providing a string
1 parent 42171a1 commit f53504a

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/Symfony/Component/Console/Helper/TreeNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function getValue(): string
5858
public function addChild(self|string|callable $node): self
5959
{
6060
if (\is_string($node)) {
61-
$node = new self($node, $this);
61+
$node = new self($node);
6262
}
6363

6464
$this->children[] = $node;

src/Symfony/Component/Console/Tests/Helper/TreeHelperTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,26 @@ public function testRenderNodeWithMultipleChildren()
195195
TREE, self::normalizeLineBreaks(trim($output->fetch())));
196196
}
197197

198+
public function testRenderNodeWithMultipleChildrenWithStringConversion()
199+
{
200+
$rootNode = new TreeNode('Root');
201+
202+
$rootNode->addChild('Child 1');
203+
$rootNode->addChild('Child 2');
204+
$rootNode->addChild('Child 3');
205+
206+
$output = new BufferedOutput();
207+
$tree = TreeHelper::createTree($output, $rootNode);
208+
209+
$tree->render();
210+
$this->assertSame(<<<TREE
211+
Root
212+
├── Child 1
213+
├── Child 2
214+
└── Child 3
215+
TREE, self::normalizeLineBreaks(trim($output->fetch())));
216+
}
217+
198218
public function testRenderTreeWithDuplicateNodeNames()
199219
{
200220
$rootNode = new TreeNode('Root');

src/Symfony/Component/Console/Tests/Helper/TreeNodeTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ public function testAddingChildren()
3434
$this->assertSame($child, iterator_to_array($root->getChildren())[0]);
3535
}
3636

37+
public function testAddingChildrenAsString()
38+
{
39+
$root = new TreeNode('Root');
40+
41+
$root->addChild('Child 1');
42+
$root->addChild('Child 2');
43+
44+
$this->assertSame(2, iterator_count($root->getChildren()));
45+
46+
$children = iterator_to_array($root->getChildren());
47+
48+
$this->assertSame(0, iterator_count($children[0]->getChildren()));
49+
$this->assertSame(0, iterator_count($children[1]->getChildren()));
50+
51+
$this->assertSame('Child 1', $children[0]->getValue());
52+
$this->assertSame('Child 2', $children[1]->getValue());
53+
}
54+
3755
public function testAddingChildrenWithGenerators()
3856
{
3957
$root = new TreeNode('Root');

0 commit comments

Comments
 (0)