Skip to content

Commit

Permalink
Fix infinite loop when no selectable items. Resolves #144
Browse files Browse the repository at this point in the history
  • Loading branch information
AydinHassan committed May 16, 2018
1 parent 52cde0a commit 1ca95cb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/CliMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,17 @@ protected function moveSelectionVertically(string $direction) : void
{
$itemKeys = array_keys($this->items);

$increments = 0;

do {
$increments++;

if ($increments > count($itemKeys)) {
//full cycle detected, there must be no selected items
//in the menu, so stop trying to select one.
return;
}

$direction === 'UP'
? $this->selectedItem--
: $this->selectedItem++;
Expand Down
14 changes: 14 additions & 0 deletions test/CliMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,20 @@ public function testGetSelectedItemThrowsExceptionIfNoSelectedItem() : void
$menu->getSelectedItem();
}

public function testMenuCanOpenAndFunctionWithoutAnySelectableItems() : void
{
$this->terminal->expects($this->exactly(3))
->method('read')
->willReturn("\033[B", "\033[B", 'Q');
$menu = new CliMenu('PHP School FTW', [new StaticItem('One')], $this->terminal);
$menu->addCustomControlMapping('Q', function (CliMenu $menu) {
$menu->close();
});
$menu->open();

self::assertCount(1, $menu->getItems());
}

private function getTestFile() : string
{
return sprintf('%s/res/%s.txt', __DIR__, $this->getName());
Expand Down

0 comments on commit 1ca95cb

Please sign in to comment.