1

I am trying to unit test a function(updateMeeting) in laravel which uses a curl request inside it. I tried many google links but nothing worked. can you guys give me suggestions on this matter.

following are relevant methods. $this->meeting is a MeetingModel

public function updateMeeting($meetingId){
$meeting = $this->meeting->find($meetingId);
 $endpoint = \Config::get('api_backend_endpoint');

$response = $this->curlPut($endpoint);

if ($response->http_status_code != 200) {

            if(Input::get('source') == 'ajax') {
                $apiResponse = app(ApiResponse::class);
                $message = 'ERROR Updating Meeting ' . $meeting->name;

                return $apiResponse->failed($message);
            }

            return Redirect::route('meetings.show', $meetingId)
                ->withInput()
                ->with('message', 'An error occurred hiding meeting with message: ' . $response->errors);
        }

        $meeting->save();

}

following is curlPut method

private function curlPut($url, $payload = array())
    {

        $data_string = json_encode($payload);

        $ch = curl_init();
        //send through our payload as post fields
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_USERPWD, $this->username . ":" . $this->password);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string))
        );

        $buffer = json_decode(curl_exec($ch));

        curl_close($ch);

        return $buffer;
    }
Deemantha
  • 433
  • 9
  • 30

1 Answers1

-1

Use a webservice like https://www.mocky.io/. Instead of requesting the real API, you send your requests to their API. You can create mock endpoints which return a response you can define.

common sense
  • 3,775
  • 6
  • 22
  • 31
  • 1
    Need to mock only inside testing. not the actual code. mate. anyway thanks. this is useful for my knowledge. – Deemantha Dec 04 '18 at 01:03