Skip to content

Commit

Permalink
Adding confirmation trait.
Browse files Browse the repository at this point in the history
The confirmation trait allows you to ask for confirmation and returns a
boolean value based on the answer.
  • Loading branch information
Danzabar committed Nov 26, 2014
1 parent aec31f9 commit e0bef8d
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 4 deletions.
14 changes: 11 additions & 3 deletions README.md
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.
122 changes: 122 additions & 0 deletions src/Traits/Confirmation.php
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;
}

}
50 changes: 49 additions & 1 deletion tests/Commands/FakeTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*/
class FakeTask extends Command
{
use Traits\Question;
use Traits\Question,
Traits\Confirmation;

/**
* The name
Expand Down Expand Up @@ -103,6 +104,53 @@ public function choiceAction(Array $params)
}
}

/**
* The confirmation test action
*
* @return void
* @author Dan Cox
*/
public function confirmationAction()
{
$confirm = $this->confirm();

if($confirm)
{
$this->output->write("Thanks for confirming");
} else
{
$this->output->write("Action cancelled");
}
}

/**
* A confirmation with explicit set and values changed
*
* @return void
* @author Dan Cox
*/
public function explicitConfirmAction(Array $params)
{
$this->setConfirmationNo('no');
$this->setConfirmationYes('yes');
$this->setConfirmExplicit(TRUE);

if(!empty($params))
{
$this->setInvalidConfirmationError($params[0]);
}

$confirm = $this->confirm("Please confirm that you wish to continue... (Yes|No)");

if($confirm)
{
$this->output->writeln("Confirmed");
} else
{
$this->output->writeln("Cancelled");
}
}




Expand Down
104 changes: 104 additions & 0 deletions tests/Traits/ConfirmationTest.php
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

0 comments on commit e0bef8d

Please sign in to comment.