Skip to content

Commit 8e6695e

Browse files
committed
Custom control mapping tests
1 parent 324f6a3 commit 8e6695e

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

src/CliMenu.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private function selectFirstItem() : void
205205
public function addCustomControlMapping(string $input, callable $callable) : void
206206
{
207207
if (isset($this->defaultControlMappings[$input]) || isset($this->customControlMappings[$input])) {
208-
throw new \InvalidArgumentException('Cannot rebind this input.');
208+
throw new \InvalidArgumentException('Cannot rebind this input');
209209
}
210210

211211
$this->customControlMappings[$input] = $callable;
@@ -217,7 +217,7 @@ public function addCustomControlMapping(string $input, callable $callable) : voi
217217
public function removeCustomControlMapping(string $input) : void
218218
{
219219
if (!isset($this->customControlMappings[$input])) {
220-
throw new \InvalidArgumentException('This input is not registered.');
220+
throw new \InvalidArgumentException('This input is not registered');
221221
}
222222

223223
unset($this->customControlMappings[$input]);

test/CliMenuTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,71 @@ public function testAskPasswordStyle() : void
377377
static::assertEquals('red', $password->getStyle()->getFg());
378378
}
379379

380+
public function testAddCustomControlMappingThrowsExceptionWhenOverwritingExistingDefaultControls() : void
381+
{
382+
$this->expectException(\InvalidArgumentException::class);
383+
$this->expectExceptionMessage('Cannot rebind this input');
384+
385+
$menu = new CliMenu('PHP School FTW', []);
386+
$menu->addCustomControlMapping(' ', function () {
387+
});
388+
}
389+
390+
public function testAddCustomControlMappingThrowsExceptionWhenAttemptingToOverwriteAddedCustomControlMap() : void
391+
{
392+
$this->expectException(\InvalidArgumentException::class);
393+
$this->expectExceptionMessage('Cannot rebind this input');
394+
395+
$menu = new CliMenu('PHP School FTW', []);
396+
$menu->addCustomControlMapping('c', function () {
397+
});
398+
$menu->addCustomControlMapping('c', function () {
399+
});
400+
}
401+
402+
public function testAddCustomControlMapping() : void
403+
{
404+
$this->terminal->expects($this->once())
405+
->method('read')
406+
->willReturn('c');
407+
408+
$style = $this->getStyle($this->terminal);
409+
410+
$action = function (CliMenu $menu) {
411+
$menu->close();
412+
};
413+
$item = new SelectableItem('Item 1', $action);
414+
415+
$menu = new CliMenu('PHP School FTW', [$item], $this->terminal, $style);
416+
$menu->addCustomControlMapping('c', $action);
417+
$menu->open();
418+
419+
static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch());
420+
}
421+
422+
public function testRemoveCustomControlMappingThrowsExceptionIfNoSuchMappingExists() : void
423+
{
424+
$this->expectException(\InvalidArgumentException::class);
425+
$this->expectExceptionMessage('This input is not registered');
426+
427+
$menu = new CliMenu('PHP School FTW', []);
428+
$menu->removeCustomControlMapping('c');
429+
}
430+
431+
public function testRemoveCustomControlMapping() : void
432+
{
433+
$action = function (CliMenu $menu) {
434+
$menu->close();
435+
};
436+
437+
$menu = new CliMenu('PHP School FTW', [], $this->terminal);
438+
$menu->addCustomControlMapping('c', $action);
439+
self::assertSame(['c' => $action], $this->readAttribute($menu, 'customControlMappings'));
440+
441+
$menu->removeCustomControlMapping('c');
442+
self::assertSame([], $this->readAttribute($menu, 'customControlMappings'));
443+
}
444+
380445
private function getTestFile() : string
381446
{
382447
return sprintf('%s/res/%s.txt', __DIR__, $this->getName());
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
 
4+
 PHP School FTW 
5+
 ========================================== 
6+
 ● Item 1 
7+
 
8+
9+

0 commit comments

Comments
 (0)