ProcessMaker-BE "Calendar (PHPUnit)"
- Adding test for validate data
This commit is contained in:
@@ -262,13 +262,19 @@ class Calendar
|
||||
public function create($arrayData)
|
||||
{
|
||||
try {
|
||||
//Verify data
|
||||
$process = new \ProcessMaker\BusinessModel\Process();
|
||||
$validator = new \ProcessMaker\BusinessModel\Validator();
|
||||
|
||||
$validator->throwExceptionIfDataIsNotArray($arrayData, "\$arrayData");
|
||||
$validator->throwExceptionIfDataIsEmpty($arrayData, "\$arrayData");
|
||||
|
||||
//Set data
|
||||
$arrayData = \G::array_change_key_case2($arrayData, CASE_UPPER);
|
||||
|
||||
unset($arrayData["CAL_UID"]);
|
||||
|
||||
//Verify data
|
||||
$process = new \ProcessMaker\BusinessModel\Process();
|
||||
|
||||
$process->throwExceptionIfDataNotMetFieldDefinition($arrayData, $this->arrayFieldDefinition, $this->arrayFieldNameForException, true);
|
||||
|
||||
$this->throwExceptionIfExistsName($arrayData["CAL_NAME"], $this->arrayFieldNameForException["calendarName"]);
|
||||
@@ -321,11 +327,7 @@ class Calendar
|
||||
$arrayDataAux = array();
|
||||
$arrayDataAux["CALENDAR_UID"] = \G::generateUniqueID();
|
||||
$arrayDataAux["CALENDAR_NAME"] = $arrayData["CAL_NAME"];
|
||||
|
||||
if (isset($arrayData["CAL_DESCRIPTION"])) {
|
||||
$arrayDataAux["CALENDAR_DESCRIPTION"] = $arrayData["CAL_DESCRIPTION"];
|
||||
}
|
||||
|
||||
$arrayDataAux["CALENDAR_DESCRIPTION"] = (isset($arrayData["CAL_DESCRIPTION"]))? $arrayData["CAL_DESCRIPTION"] : "";
|
||||
$arrayDataAux["CALENDAR_WORK_DAYS"] = explode("|", $this->workDaysReplaceData(implode("|", $arrayData["CAL_WORK_DAYS"])));
|
||||
$arrayDataAux["CALENDAR_STATUS"] = $arrayData["CAL_STATUS"];
|
||||
|
||||
@@ -361,11 +363,17 @@ class Calendar
|
||||
public function update($calendarUid, $arrayData)
|
||||
{
|
||||
try {
|
||||
//Verify data
|
||||
$process = new \ProcessMaker\BusinessModel\Process();
|
||||
$validator = new \ProcessMaker\BusinessModel\Validator();
|
||||
|
||||
$validator->throwExceptionIfDataIsNotArray($arrayData, "\$arrayData");
|
||||
$validator->throwExceptionIfDataIsEmpty($arrayData, "\$arrayData");
|
||||
|
||||
//Set data
|
||||
$arrayData = \G::array_change_key_case2($arrayData, CASE_UPPER);
|
||||
|
||||
//Verify data
|
||||
$process = new \ProcessMaker\BusinessModel\Process();
|
||||
|
||||
$this->throwExceptionIfNotExistsCalendar($calendarUid, $this->arrayFieldNameForException["calendarUid"]);
|
||||
|
||||
$process->throwExceptionIfDataNotMetFieldDefinition($arrayData, $this->arrayFieldDefinition, $this->arrayFieldNameForException, false);
|
||||
|
||||
@@ -9,7 +9,8 @@ namespace ProcessMaker\BusinessModel;
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
class Validator{
|
||||
class Validator
|
||||
{
|
||||
/**
|
||||
* Validate dep_uid
|
||||
* @var string $dep_uid. Uid for Departament
|
||||
@@ -330,6 +331,47 @@ class Validator{
|
||||
throw (new \Exception("The field '$nameField' is empty."));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if data is array
|
||||
*
|
||||
* @param string $data Data
|
||||
* @param string $dataNameForException Data name for the exception
|
||||
*
|
||||
* return void Throw exception if data is not array
|
||||
*/
|
||||
public function throwExceptionIfDataIsNotArray($data, $dataNameForException)
|
||||
{
|
||||
try {
|
||||
if (!is_array($data)) {
|
||||
$msg = str_replace(array("{0}"), array($dataNameForException), "The data \"{0}\" is not array");
|
||||
|
||||
throw (new \Exception($msg));
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if data is empty
|
||||
*
|
||||
* @param string $data Data
|
||||
* @param string $dataNameForException Data name for the exception
|
||||
*
|
||||
* return void Throw exception if data is empty
|
||||
*/
|
||||
public function throwExceptionIfDataIsEmpty($data, $dataNameForException)
|
||||
{
|
||||
try {
|
||||
if (empty($data)) {
|
||||
$msg = str_replace(array("{0}"), array($dataNameForException), "The data \"{0}\" is empty");
|
||||
|
||||
throw (new \Exception($msg));
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user