Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1c43dcd

Browse files
committed
bug #61080 [Console] Fix TreeHelper::addChild when providing a string (jtattevin)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Console] Fix `TreeHelper::addChild` when providing a string | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | -- | License | MIT When using the tree helper, the method addChild allow to pass a string instead of an instance of TreeNode, for example : ```php $rootNode = new TreeNode('Root'); $rootNode->addChild('Child 1'); $rootNode->addChild('Child 2'); $rootNode->addChild('Child 3'); $tree = TreeHelper::createTree($output, $rootNode); $tree->render(); ``` This method was creating the TreeNode from the string and using the parent as the children iterator, leading to an error like `LogicException: Cycle detected at node: "Child 1"` This commit remove this second parameter. Commits ------- f53504aabb8 [Console] Fix `TreeHelper::addChild` when providing a string
2 parents 3d42007 + b4ee0d4 commit 1c43dcd

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

‎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;

‎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');

‎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)

AltStyle によって変換されたページ (->オリジナル) /