Introduction: This is used to test PHP functions.CodeThe details page of PHP, related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 323734 'rolling = 'no'>
Today, I suddenly thought of writing a piece of code to test the PHP function.
<? PHP
/**
* Parameter array $ paramlist description
*
* The first-dimension index of the array is the parameter name of the function to be tested. Each element in the second-dimension is the possible value to be tested for this parameter. 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 the Function
Systestfunction ("test", $ paramlist );
// Function to be tested
Function Test ($ param1, $ param2, $ param3)
{
Return $ param1. "|". $ param2. "|". $ param3;
}
/**
* Automatic Test
*
* @ Author Li Yupeng <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 blank ";
Return false;
}
If (! Is_array (current ($ paramlist )))
{
Echo "the parameter is not a two-dimensional array ";
Return false;
}
$ Testparamlist = syscombinearray ($ paramlist );
Echo "start to test the function". $ functionname. "<br/> ";
Foreach ($ testparamlist as $ key => $ testparaminfo)
{
Echo "Start test". $ key. "group parameters: <br/> ";
Foreach ($ testparaminfo as $ paramkey => $ PARAM)
{
$ {"Param". $ paramkey} = $ Param;
$ Tempparamlist [] = "$ Param". $ paramkey;
If (is_array ($ PARAM ))
{
Echo "parameter". $ paramkey. ", type: array :";
Echo "<PRE> ";
Print_r ($ PARAM );
}
Elseif (is_bool ($ PARAM ))
{
Echo "parameter". $ paramkey. ", type: boll :";
If ($ PARAM)
{
Echo "true ";
}
Else
{
Echo "false ";
}
}
Else
{
Echo "parameter". $ paramkey. ", type: string or number :";
Echo $ Param;
}
Echo "<br/> ";
}
$ Params = join (",", $ tempparamlist );
Unset ($ tempparamlist );
Eval ("$ testreturnresult =". $ functionname. "(". $ Params .");");
If (is_array ($ testreturnresult ))
{
Echo "array returned by the function: <PRE> ";
Print_r ($ testreturnresult );
}
Elseif (is_bool ($ testreturnresult ))
{
If ($ testreturnresult)
{
Echo "the function returns true ";
}
Else
{
Echo "the function returns false ";
}
}
Else
{
Echo "function returns a number or string:". $ testreturnresult;
}
Echo "<br/> ";
}
}
/**
* Compute the combined functions
*
* @ Author Li Yupeng <leeyupeng@163.com>
* @ Param array $ combinlist the two-dimensional array to be sorted and combined
* @ Return array Array
*/
Function syscombinearray ($ combinlist)
{
If (! Is_array (current ($ combinlist )))
{
Echo "the parameter is not a two-dimensional array ";
Return false;
}
/* Calculate 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)
{
// $ Maximum number of times that an element in the paramlist appears vertically after being 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;
}
?>
More articles on "Code for testing PHP functions"
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/323734.html pageno: 16.