From d981df25e4daee48e1ce2f9dfbf7eeb907aae407 Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Mon, 30 Mar 2015 17:34:44 +0300 Subject: [PATCH] update checkRequiredParamsForArray() --- src/bb-library/Box/Validate.php | 11 ++++++- tests/bb-library/Box/ValidateTest.php | 42 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/bb-library/Box/Validate.php b/src/bb-library/Box/Validate.php index dc5e6bd42..19d603596 100644 --- a/src/bb-library/Box/Validate.php +++ b/src/bb-library/Box/Validate.php @@ -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); } } diff --git a/tests/bb-library/Box/ValidateTest.php b/tests/bb-library/Box/ValidateTest.php index 7ba72774b..37dc23025 100644 --- a/tests/bb-library/Box/ValidateTest.php +++ b/tests/bb-library/Box/ValidateTest.php @@ -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); + } + } \ No newline at end of file