Today, I came up with a piece of code that tests the PHP function.
<?php
/**
* Parameter Array $paramlist description
*
* The first-dimensional index of the array is the parameter name of the function that needs to be tested, and each element of the second dimension is the possible value that the parameter needs to be tested, and the element value can be an array.
*/
$ParamList = Array ("PARAM1" => Array (3,4,3,2,1),
"Param2" => Array (3,2,5),
"Param3" => Array (0,0.5,1,1.5));
Test function
Systestfunction ("Test", $ParamList);
Functions to be tested
function Test ($Param 1, $Param 2, $Param 3)
{
Return $Param 1. "|" . $Param 2. "|" . $Param 3;
}
/**
* Automatic Testing
*
* @author confined <leeyupeng@163.com>
* @param string $FunctionName function name
* @param array $ParamList parameter list
* @return Array
*/
function Systestfunction ($FunctionName, $ParamList)
{
if (empty ($FunctionName))
{
echo "function name cannot be null";
return false;
}
if (!is_array ($ParamList))
{
echo "parameter is not a 2-D array";
return false;
}
$TestParamList = Syscombinearray ($ParamList);
echo "Start test function." $FunctionName. "<br/>";
foreach ($TestParamList as $Key => $TestParamInfo)
{
echo "Start test First". $Key. "Group parameters: <br/>";
foreach ($TestParamInfo as $ParamKey => $Param)
{
${"Param". $ParamKey} = $Param;
$TempParamList [] = "$Param". $ParamKey;
if (Is_array ($Param))
{
echo "Parameters". $ParamKey. ", the type is an array:";
echo "<pre>";
Print_r ($Param);
}
ElseIf (Is_bool ($Param))
{
echo "Parameters". $ParamKey. ", the type is boll:";
if ($Param)
{
echo "true";
}
Else
{
echo "false";
}
}
Else
{
echo "Parameters". $ParamKey. ", type is a string or number:";
Echo $Param;
}
echo "<br/>";
}
$Params = Join (",", $TempParamList);
Unset ($TempParamList);
Eval ("$TestReturnResult =". $FunctionName. "(" . $Params. ");");
if (Is_array ($TestReturnResult))
{
echo "function returns array:<pre>";
Print_r ($TestReturnResult);
}
ElseIf (Is_bool ($TestReturnResult))
{
if ($TestReturnResult)
{
echo "function returns True";
}
Else
{
echo "function returns false";
}
}
Else
{
echo function returns a number or string: ". $TestReturnResult;
}
echo "<br/><br/>";
}
}
/**
* Compute a combination of functions
*
* @author confined <leeyupeng@163.com>
* @param array $CombinList 2-D arrays to be arranged
* The array after the combination of @return array
*/
function Syscombinearray ($CombinList)
{
if (!is_array ($CombinList))
{
echo "parameter is not a 2-D array";
return false;
}
/* Compute the value of C (a,1) * C (b, 1) * ... * C (n, 1)
$CombineCount = 1;
foreach ($CombinList as $Key => $Value)
{
$CombineCount *= count ($Value);
}
$RepeatTime = $CombineCount;
foreach ($CombinList as $ClassNo => $ParamList)
{
The maximum number of repetitions of an element in a $ParamList that appears vertically after it is split into a combination
$RepeatTime = $RepeatTime/count ($ParamList);
$StartPosition = 1;
foreach ($ParamList as $Param)
{
$TempStartPosition = $StartPosition;
$SpaceCount = $CombineCount/count ($ParamList)/$RepeatTime;
for ($J = 1; $J <= $SpaceCount; $J + +)
{
for ($I = 0; $I < $RepeatTime; $I + +)
{
$Result [$TempStartPosition + $I] [$ClassNo] = $Param;
}
$TempStartPosition + + $RepeatTime * COUNT ($ParamList);
}
$StartPosition + + $RepeatTime;
}
}
return $Result;
}
?>