Skip to content

function addCustomControlMappings() #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/CliMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ public function addCustomControlMapping(string $input, callable $callable) : voi
$this->customControlMappings[$input] = $callable;
}

/**
* Shorthand function to add multiple custom control mapping at once
*/
public function addCustomControlMappings(array $map) : void
{
foreach ($map as $input => $callable) {
$this->addCustomControlMapping($input, $callable);
}
}

/**
* Removes a custom control mapping
*/
Expand Down
56 changes: 56 additions & 0 deletions test/CliMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,62 @@ public function testAddCustomControlMapping() : void
static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch());
}

public function testAddCustomControlMappingsThrowsExceptionWhenOverwritingExistingDefaultControls() : void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot rebind this input');

$menu = new CliMenu('PHP School FTW', []);
$menu->addCustomControlMappings([
' ' => function () {
}
]);
}

public function testAddCustomControlMappingsThrowsExceptionWhenAttemptingToOverwriteAddedCustomControlMap() : void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot rebind this input');

$menu = new CliMenu('PHP School FTW', []);
$menu->addCustomControlMappings([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a little while to figure this - you actually only send one element to the method, the second c key overwrites the first ! So it's actually not possible to call this method once and cause a rebind exception, you'd have to call it twice.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hahaha, of course ! Thank you !

'c' => function () {
}
]);
$menu->addCustomControlMappings([
'c' => function () {
}
]);
}

public function testAddCustomControlMappings() : void
{
$first = true;
$this->terminal->expects($this->any())
->method('read')
->willReturn('c', 'x');

$style = $this->getStyle($this->terminal);

$action = function (CliMenu $menu) {
$menu->close();
};
$item = new SelectableItem('Item 1', $action);

$menu = new CliMenu('PHP School FTW', [$item], $this->terminal, $style);
$menu->addCustomControlMappings([
'c' => $action,
'x' => $action
]);

$menu->open();
static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch());

$first = false;
$menu->open();
static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch());
}

public function testRemoveCustomControlMappingThrowsExceptionIfNoSuchMappingExists() : void
{
$this->expectException(\InvalidArgumentException::class);
Expand Down
9 changes: 9 additions & 0 deletions test/res/testAddCustomControlMappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


 
 PHP School FTW 
 ========================================== 
 ● Item 1