diff --git a/src/HGG/ParameterValidator/Input.php b/src/HGG/ParameterValidator/Input.php index 1c46196..9ab40d3 100644 --- a/src/HGG/ParameterValidator/Input.php +++ b/src/HGG/ParameterValidator/Input.php @@ -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]); @@ -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())); } } diff --git a/src/HGG/ParameterValidator/Parameter/TextParameter.php b/src/HGG/ParameterValidator/Parameter/TextParameter.php index 1049c28..cfbb33a 100644 --- a/src/HGG/ParameterValidator/Parameter/TextParameter.php +++ b/src/HGG/ParameterValidator/Parameter/TextParameter.php @@ -4,8 +4,20 @@ use HGG\ParameterValidator\Parameter\Validator\TextValidator; +/** + * TextParameter + * + * @uses Parameter + * @author Henning Glatter-Götz + */ class TextParameter extends Parameter { + /** + * getDefaultValidator + * + * @access protected + * @return void + */ protected function getDefaultValidator() { return new TextValidator(); diff --git a/tests/HGG/ParameterValidator/Test/InputTest.php b/tests/HGG/ParameterValidator/Test/InputTest.php index 7b9d76f..d488113 100644 --- a/tests/HGG/ParameterValidator/Test/InputTest.php +++ b/tests/HGG/ParameterValidator/Test/InputTest.php @@ -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 + */ 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(); @@ -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', @@ -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() { @@ -81,6 +118,12 @@ public function testParamterAlreadyExists() ); } + /** + * testParameterGetters + * + * @access public + * @return void + */ public function testParameterGetters() { $numberParameter = new NumberParameter( @@ -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); @@ -113,6 +163,12 @@ public function testSunnyDay() $this->assertEquals($expected, $result); } + /** + * testOmitOptionalParameter + * + * @access public + * @return void + */ public function testOmitOptionalParameter() { $parameters = array( @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -191,16 +267,31 @@ 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( @@ -208,10 +299,17 @@ public function testIncorrectParameterTypeText() 'opt-txt' => true ); - $this->setExpectedException('Exception'); $input = new Input($parameters, $this->def); } + /** + * testIncorrectParameterTypeDate + * + * @expectedException Exception + * + * @access public + * @return void + */ public function testIncorrectParameterTypeDate() { $parameters = array( @@ -219,10 +317,17 @@ public function testIncorrectParameterTypeDate() '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( @@ -230,10 +335,17 @@ public function testIncorrectParameterTypeDateTime() '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( @@ -241,12 +353,17 @@ public function testIncorrectParameterTypeBoolean() '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) { @@ -264,6 +381,12 @@ public function testAllTrueBooleanPrameterValues($parameters) $this->assertEquals($expected, $result); } + /** + * allTrueBooleanDataProvider + * + * @access public + * @return void + */ public function allTrueBooleanDataProvider() { $data = array( @@ -287,7 +410,13 @@ public function allTrueBooleanDataProvider() } /** + * testAllFalseBooleanPrameterValues + * * @dataProvider allFalseBooleanDataProvider + * + * @param mixed $parameters + * @access public + * @return void */ public function testAllFalseBooleanPrameterValues($parameters) { @@ -305,6 +434,12 @@ public function testAllFalseBooleanPrameterValues($parameters) $this->assertEquals($expected, $result); } + /** + * allFalseBooleanDataProvider + * + * @access public + * @return void + */ public function allFalseBooleanDataProvider() { $data = array(