Skip to content

Commit

Permalink
Accepting lower case answers on confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
Danzabar committed Jan 4, 2016
1 parent 1a2ab99 commit 380c369
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Tasks/Helpers/Confirmation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class Confirmation extends Helper
*/
protected $confirmNo = 'N';

/**
* Specifies whether the answer should be case sensitive, Default false
*
* @var Boolean
*/
protected $caseSensitive = false;

/**
* The error message that returns in the explicit option is set
* and the answer user gives does not match the confirm vars
Expand Down Expand Up @@ -63,6 +70,10 @@ public function confirm($text = 'Do you wish to continue?')
*/
public function convertToBool($answer)
{
if (!$this->caseSensitive) {
$answer = strtoupper($answer);
}

// If it equals confirm yes
if ($answer == $this->confirmYes) {
return true;
Expand Down Expand Up @@ -105,6 +116,16 @@ public function setConfirmExplicit($switch)
$this->explicit = $switch;
}

/**
* Sets whether the answer should be case sensitive
*
* @return void
*/
public function setCaseSensitive($switch)
{
$this->caseSensitive = $switch;
}

/**
* Set the error message that shows on an invalid answer in explicit mode
*
Expand Down
1 change: 1 addition & 0 deletions tests/Commands/FakeTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public function explicitConfirm()

$confirmation->setConfirmationNo('no');
$confirmation->setConfirmationYes('yes');
$confirmation->setCaseSensitive(true);
$confirmation->setConfirmExplicit(true);

if (isset($this->argument->error)) {
Expand Down
14 changes: 14 additions & 0 deletions tests/Tasks/Helpers/ConfirmationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ public function test_confirmationAction()
$this->assertContains('Thanks for confirming', $this->CT->getOutput());
}

/**
* Test that the confirmation help accepts lowercase answers
*
* @return void
* @author Dan Cox
*/
public function test_caseSensitive()
{
$this->CT->setInput("y\n");
$this->CT->execute('fake:confirmation');

$this->assertContains('Thanks for confirming', $this->CT->getOutput());
}

/**
* Same as above, but we say no.
*
Expand Down

0 comments on commit 380c369

Please sign in to comment.