From f53504aabb8f14c5ceab10745bb02684bb88a73b Mon Sep 17 00:00:00 2001 From: Julien Tattevin Date: Wed, 9 Jul 2025 13:49:55 +0200 Subject: [PATCH] [Console] Fix `TreeHelper::addChild` when providing a string --- .../Component/Console/Helper/TreeNode.php | 2 +- .../Console/Tests/Helper/TreeHelperTest.php | 20 +++++++++++++++++++ .../Console/Tests/Helper/TreeNodeTest.php | 18 +++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Helper/TreeNode.php b/src/Symfony/Component/Console/Helper/TreeNode.php index 7f2ed8a4af371..8c35266c12a2a 100644 --- a/src/Symfony/Component/Console/Helper/TreeNode.php +++ b/src/Symfony/Component/Console/Helper/TreeNode.php @@ -58,7 +58,7 @@ public function getValue(): string public function addChild(self|string|callable $node): self { if (\is_string($node)) { - $node = new self($node, $this); + $node = new self($node); } $this->children[] = $node; diff --git a/src/Symfony/Component/Console/Tests/Helper/TreeHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/TreeHelperTest.php index 15ec0f0b23707..5d1399b27aef7 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TreeHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TreeHelperTest.php @@ -195,6 +195,26 @@ public function testRenderNodeWithMultipleChildren() TREE, self::normalizeLineBreaks(trim($output->fetch()))); } + public function testRenderNodeWithMultipleChildrenWithStringConversion() + { + $rootNode = new TreeNode('Root'); + + $rootNode->addChild('Child 1'); + $rootNode->addChild('Child 2'); + $rootNode->addChild('Child 3'); + + $output = new BufferedOutput(); + $tree = TreeHelper::createTree($output, $rootNode); + + $tree->render(); + $this->assertSame(<<fetch()))); + } + public function testRenderTreeWithDuplicateNodeNames() { $rootNode = new TreeNode('Root'); diff --git a/src/Symfony/Component/Console/Tests/Helper/TreeNodeTest.php b/src/Symfony/Component/Console/Tests/Helper/TreeNodeTest.php index 981e7ea477bfb..0e80da3bd069c 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TreeNodeTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TreeNodeTest.php @@ -34,6 +34,24 @@ public function testAddingChildren() $this->assertSame($child, iterator_to_array($root->getChildren())[0]); } + public function testAddingChildrenAsString() + { + $root = new TreeNode('Root'); + + $root->addChild('Child 1'); + $root->addChild('Child 2'); + + $this->assertSame(2, iterator_count($root->getChildren())); + + $children = iterator_to_array($root->getChildren()); + + $this->assertSame(0, iterator_count($children[0]->getChildren())); + $this->assertSame(0, iterator_count($children[1]->getChildren())); + + $this->assertSame('Child 1', $children[0]->getValue()); + $this->assertSame('Child 2', $children[1]->getValue()); + } + public function testAddingChildrenWithGenerators() { $root = new TreeNode('Root'); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy