0

I wanted to test an Api function, but Post method is sending null as arguements. Here is my code

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"]);
}

I want to access the Status Code send to a client by the function, but I am getting {"Error":"Too few or wrong Arguments"} instead, It shows me that Api function is send this to me because of null parameters.. Here is the Api Function

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;
    }

}

Please give it a look, everyone

msmolcic
  • 6,407
  • 8
  • 32
  • 56

1 Answers1

1

In testPOST() you try to access the $result variable which is not defined in your code. You have to decode the body of the response. If the server use JSON you can try this:

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

It would be interesting to see the code of the response() method.

seboettg
  • 195
  • 8