2016-08-16 89 views
0

我想測試一個Api函數,但Post方法發送null作爲參數。這裏是我的代碼PHPUnit 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"]); 
} 

我要訪問的狀態代碼由函數發送到客戶端,但我得到{「錯誤」:「太少或錯誤的參數」}相反,它表明我該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; 
    } 

} 

請給它一個眼神,大家

回答

0

testPOST()您嘗試訪問未在規定的$result變量您碼。你必須解碼響應的主體。如果服務器使用JSON你可以試試這個:

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

這將看到response()方法的代碼很有趣。

+0

我已經更新了代碼,但它仍然沒有工作,給出了相同的錯誤,因爲POST傳遞null作爲參數... –