Skip to content

Provide mock guzzle client #283

@pavlepredic

Description

@pavlepredic

I don't see any obvious way of mocking API responses when using this SDK. The problem is that the guzzle client is created in the GraphRequest object, so it can't be provided to the Graph object when instantiating. That makes unit testing really difficult. A proposed solution is to:

  • Add an optional argument to the Graph constructor and the GraphRequest constructor, accepting an instance of \GuzzleHttp\Client
  • Pass the provided client (if any) to the GraphRequest when createRequest is called
  • Return the provided client from GraphRequest::createGuzzleClient - if it was provided during construction at all.

Then you could do this in your unit tests:

$mockGuzzle = $this->createMock(\GuzzleHttp\Client::class);
//mock the response here or use the mock handler in guzzle
$graph = new Graph($mockGuzzle);
$myClient = new MyClient($graph); 
$myClient->fetchEmails(); //example  method that might be utilizing the SDK to make an API call

Apart from this, do you see other workarounds for writing unit tests that utilize this SDK? The solution with a proxy is not really acceptable for unit tests - though it may be just fine for debugging.
AB#7363

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions