uccdev's avatar

Best practice for test cases for cURL testing?

Hi,

I'm using laravel frameworks to commit cURL requests and process its data. I'm able to do these things fine, but I want to build test cases for them too.

Here is an example of a cURL request function I might use:

 public function getLatestCourse() {
     $token = $this->accessToken;
     $headers = ['Authorization: Bearer ' . $token];

     $curl = curl_init();
     $url = "https://my.instructure.com/api/v1/accounts/1/courses?sort=created_at&order=desc&per_page=30";  

     curl_setopt_array($curl, [
   CURLOPT_RETURNTRANSFER => TRUE,
   CURLINFO_HEADER_OUT => TRUE,
   CURLOPT_URL => $url, 
   CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
   CURLOPT_SSL_VERIFYPEER => TRUE,
   CURLOPT_HTTPHEADER => $headers,
   CURLOPT_CUSTOMREQUEST => 'GET',
   CURLOPT_HEADER => 0,
   CURLOPT_RETURNTRANSFER, TRUE
]);
     if (curl_errno($curl)) {
       echo 'Error:' . curl_error($curl) . "<br>";
     } else { echo " all good<br>"; }
     $resp = curl_exec($curl);
     $data = json_decode($resp, true);
     curl_close($curl);
     return $data[0];
   }

My question, to be clear, is if given a request and an action like this, what kinds of unit test cases could I make for them? I feel like I'd have to restructure the code to test each individual part, but I'm not sure how

Any help would be greatly appreciated

0 likes
4 replies
amitjoshi's avatar

Hello @bobbybouwmann ,

Do you have any examples of mocking HTTP guzzle requests? i am trying to make a mock of HTTP Guzzle request using PHPUnit in laravel, but not working.

Please or to participate in this conversation.