search
Homephp教程php手册测试php函数的方法

今天忽然想到的,就写了一段测试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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment