I'm trying wi write a test for a scenario where in a pool of requests, one specific URL fails with a ConnectException (meaning that there was no response at all, in fact, the request could not be made).
In the code we deal with this like so:
$responses = Http::pool(function (Pool $pool) use ($user) {
$pool->as('first')->timeout(5)
->get('http://non-existing-third-party.com/endpoint', [
'foo' => 'bar',
]);
$pool->as('second')->timeout(5)
->get('http://third-party.com/second-endpoint', [
'foo' => 'bar2OrWhatever',
]);
});
foreach ($responses as $response) {
if ($response instanceof TransferException) {
// Throw another exception that we can expect in tests.
}
if ($response->failed()) {
// Response code => 400
}
// handle success of that request
}
What I'm searching for is how to test the ConnectException/TransferException scenario here?
Tryied so far with
Http::fake([
'http://non-existing-third-party.com/endpoint'=> fn () => new ConnectException('Connection error', $request),
'http://third-party.com/second-endpoint'=> Http::response(...),
]);
But that just gives me an error
"Call to undefined method GuzzleHttp\Exception\ConnectException::then()" at
/home/vendor/laravel/framework/src/Illuminate/Http/Client/PendingRequest.php:916
Is there a way to fake a request exception in the HTTP facade?