mercredi 13 mars 2019

Laravel 5.6 Mock Guzzle Response

I am trying to mock a guzzle response from a specific api.

My controller code looks like this (amended for brevity):

class SomeClass
{

    private $guzzle;

    public function __construct(\GuzzleHttp\Client $guzzle) {
        $this->guzzle = new $guzzle();
    }

    public function makeRequest(){

        $client = $this->guzzle;

        $url = 'http//somerurl';
        $options = [];

        $response = $client->request('POST', $url, $options);    

        return $response;
    }
}

And the test looks something like this (again edited)...

public function someTest(){

     $mock = $this->createMock(\GuzzleHttp\Client::class);

     $mock->method('request')->willReturn([
         'response' => 'somedata'
     ]);

     $someClass = new $SomeClass($mock);

     $response = $someClass->makeRequest();

     $body = $response->getBody();

     ...
}

At this point the test returns "Call to a member function getBody on null";

How can the getBody response of a guzzle call be tested?

Thank you in advance...



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire