@@ -2311,7 +2311,26 @@ when making HTTP requests you might face errors at transport level.
23112311
23122312That's why it's useful to test how your application behaves in case of a transport
23132313error. :class: `Symfony\\ Component\\ HttpClient\\ Response\\ MockResponse ` allows
2314- you to do so, by yielding the exception from its body::
2314+ you to do so in multiple ways.
2315+
2316+ In order to test errors that occur before headers have been received,
2317+ set the ``error `` option value when creating the ``MockResponse ``.
2318+ Transport errors of this kind occur, for example, when a host name
2319+ cannot be resolved or the host was unreachable. The
2320+ ``TransportException `` will be thrown as soon as a method like
2321+ ``getStatusCode() `` or ``getHeaders() `` is called.
2322+
2323+ In order to test errors that occur while a response is being streamed
2324+ (that is, after the headers have already been received), provide the
2325+ exception to ``MockResponse `` as part of the ``body ``
2326+ parameter. You can either use an exception directly, or yield the
2327+ exception from a callback. For exceptions of this kind,
2328+ ``getStatusCode() `` may indicate a success (200), but accessing
2329+ ``getContent() `` fails.
2330+
2331+ The following example code illustrates all three options.
2332+
2333+ body::
23152334
23162335 // ExternalArticleServiceTest.php
23172336 use PHPUnit\Framework\TestCase;
@@ -2326,10 +2345,16 @@ you to do so, by yielding the exception from its body::
23262345 {
23272346 $requestData = ['title' => 'Testing with Symfony HTTP Client'];
23282347 $httpClient = new MockHttpClient([
2329- // You can create the exception directly in the body...
2348+ // Mock a transport level error at a time before
2349+ // headers have been received (e. g. host unreachable)
2350+ new MockResponse(info: ['error' => 'host unreachable']),
2351+
2352+ // Mock a response with headers indicating
2353+ // success, but a failure while retrieving the body by
2354+ // creating the exception directly in the body...
23302355 new MockResponse([new \RuntimeException('Error at transport level')]),
23312356
2332- // ... or you can yield the exception from a callback
2357+ // ... or by yielding it from a callback.
23332358 new MockResponse((static function (): \Generator {
23342359 yield new TransportException('Error at transport level');
23352360 })()),
0 commit comments