Mock Builder

The Mock Builder creates a fluid interface to build HTTP mocks with an expressive syntax to test with Guzzle, Symfony, or other HTTP Clients. The main idea of Mock Builder is to create an expectation and to return a predefined response. Take a look at the following example:

use EasyHttp\MockBuilder\HttpMock;
use EasyHttp\MockBuilder\MockBuilder;

$builder = new MockBuilder();

$mock = new HttpMock($builder);

The when() method creates an expectation to store all constraints to meet the request. In the above example, the request must meet POST method and /v1/products path. When these constraints are met the mock returns the response created with then() method. In this case, the response must return the bar string in the body content.

Notice that you don't have to use a client based on Easy Http Contracts to use the Mock Builder. You could use the Mock Builder directly with Guzzle, Symfony, or any http client of your preference. Of course, our recommendation is to use this builder in conjunction with any implementation of Easy Http.

Take a look at how to use the above builder with Guzzle Client.

use GuzzleHttp\HandlerStack;
use GuzzleHttp\Client;

$client = new Client(['handler' => HandlerStack::create($mock)]);
    ->getContents(); // bar

As we expect, the body contents will be what we define in the mock builder (i.e bar).