0

我想测试一个 Api 函数,但 Post 方法发送 null 作为参数。这是我的代码

public function testPOST() {
    require 'vendor/autoload.php';

    // create our http client (Guzzle)
    $client = new \GuzzleHttp\Client();

    $data = array("MobileNumber" => "923024175176", "Type" => "mobile no validation");
    $url = 'http://192.168.8.101/ren-money/index.php/OTP/generateOTPbyType';

    $response = $client -> post($url, $data);
    echo $response -> getBody();
    $this -> assertEquals(1, (int)$result["StatusCode"]);
}

我想访问该函数发送给客户端的状态代码,但我得到的是 {"Error":"Too few or wrong Arguments"},这表明 Api 函数由于参数为空而将其发送给我。 . 这里是Api函数

public function generateOTPbyType_post() {
    //getting number from post
    $Number = $this -> post("MobileNumber");
    //getting type from post
    $Type = strtolower($this -> post("Type"));

    //Generating 6 digit Random number
    $Pin = random_string('numeric', 6);

    //checking weather all/requied parametters are provided correctly
    if ($Number === NULL || $Type === NULL) {
        $this -> response(array("StatusCode" => "2", "Description" => "Too few or wrong Arguments"));
        //return 2;
    }

    //infoBip SDK code for sending pin to user
    $Client = new infobip\api\client\SendSingleTextualSms(new infobip\api\configuration\BasicAuthConfiguration("husnainMalik", "BlueRose"));
    $RequestBody = new infobip\api\model\sms\mt\send\textual\SMSTextualRequest();
    $RequestBody -> setFrom("InfoSMS");
    $RequestBody -> setTo($Number);

    //checking type
    if ($Type === 'mobile no validation' || $Type === 'goods delivery' || $Type === 'goods receipt') {
        $DbObj = array("number" => $Number, "key" => $Pin, "time" => (int)time(), "status" => "0", "type" => strtolower($Type));
        //checking DB error and saving into DB
        if ($this -> OTP_Model -> saveOTP($DbObj) === FALSE) {
            $this -> response(array("StatusCode" => "2", "Description" => "Internal Db Error"));
            //return 2;
        }

        //infoBip SDK setting parametters
        $RequestBody -> setText("Your " . strtolower($Type) . " code: " . $Pin);
        $Response = json_decode(json_encode($Client -> execute($RequestBody)));
        $GrpName = $Response -> messages[0] -> status -> groupName;
        $Discription = $Response -> messages[0] -> status -> description;
        //InfoBip error handling
        if ("REJECTED" === $GrpName) {
            $this -> response(array("StatusCode" => "2", "Description" => $Discription));
            //return 2;
        } else {
            $this -> response(array("StatusCode" => "1", "Description" => $Discription));
            //return 1;
        }

    } else {
        $this -> response(array("StatusCode" => "2", "Description" => "Invalid Type"));
        //return 2;
    }

}

请大家看看

4

1 回答 1

1

testPOST()您尝试访问$result代码中未定义的变量时。您必须解码响应的正文。如果服务器使用 JSON,你可以试试这个:

$response = $client->post($url, $data);
$result = json_decode($response->getBody());
$this->assertEquals(1, intval($result["StatusCode"]));

看看方法的代码会很有趣response()

于 2016-08-16T13:56:41.670 回答