vendredi 3 janvier 2020

How do I mock guzzle request

How do I make a mock of third party api call . That is happening from controller . I have this line of code in controller .

public function store(){
  $response =    $request->post('http://thirdpaty.app/rmis/api/ebp/requests', [
                  "headers" => [
                      'Content-Type' => 'application/json',
                   ],
                "json" => [
                  "data"=>1
                   ]
            ]);
                $data = json_decode($response->getBody()->getContents());
                $token = $data->token;

         // Saving that token to database

}

And from the test I am doing

$response = $this->post('/to-store-method');

How do I mock the api request . So that in testing I don't have to call the third api request .

Right now I am doing

if(app()->get('env') == 'testing'){
     $token = 123;
}else{
     //Api call here
  }

Is there any better alternative of doing this test

Aucun commentaire:

Enregistrer un commentaire