Home  >  Article  >  php教程  >  测试php函数的方法

测试php函数的方法

WBOY
WBOYOriginal
2016-06-13 09:30:551099browse

今天忽然想到的,就写了一段测试php函数的代码。

复制代码 代码如下:


/**
 * 参数数组$ParamList说明
 *
 * 数组的第一维索引是需要测试的函数的参数名,第二维的每个元素是该参数需要测试的可能值,元素值可以为数组。
 */
$ParamList = array("Param1" => array(3,4,3,2,1),
                   "Param2" => array(3,2,5),
                   "Param3" => array(0,0.5,1,1.5));
// 测试函数
sysTestFunction("Test", $ParamList);

// 待测试的函数
function Test($Param1, $Param2, $Param3)
{
    return $Param1 . "|" . $Param2 . "|" . $Param3;
}

/**
 * 自动测试
 *
 * @param  string  $FunctionName  函数名称
 * @param  array   $ParamList     参数列表
 * @return array
 */
function sysTestFunction($FunctionName, $ParamList)
{
    if(empty($FunctionName))
    {
        echo "函数名不能为空";
        return false;
    }
    if(!is_array(current($ParamList)))
    {
        echo "参数不是2维数组";
        return false;
    }
    $TestParamList = sysCombineArray($ParamList);
    echo "开始测试函数" . $FunctionName . "
";
    foreach($TestParamList as $Key => $TestParamInfo)
    {
        echo "开始测试第" . $Key . "组参数:
";
        foreach($TestParamInfo as $ParamKey => $Param)
        {
            ${"Param" . $ParamKey} = $Param;
            $TempParamList[] = "$Param" . $ParamKey;
            if(is_array($Param))
            {
                echo "参数" . $ParamKey . ",类型为数组:";
                echo "

";<br>                print_r($Param);<br>            }<br>            elseif(is_bool($Param))<br>            {<br>                echo "参数" . $ParamKey . ",类型为boll:";<br>                if($Param)<br>                {<br>                    echo "true";<br>                }<br>                else<br>                {<br>                    echo "false";<br>                }<br>            }<br>            else<br>            {<br>                echo "参数" . $ParamKey . ",类型为字符串或数字:";<br>                echo $Param;<br>            }<br>            echo "<br>";<br>        }<br>        $Params = join(", ", $TempParamList);<br>        unset($TempParamList);<br>        eval("$TestReturnResult = " . $FunctionName . "(" . $Params . ");");<br>        if(is_array($TestReturnResult))<br>        {<br>            echo "函数返回数组:<pre class="brush:php;toolbar:false">";<br>            print_r($TestReturnResult);<br>        }<br>        elseif(is_bool($TestReturnResult))<br>        {<br>            if($TestReturnResult)<br>            {<br>                echo "函数返回true";<br>            }<br>            else<br>            {<br>                echo "函数返回false";<br>            }<br>        }<br>        else<br>        {<br>            echo "函数返回数字或字符串:" . $TestReturnResult;<br>        }<br>        echo "<br><br>";<br>    }<br>}<br>/**<br> * 计算组合的函数<br> *<br> * @param  array $CombinList 待排列组合的2维数组<br> * @return array             组合后的数组<br> */<br>function sysCombineArray($CombinList)<br>{<br>    if(!is_array(current($CombinList)))<br>    {<br>        echo "参数不是2维数组";<br>        return false;<br>    }<br>    /* 计算C(a,1) * C(b, 1) * ... * C(n, 1)的值 */<br>    $CombineCount = 1;<br>    foreach($CombinList as $Key => $Value)<br>    {<br>        $CombineCount *= count($Value);<br>    }<br>    $RepeatTime = $CombineCount;<br>    foreach($CombinList as $ClassNo => $ParamList)<br>    {<br>        // $ParamList中的元素在拆分成组合后纵向出现的最大重复次数<br>        $RepeatTime = $RepeatTime / count($ParamList);<br>        $StartPosition = 1;<br>        foreach($ParamList as $Param)<br>        {<br>            $TempStartPosition = $StartPosition;<br>            $SpaceCount = $CombineCount / count($ParamList) / $RepeatTime;<br>            for($J = 1; $J             {<br>                for($I = 0; $I                 {<br>                   $Result[$TempStartPosition + $I][$ClassNo] = $Param;<br>                }<br>                $TempStartPosition += $RepeatTime * count($ParamList);<br>            }<br>            $StartPosition += $RepeatTime;<br>        }<br>    }<br>    return $Result;<br>}<br>?><br>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn