dongyi1748 2018-07-16 05:49
浏览 64
已采纳

如何用PHP编写测试用例并推入多维数组

Here i am having three field like name,email,mobile. here what i am doing means i writing the test case for those fields in PHP,lets take name field condition like

  1. empty condition
  2. name should not be number
  3. name minimum 3 charector

suppose above conditions failed means i have to push to Failed Condition array, i tried i am not getting my expected results,kindly any one update my code

My code

    <?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
if(!empty($name) && !empty($email) && !empty($phone)){

    // Scenario = 1
    if(!preg_match("/^[a-zA-Z'-]+$/", $name)){
        $namevalidation['status'] = "Fail" ;
        $namevalidation['Failed Condition'][]['name'][]['Name should not be a number'] = "Condition Failed" ;

    }
    // Scenario = 2
    $namelength = strlen($name);

    if($namelength > 2){

    }else{
    $namevalidation['Failed Condition'][]['name'][]['Minimum Charector'] = "Condition Failed" ;
    }


    if(!empty($namevalidation)){
        echo json_encode($namevalidation);
    }

}else{
    $mandatory['status'] = "Fail" ;
    $mandatory['Error Message'] = "Mandatory fields mismatch";
    if(empty($name)){
    $mandatory['Required Fields'][] = "Name is mandatory";
    } 
    if(empty($email)){
    $mandatory['Required Fields'][] = "Email is mandatory";
    }
    if(empty($phone)){
    $mandatory['Required Fields'][] = "Phone is mandatory";
    }
    if(!empty($mandatory)){
    echo json_encode($mandatory);
    }
}


?>

Expected Output

{
"status": "Fail",
"Failed Condition": [
    {
        "name": [
            {
                "Name should not be a number": "Condition Failed"
            },
            {
                "Minimum Charector": "Condition Failed"
            }
        ]
    }
]
}

But i am getting out put like this

{
"status": "Fail",
"Failed Condition": [
    {
        "name": [
            {
                "Name should not be a number": "Condition Failed"
            }
        ]
    },
    {
        "name": [
            {
                "Minimum Charector": "Condition Failed"
            }
        ]
    }
]
}
  • 写回答

1条回答 默认 最新

  • du4822 2018-07-16 06:55
    关注

    You used [] in your code that creates new arrays instead of appending the previously created array.

    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    if(!empty($name) && !empty($email) && !empty($phone)){
    
        // Scenario = 1
        if(!preg_match("/^[a-zA-Z'-]+$/", $name)){
            $namevalidation['status'] = "Fail" ;
            $namevalidation['Failed Condition'][0]['name'][]['Name should not be a number'] = "Condition Failed" ;
    
        }
        // Scenario = 2
        $namelength = strlen($name);
    
        if($namelength > 2){
    
        }else{
        $namevalidation['Failed Condition'][0]['name'][]['Minimum Charector'] = "Condition Failed" ;
        }
    
    
        if(!empty($namevalidation)){
            echo json_encode($namevalidation);
        }
    
    }else{
        $mandatory['status'] = "Fail" ;
        $mandatory['Error Message'] = "Mandatory fields mismatch";
        if(empty($name)){
        $mandatory['Required Fields'][] = "Name is mandatory";
        } 
        if(empty($email)){
        $mandatory['Required Fields'][] = "Email is mandatory";
        }
        if(empty($phone)){
        $mandatory['Required Fields'][] = "Phone is mandatory";
        }
        if(!empty($mandatory)){
        echo json_encode($mandatory);
        }
    }
    

    However your current code creates different main structure of the Json if a different input is made.

    Say the name is 'ab' then your code does not add the $namevalidation['status'] = "Fail" ;.

    My suggestion is that you in "scenario 2" do a isset() check and if it's not set then you set it.

    if($namelength > 2){
    
    }else{
    $namevalidation['Failed Condition'][0]['name'][]['Minimum Charector'] = "Condition Failed" ;
    if(!isset($namevalidation['status'])) $namevalidation['status'] = "Fail" ;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?