Skip to content

Commit b57065d

Browse files
committed
Adds test for propagating styles to SplitItem + SubMenu
1 parent eede6bf commit b57065d

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/Builder/CliMenuBuilder.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,9 +555,12 @@ public function build() : CliMenu
555555
* Pass styles from current menu to sub-menu
556556
* only if sub-menu style has not be customized
557557
*/
558-
private function propagateStyles(CliMenu $menu)
558+
private function propagateStyles(CliMenu $menu, array $items = [])
559559
{
560-
foreach ($menu->getItems() as $item) {
560+
$currentItems = !empty($items) ? $items : $menu->getItems();
561+
562+
foreach ($currentItems as $item) {
563+
// Apply current style to children, if they are not customized
561564
if ($item instanceof MenuMenuItem) {
562565
$subMenu = $item->getSubMenu();
563566

@@ -566,6 +569,11 @@ private function propagateStyles(CliMenu $menu)
566569

567570
$this->propagateStyles($subMenu);
568571
}
572+
573+
// Apply styles to SplitItem children using current $menu
574+
if ($item instanceof SplitItem) {
575+
$this->propagateStyles($menu, $item->getItems());
576+
}
569577
}
570578
}
571579
}

test/Builder/CliMenuBuilderTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
1212
use PhpSchool\CliMenu\MenuItem\RadioItem;
1313
use PhpSchool\CliMenu\MenuItem\SelectableItem;
14+
use PhpSchool\CliMenu\MenuItem\SplitItem;
1415
use PhpSchool\CliMenu\MenuItem\StaticItem;
1516
use PhpSchool\Terminal\Terminal;
1617
use PHPUnit\Framework\TestCase;
@@ -606,6 +607,37 @@ public function testSubMenuInheritsParentsStyle() : void
606607
self::assertEquals($menu->getStyle(), $subMenu2->getStyle());
607608
}
608609

610+
public function testSplitItemSubMenuInheritsParentsStyle() : void
611+
{
612+
$terminal = self::createMock(Terminal::class);
613+
$terminal
614+
->expects($this->any())
615+
->method('getWidth')
616+
->will($this->returnValue(200));
617+
618+
$menu = (new CliMenuBuilder($terminal))
619+
->setBackgroundColour('green')
620+
->addSplitItem(function (SplitItemBuilder $b) {
621+
$b
622+
->addItem('Item 1', function () {})
623+
->addSubMenu('Submenu 1', function (CliMenuBuilder $b) {
624+
$b->addItem('Item 2', function () {});
625+
})
626+
;
627+
})
628+
->build();
629+
630+
/** @var SplitItem $splitItem */
631+
$splitItem = $menu->getItems()[0];
632+
/** @var SelectableItem $selectableItem1 */
633+
$selectableItem1 = $splitItem->getItems()[0];
634+
/** @var CliMenu $subMenu */
635+
$subMenu = $splitItem->getItems()[1]->getSubMenu();
636+
637+
self::assertSame('green', $subMenu->getStyle()->getBg());
638+
self::assertEquals($menu->getStyle(), $subMenu->getStyle());
639+
}
640+
609641
public function testSubMenuIgnoresParentsStyleIfCustomAndPassesToChildren() : void
610642
{
611643
$terminal = self::createMock(Terminal::class);

0 commit comments

Comments
 (0)