Skip to content

Commit 587ed66

Browse files
author
Jannik
authored
Merge pull request #16 from Becklyn/next
Next
2 parents 260dc3c + be138e4 commit 587ed66

3 files changed

Lines changed: 82 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2.1.1
2+
=====
3+
4+
* (improvement) Added `MenuItem::removeAllChildren()`.
5+
6+
17
2.1.0
28
=====
39

src/Item/MenuItem.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ public function removeChild (self $child) : self
649649
if (false !== $index)
650650
{
651651
\array_splice($this->children, $index, 1, null);
652+
$child->setParent(null);
652653
}
653654

654655
return $this;
@@ -851,4 +852,18 @@ public function isAnyCurrent () : bool
851852
{
852853
return $this->current || $this->currentAncestor;
853854
}
855+
856+
857+
/**
858+
* Removes all children
859+
*/
860+
public function removeAllChildren () : void
861+
{
862+
foreach ($this->children as $child)
863+
{
864+
$child->setParent(null);
865+
}
866+
867+
$this->children = [];
868+
}
854869
}

tests/Item/MenuItemTest.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Tests\Becklyn\Menu\Item;
44

55
use Becklyn\Menu\Item\MenuItem;
6-
use Becklyn\Menu\Sorter\MenuItemSorter;
76
use Becklyn\Menu\Target\LazyRoute;
87
use PHPUnit\Framework\TestCase;
98

@@ -349,4 +348,65 @@ public function testAnyCurrent () : void
349348
self::assertTrue($both->isCurrentAncestor());
350349
self::assertTrue($both->isAnyCurrent());
351350
}
351+
352+
353+
/**
354+
*
355+
*/
356+
public function testRemoveChild () : void
357+
{
358+
$root = new MenuItem();
359+
$child = $root->createChild("1");
360+
$otherRoot = new MenuItem();
361+
$otherChild = $otherRoot->createChild("other");
362+
363+
self::assertCount(1, $root->getChildren());
364+
self::assertSame($child, $root->getChildren()[0]);
365+
self::assertSame($root, $child->getParent());
366+
self::assertCount(1, $otherRoot->getChildren());
367+
self::assertSame($otherChild, $otherRoot->getChildren()[0]);
368+
self::assertSame($otherRoot, $otherChild->getParent());
369+
370+
// should change nothing -> wrong parent
371+
$root->removeChild($otherChild);
372+
373+
self::assertCount(1, $root->getChildren());
374+
self::assertSame($child, $root->getChildren()[0]);
375+
self::assertSame($root, $child->getParent());
376+
self::assertCount(1, $otherRoot->getChildren());
377+
self::assertSame($otherChild, $otherRoot->getChildren()[0]);
378+
self::assertSame($otherRoot, $otherChild->getParent());
379+
380+
$root->removeChild($child);
381+
382+
self::assertCount(0, $root->getChildren());
383+
self::assertSame(null, $child->getParent());
384+
self::assertCount(1, $otherRoot->getChildren());
385+
self::assertSame($otherChild, $otherRoot->getChildren()[0]);
386+
self::assertSame($otherRoot, $otherChild->getParent());
387+
}
388+
389+
390+
/**
391+
*
392+
*/
393+
public function testRemoveAllChildren () : void
394+
{
395+
$root = new MenuItem();
396+
$child1 = $root->createChild("1");
397+
$child2 = $root->createChild("2");
398+
399+
self::assertContains($child1, $root->getChildren());
400+
self::assertContains($child2, $root->getChildren());
401+
self::assertSame($root, $child1->getParent());
402+
self::assertSame($root, $child2->getParent());
403+
404+
$root->removeAllChildren();
405+
406+
407+
408+
self::assertEmpty($root->getChildren());
409+
self::assertSame(null, $child1->getParent());
410+
self::assertSame(null, $child2->getParent());
411+
}
352412
}

0 commit comments

Comments
 (0)