Skip to content

Commit d10461d

Browse files
committed
Throw exception when opening menu with no items. Resolves #130
1 parent b0abfd8 commit d10461d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/CliMenu.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,10 @@ public function open() : void
486486
if ($this->isOpen()) {
487487
return;
488488
}
489+
490+
if (count($this->items) === 0) {
491+
throw new \RuntimeException('Menu must have at least 1 item before it can be opened');
492+
}
489493

490494
$this->configureTerminal();
491495
$this->open = true;

test/CliMenuTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,19 @@ public function testThrowsExceptionIfTerminalIsNotValidTTY() : void
359359
->method('isInteractive')
360360
->willReturn(false);
361361

362-
$menu = new CliMenu('PHP School FTW', [], $terminal);
362+
$menu = new CliMenu('PHP School FTW', [new StaticItem('One')], $terminal);
363363

364364
$menu->open();
365365
}
366366

367+
public function testOpenThrowsExceptionIfNoItemsInMenu() : void
368+
{
369+
$this->expectException(\RuntimeException::class);
370+
$this->expectExceptionMessage('Menu must have at least 1 item before it can be opened');
371+
372+
(new CliMenu('PHP School FTW', [], $this->terminal))->open();
373+
}
374+
367375
public function testGetTerminal() : void
368376
{
369377
$menu = new CliMenu('PHP School FTW', []);

0 commit comments

Comments
 (0)