如何用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"
            }
        ]
    }
]
}
查看全部
dongyi1748
dongyi1748
2018/07/16 05:49
  • php
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复