Skip to content

Commit

Permalink
Add support for array parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
hglattergotz committed Nov 26, 2015
1 parent a583ef8 commit 3e16c07
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 16 deletions.
18 changes: 12 additions & 6 deletions src/HGG/ParameterValidator/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,16 @@ protected function parse(array $definitions)
{
foreach ($this->rawParams as $rKey => $rVal) {
if (array_key_exists($rKey, $definitions)) {
$value = $this->validateValue($rVal, $definitions[$rKey]);
if (is_array($rVal)) {
$value = [];

foreach ($rVal as $key => $val) {
$value[$key] = $this->validateValue($val, $definitions[$rKey]);
}
} else {
$value = $this->validateValue($rVal, $definitions[$rKey]);
}

$this->parsedParams[$rKey] = $value;
unset($this->rawParams[$rKey]);

Expand Down Expand Up @@ -148,12 +157,9 @@ private function handleMissingRequiredParameters()
*/
protected function validateValue($value, $definition)
{
try
{
try {
return $definition->getValidator()->validate($value);
}
catch (\Exception $e)
{
} catch (\Exception $e) {
throw new \Exception(sprintf('[%s] %s', $definition->getName(), $e->getMessage()));
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/HGG/ParameterValidator/Parameter/TextParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@

use HGG\ParameterValidator\Parameter\Validator\TextValidator;

/**
* TextParameter
*
* @uses Parameter
* @author Henning Glatter-Götz <henning@glatter-gotz.com>
*/
class TextParameter extends Parameter
{
/**
* getDefaultValidator
*
* @access protected
* @return void
*/
protected function getDefaultValidator()
{
return new TextValidator();
Expand Down
155 changes: 145 additions & 10 deletions tests/HGG/ParameterValidator/Test/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,34 @@
use HGG\ParameterValidator\Parameter\Parameter;
use HGG\ParameterValidator\Parameter\NumberParameter;
use HGG\ParameterValidator\Parameter\TextParameter;
use HGG\ParameterValidator\Parameter\TextArrayParameter;
use HGG\ParameterValidator\Parameter\DateParameter;
use HGG\ParameterValidator\Parameter\DatetimeParameter;
use HGG\ParameterValidator\Parameter\BooleanParameter;
use HGG\ParameterValidator\ParameterDefinition;
use HGG\ParameterValidator\Input;

/**
* InputTest
*
* @author Henning Glatter-Götz <henning@glatter-gotz.com>
*/
class InputTest extends \PHPUnit_Framework_TestCase
{
/**
* def
*
* @var mixed
* @access protected
*/
protected $def;

/**
* setUp
*
* @access protected
* @return void
*/
protected function setUp()
{
$this->def = new ParameterDefinition();
Expand All @@ -35,6 +53,14 @@ protected function setUp()
'Some more details could go here'
)
)
->addParameter(
new TextParameter(
'opt-txt-array',
Parameter::OPTIONAL,
'This is an optional text array parameter',
'Some more details could go here'
)
)
->addParameter(
new DateParameter(
'opt-date',
Expand All @@ -61,12 +87,23 @@ protected function setUp()
);
}

/**
* tearDown
*
* @access protected
* @return void
*/
protected function tearDown()
{}

/**
* testParamterAlreadyExists
*
* @expectedException Exception
* @expectedExceptionMessage Parameter with name 'req-num' already exists!
*
* @access public
* @return void
*/
public function testParamterAlreadyExists()
{
Expand All @@ -81,6 +118,12 @@ public function testParamterAlreadyExists()
);
}

/**
* testParameterGetters
*
* @access public
* @return void
*/
public function testParameterGetters()
{
$numberParameter = new NumberParameter(
Expand All @@ -96,14 +139,21 @@ public function testParameterGetters()
$this->assertEquals('RandomValidatorClass', $numberParameter->getValidator());
}

/**
* testSunnyDay
*
* @access public
* @return void
*/
public function testSunnyDay()
{
$parameters = array(
'req-num' => 1234,
'opt-txt' => 'Some text value',
'opt-date' => '2000-01-01',
'opt-datetime' => '2000-01-01 23:00:00',
'opt-bool' => true
'req-num' => 1234,
'opt-txt' => 'Some text value',
'opt-txt-array' => array('type' => 'Some text value on index 0'),
'opt-date' => '2000-01-01',
'opt-datetime' => '2000-01-01 23:00:00',
'opt-bool' => true
);

$input = new Input($parameters, $this->def);
Expand All @@ -113,6 +163,12 @@ public function testSunnyDay()
$this->assertEquals($expected, $result);
}

/**
* testOmitOptionalParameter
*
* @access public
* @return void
*/
public function testOmitOptionalParameter()
{
$parameters = array(
Expand All @@ -127,8 +183,13 @@ public function testOmitOptionalParameter()
}

/**
* testOmitRequiredParameter
*
* @expectedException Exception
* @expectedExceptionMessage The required parameter 'req-num' is missing
*
* @access public
* @return void
*/
public function testOmitRequiredParameter()
{
Expand All @@ -140,8 +201,13 @@ public function testOmitRequiredParameter()
}

/**
* testOmitRequiredParameters
*
* @expectedException Exception
* @expectedExceptionMessage The required parameters 'req-num', 'req-num-2' are missing
*
* @access public
* @return void
*/
public function testOmitRequiredParameters()
{
Expand All @@ -162,8 +228,13 @@ public function testOmitRequiredParameters()
}

/**
* testAddUndefinedParameter
*
* @expectedException Exception
* @expectedExceptionMessage The parameter 'not-defined' is not valid
*
* @access public
* @return void
*/
public function testAddUndefinedParameter()
{
Expand All @@ -176,8 +247,13 @@ public function testAddUndefinedParameter()
}

/**
* testAddUndefinedParameters
*
* @expectedException Exception
* @expectedExceptionMessage The parameters 'not-defined', 'not-defined-2', 'not-defined-3' are not valid
*
* @access public
* @return void
*/
public function testAddUndefinedParameters()
{
Expand All @@ -191,62 +267,103 @@ public function testAddUndefinedParameters()
$input = new Input($parameters, $this->def);
}

/**
* testIncorrectParameterTypeNumber
*
* @expectedException Exception
*
* @access public
* @return void
*/
public function testIncorrectParameterTypeNumber()
{
$parameters = array(
'req-num' => 'this-is-not-a-number'
);

$this->setExpectedException('Exception');
$input = new Input($parameters, $this->def);
}

/**
* testIncorrectParameterTypeText
*
* @expectedException Exception
*
* @access public
* @return void
*/
public function testIncorrectParameterTypeText()
{
$parameters = array(
'req-num' => 1234,
'opt-txt' => true
);

$this->setExpectedException('Exception');
$input = new Input($parameters, $this->def);
}

/**
* testIncorrectParameterTypeDate
*
* @expectedException Exception
*
* @access public
* @return void
*/
public function testIncorrectParameterTypeDate()
{
$parameters = array(
'req-num' => 1234,
'opt-date' => 'this is not a date'
);

$this->setExpectedException('Exception');
$input = new Input($parameters, $this->def);
}

/**
* testIncorrectParameterTypeDateTime
*
* @expectedException Exception
*
* @access public
* @return void
*/
public function testIncorrectParameterTypeDateTime()
{
$parameters = array(
'req-num' => 1234,
'opt-datetime' => 'this is not a datetime'
);

$this->setExpectedException('Exception');
$input = new Input($parameters, $this->def);
}

/**
* testIncorrectParameterTypeBoolean
*
* @expectedException Exception
*
* @access public
* @return void
*/
public function testIncorrectParameterTypeBoolean()
{
$parameters = array(
'req-num' => 1234,
'opt-bool' => 'this is not a boolean'
);

$this->setExpectedException('Exception');
$input = new Input($parameters, $this->def);
}

/**
* testAllTrueBooleanPrameterValues
*
* @dataProvider allTrueBooleanDataProvider
*
* @param mixed $parameters
* @access public
* @return void
*/
public function testAllTrueBooleanPrameterValues($parameters)
{
Expand All @@ -264,6 +381,12 @@ public function testAllTrueBooleanPrameterValues($parameters)
$this->assertEquals($expected, $result);
}

/**
* allTrueBooleanDataProvider
*
* @access public
* @return void
*/
public function allTrueBooleanDataProvider()
{
$data = array(
Expand All @@ -287,7 +410,13 @@ public function allTrueBooleanDataProvider()
}

/**
* testAllFalseBooleanPrameterValues
*
* @dataProvider allFalseBooleanDataProvider
*
* @param mixed $parameters
* @access public
* @return void
*/
public function testAllFalseBooleanPrameterValues($parameters)
{
Expand All @@ -305,6 +434,12 @@ public function testAllFalseBooleanPrameterValues($parameters)
$this->assertEquals($expected, $result);
}

/**
* allFalseBooleanDataProvider
*
* @access public
* @return void
*/
public function allFalseBooleanDataProvider()
{
$data = array(
Expand Down

0 comments on commit 3e16c07

Please sign in to comment.