Skip to content

Commit 94a9c54

Browse files
authored
Merge pull request #114 from Lynesth/patch-19
function addCustomControlMappings()
2 parents 3dd6856 + df04578 commit 94a9c54

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

src/CliMenu.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,16 @@ public function addCustomControlMapping(string $input, callable $callable) : voi
212212
$this->customControlMappings[$input] = $callable;
213213
}
214214

215+
/**
216+
* Shorthand function to add multiple custom control mapping at once
217+
*/
218+
public function addCustomControlMappings(array $map) : void
219+
{
220+
foreach ($map as $input => $callable) {
221+
$this->addCustomControlMapping($input, $callable);
222+
}
223+
}
224+
215225
/**
216226
* Removes a custom control mapping
217227
*/

test/CliMenuTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,62 @@ public function testAddCustomControlMapping() : void
480480
static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch());
481481
}
482482

483+
public function testAddCustomControlMappingsThrowsExceptionWhenOverwritingExistingDefaultControls() : void
484+
{
485+
$this->expectException(\InvalidArgumentException::class);
486+
$this->expectExceptionMessage('Cannot rebind this input');
487+
488+
$menu = new CliMenu('PHP School FTW', []);
489+
$menu->addCustomControlMappings([
490+
' ' => function () {
491+
}
492+
]);
493+
}
494+
495+
public function testAddCustomControlMappingsThrowsExceptionWhenAttemptingToOverwriteAddedCustomControlMap() : void
496+
{
497+
$this->expectException(\InvalidArgumentException::class);
498+
$this->expectExceptionMessage('Cannot rebind this input');
499+
500+
$menu = new CliMenu('PHP School FTW', []);
501+
$menu->addCustomControlMappings([
502+
'c' => function () {
503+
}
504+
]);
505+
$menu->addCustomControlMappings([
506+
'c' => function () {
507+
}
508+
]);
509+
}
510+
511+
public function testAddCustomControlMappings() : void
512+
{
513+
$first = true;
514+
$this->terminal->expects($this->any())
515+
->method('read')
516+
->willReturn('c', 'x');
517+
518+
$style = $this->getStyle($this->terminal);
519+
520+
$action = function (CliMenu $menu) {
521+
$menu->close();
522+
};
523+
$item = new SelectableItem('Item 1', $action);
524+
525+
$menu = new CliMenu('PHP School FTW', [$item], $this->terminal, $style);
526+
$menu->addCustomControlMappings([
527+
'c' => $action,
528+
'x' => $action
529+
]);
530+
531+
$menu->open();
532+
static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch());
533+
534+
$first = false;
535+
$menu->open();
536+
static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch());
537+
}
538+
483539
public function testRemoveCustomControlMappingThrowsExceptionIfNoSuchMappingExists() : void
484540
{
485541
$this->expectException(\InvalidArgumentException::class);
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)