-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The confirmation trait allows you to ask for confirmation and returns a boolean value based on the answer.
- Loading branch information
Showing
4 changed files
with
286 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
phalcon-cli | ||
=========== | ||
CLI Tools for the Phalcon Framework | ||
=================================== | ||
|
||
Some tools for the Phalcon framework to make the CLI a better experience all around. | ||
[![Build Status](https://travis-ci.org/Danzabar/phalcon-cli.svg?branch=master)](https://travis-ci.org/Danzabar/phalcon-cli) [![Coverage Status](https://coveralls.io/repos/Danzabar/phalcon-cli/badge.png?branch=master)](https://coveralls.io/r/Danzabar/phalcon-cli?branch=master) | ||
|
||
An expansion to the Phalcon Frameworks CLI Classes. This includes things like Questions, Confirmation, Command test class, Input/Output Streams and Application wrapper that allows you to start a CLI with minimal Effort. | ||
|
||
This is still a work in progress at the moment, so more details including documentation and examples will follow. | ||
|
||
|
||
## Contributing | ||
If you want to contribute, great. Just fork this repo and make a pull request with changes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php namespace Danzabar\CLI\Traits; | ||
|
||
|
||
/** | ||
* The confirmation trait, confirm an action and return a Boolean value | ||
* | ||
* | ||
*/ | ||
Trait Confirmation | ||
{ | ||
|
||
/** | ||
* The value for a a "Yes" confirmation | ||
* | ||
* @var string | ||
*/ | ||
protected $confirmYes = 'Y'; | ||
|
||
/** | ||
* The value for a "No" confirmation | ||
* | ||
* @var string | ||
*/ | ||
protected $confirmNo = 'N'; | ||
|
||
/** | ||
* The error message that returns in the explicit option is set | ||
* and the answer user gives does not match the confirm vars | ||
* | ||
* @var string | ||
*/ | ||
protected $invalidConfirmationError = 'Unexpected confirmation.'; | ||
|
||
/** | ||
* If set to true this will only accept the confirm yesy | ||
* and confirm no values, otherwise any value that is not | ||
* equal to confirm yes will equate to a no | ||
* | ||
* @var Boolean | ||
*/ | ||
protected $explicit = FALSE; | ||
|
||
/** | ||
* Request basic confirmation returns a boolean value depending on the answer | ||
* | ||
* @return Boolean | ||
* @author Dan Cox | ||
*/ | ||
public function confirm($text = 'Do you wish to continue?') | ||
{ | ||
$this->output->writeln($text); | ||
|
||
return $this->convertToBool($this->input->getInput()); | ||
} | ||
|
||
/** | ||
* Converts the input to a boolean value depending on its answer | ||
* | ||
* @return Boolean | ||
* @author Dan Cox | ||
*/ | ||
public function convertToBool($answer) | ||
{ | ||
// If it equals confirm yes | ||
if($answer == $this->confirmYes) | ||
{ | ||
return true; | ||
} | ||
|
||
if($answer == $this->confirmNo || $this->explicit === false) | ||
{ | ||
return false; | ||
} | ||
|
||
$this->output->writeln($this->invalidConfirmationError); | ||
} | ||
|
||
/** | ||
* Sets the value of the confirmation | ||
* | ||
* @return void | ||
* @author Dan Cox | ||
*/ | ||
public function setConfirmationYes($value) | ||
{ | ||
$this->confirmYes = $value; | ||
} | ||
|
||
/** | ||
* Sets the reject value for confirmation | ||
* | ||
* @return void | ||
* @author Dan Cox | ||
*/ | ||
public function setConfirmationNo($value) | ||
{ | ||
$this->confirmNo = $value; | ||
} | ||
|
||
/** | ||
* Sets the explicit variable which controlls how answers are returned | ||
* | ||
* @return void | ||
* @author Dan Cox | ||
*/ | ||
public function setConfirmExplicit($switch) | ||
{ | ||
$this->explicit = $switch; | ||
} | ||
|
||
/** | ||
* Set the error message that shows on an invalid answer in explicit mode | ||
* | ||
* @return void | ||
* @author Dan Cox | ||
*/ | ||
public function setInvalidConfirmationError($error) | ||
{ | ||
$this->invalidConfirmationError = $error; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
use Danzabar\CLI\CommandTester; | ||
|
||
/** | ||
* Test case for the Confirmation Trait | ||
* | ||
* @package CLI | ||
* @subpackage Tests | ||
* @author Dan Cox | ||
*/ | ||
class ConfirmationTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Instance of the command tester | ||
* | ||
* @var Object | ||
*/ | ||
protected $CT; | ||
|
||
/** | ||
* Setup the command Tester | ||
* | ||
* @return void | ||
* @author Dan Cox | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->CT = new CommandTester(); | ||
} | ||
|
||
/** | ||
* Test the confirmation action | ||
* | ||
* @return void | ||
* @author Dan Cox | ||
*/ | ||
public function test_confirmationAction() | ||
{ | ||
$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. | ||
* | ||
* @return void | ||
* @author Dan Cox | ||
*/ | ||
public function test_confirmationDecline() | ||
{ | ||
$this->CT->setInput("N\n"); | ||
$this->CT->execute('Fake:confirmation'); | ||
|
||
$this->assertContains("Action cancelled", $this->CT->getOutput()); | ||
} | ||
|
||
/** | ||
* With explicit option switched off, any answer that isnt Y should be considered N | ||
* | ||
* @return void | ||
* @author Dan Cox | ||
*/ | ||
public function test_implicitAction() | ||
{ | ||
$this->CT->setInput("P\n"); | ||
$this->CT->execute('Fake:confirmation'); | ||
|
||
$this->assertContains("Action cancelled", $this->CT->getOutput()); | ||
} | ||
|
||
/** | ||
* Set to explicit and given a response it does not expect | ||
* | ||
* @return void | ||
* @author Dan Cox | ||
*/ | ||
public function test_confirmationError() | ||
{ | ||
$this->CT->setInput("P\n"); | ||
$this->CT->execute('Fake:explicitConfirm'); | ||
|
||
$this->assertContains("Unexpected confirmation", $this->CT->getOutput()); | ||
} | ||
|
||
/** | ||
* Set a custom confirmation error | ||
* | ||
* @return void | ||
* @author Dan Cox | ||
*/ | ||
public function test_customConfirmationError() | ||
{ | ||
$this->CT->setInput("P\n"); | ||
$this->CT->execute('Fake:explicitConfirm', Array('Invalid')); | ||
|
||
$this->assertContains("Invalid", $this->CT->getOutput()); | ||
} | ||
|
||
|
||
|
||
} // END class ConfirmationTest extends \PHPUnit_Framework_TestCase |