diff --git a/src/CliMenu.php b/src/CliMenu.php index e08cfa1c..e6d434d6 100644 --- a/src/CliMenu.php +++ b/src/CliMenu.php @@ -486,6 +486,10 @@ public function open() : void if ($this->isOpen()) { return; } + + if (count($this->items) === 0) { + throw new \RuntimeException('Menu must have at least 1 item before it can be opened'); + } $this->configureTerminal(); $this->open = true; diff --git a/test/CliMenuTest.php b/test/CliMenuTest.php index 48a14568..1f6df128 100644 --- a/test/CliMenuTest.php +++ b/test/CliMenuTest.php @@ -359,11 +359,19 @@ public function testThrowsExceptionIfTerminalIsNotValidTTY() : void ->method('isInteractive') ->willReturn(false); - $menu = new CliMenu('PHP School FTW', [], $terminal); + $menu = new CliMenu('PHP School FTW', [new StaticItem('One')], $terminal); $menu->open(); } + public function testOpenThrowsExceptionIfNoItemsInMenu() : void + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Menu must have at least 1 item before it can be opened'); + + (new CliMenu('PHP School FTW', [], $this->terminal))->open(); + } + public function testGetTerminal() : void { $menu = new CliMenu('PHP School FTW', []);