update checkRequiredParamsForArray()

This commit is contained in:
Vaidas Lungis committed 2015-03-30 17:34:44 +03:00
1 parent 2b165891aa
commit d981df25e4
2 files changed
+52 -1

No files matched your search

+10 -1
View File
@@ -92,7 +92,16 @@ class Box_Validate
public function checkRequiredParamsForArray(array $required, array $data, array $variables = NULL, $code = 0)
{
foreach ($required as $key => $msg) {
if (!isset($data[$key]) || empty($data[$key])) {
if(!isset($data[$key])){
throw new \Box_Exception($msg, $variables, $code);
}
if (is_string($data[$key]) && strlen(trim($data[$key])) === 0){
throw new \Box_Exception($msg, $variables, $code);
}
if (!is_numeric($data[$key]) && empty($data[$key])){
throw new \Box_Exception($msg, $variables, $code);
}
}
+42
View File
@@ -162,4 +162,46 @@ class Box_ValidateTest extends PHPUnit_Framework_TestCase
$v->checkRequiredParamsForArray($required, $data, array(), 54321);
}
public function testcheckRequiredParamsForArray_KeyValueIsZero()
{
$data = array(
'amount' => 0
);
$required = array(
'amount' => 'amount must be set',
);
$v = new Box_Validate();
$v->checkRequiredParamsForArray($required, $data);
}
public function testcheckRequiredParamsForArray_EmptyString()
{
$data = array(
'message' => ''
);
$required = array(
'message' => 'message must be set',
);
$v = new Box_Validate();
$this->setExpectedException('\Box_Exception', $required['message']);
$v->checkRequiredParamsForArray($required, $data);
}
public function testcheckRequiredParamsForArray_EmptyStringFilledWithSpaces()
{
$data = array(
'message' => ' '
);
$required = array(
'message' => 'message must be set',
);
$v = new Box_Validate();
$this->setExpectedException('\Box_Exception', $required['message']);
$v->checkRequiredParamsForArray($required, $data);
}
}