Is it possible to use Pact with wrapper Client cla...
# pact-php
p
Is it possible to use Pact with wrapper Client class?
Copy code
use GuzzleHttp\Client;

Class InternalClient extends Client
{
    // ....
}

Class UserApi
{
    public function __construct(private InterClient $client)
    {
         
    }
}
In this case, I should mock InternalClient or UserApi? Now, I got this error
Copy code
Server error: `GET <http://localhost:7200/interactions/verification>` resulted in a `500 Internal Server Error` response:
Actual interactions do not match expected interactions for mock MockService.

Incorrect requests:
	GET /v1/sso-user/id (truncated...)
m
I don’t see how a wrapper client class has anything to do with the problem
the problem is your expectation doesn’t match what your code is doing
What your api client (wrapper or otherwise) does needs to match up with your unit test expectations
what does that look like?
p
The problem should cause by
base_uri
, I the API Client not use this to set default endpoint. The endpoint will be placed in UserAPi like this
Copy code
Class UserApi
{
    public function __construct(private InterClient $client)
    {
         $this->client->setEndpoint('<http://localhost/v1/user>');
    }
}
But in the example of Pact PHP see only a way to mock with base_uri.
Copy code
$service = new HttpClientService($config->getBaseUri()); // Pass in the URL to the Mock Server.
        $result  = $service->getHelloString('Bob'); // Make the real API request against the Mock Server.
Because our API Client does not use base_uri. It makes a request by itself like this
Copy code
public function request($method, $uri = '', array $options = []) : ResponseInterface
    {
        $uri = $this->baseUrl . $uri;

        try {
            $response = $this->makeRequest($method, $uri, $options);
    ....
}
This is an override Client method. Now I try to create a new Class using the default Client class (GuzzleHttp) instead of the custom Client API class. See it work properly. Need to try more . . .
m
You need to be able to dynamically configure your API client to send requests to the mock server, and not the real one. How you do that Pact doesn’t really care
p
OK, Thank you so much. Really need to take more time on this. 🙂
👍 1