@@ -1387,8 +1387,8 @@ This allows using them where native PHP streams are needed::
1387
1387
// later on if you need to, you can access the response from the stream
1388
1388
$response = stream_get_meta_data($streamResource)['wrapper_data']->getResponse();
1389
1389
1390
- Testing HTTP Clients and Responses
1391
- ----------------------------------
1390
+ Testing
1391
+ -------
1392
1392
1393
1393
This component includes the ``MockHttpClient `` and ``MockResponse `` classes to
1394
1394
use them in tests that shouldn't make actual HTTP requests. Such tests can be
@@ -1402,7 +1402,10 @@ HTTP client in this component. You can use the interface to accept real client
1402
1402
in your code, while replacing it with ``MockHttpClient `` in the test.
1403
1403
1404
1404
When the ``request `` method is used on ``MockHttpClient ``, it will respond with
1405
- supplied ``MockResponse ``. There are few ways to use it:
1405
+ supplied ``MockResponse ``. There are few ways to use it, as described below.
1406
+
1407
+ HTTP Client and Responses
1408
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
1406
1409
1407
1410
The first way of using ``MockHttpClient `` is to pass a list of responses to its
1408
1411
constructor. These will be yielded in order when requests are made::
@@ -1461,6 +1464,39 @@ However, using ``MockResponse`` allows simulating chunked responses and timeouts
1461
1464
1462
1465
$mockResponse = new MockResponse($body());
1463
1466
1467
+ Testing Request Data
1468
+ ~~~~~~~~~~~~~~~~~~~~
1469
+
1470
+ The examples above describe how to return desired response. What if you wanted
1471
+ to also test the request itself? ``MockResponse `` comes with helper methods:
1472
+
1473
+ * ``getRequestMethod() `` - returns HTTP method
1474
+ * ``getRequestUrl() `` - returns URL the request would be sent to
1475
+ * ``getRequestOptions() `` - returns an array containing other information about
1476
+ the request such as headers, query parameters, body content etc.
1477
+
1478
+ Usage example::
1479
+
1480
+ $mockResponse = new MockResponse('', ['http_code' => 204]);
1481
+ $httpClient = new MockHttpClient($mockResponse, 'https://example.com/');
1482
+
1483
+ $response = $httpClient->request('DELETE', '/api/article/1337', [
1484
+ 'headers' => [
1485
+ 'Accept: */*',
1486
+ 'Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l',
1487
+ ],
1488
+ ]);
1489
+
1490
+ // returns "DELETE"
1491
+ $mockResponse->getRequestMethod();
1492
+
1493
+ // returns "https://example.com//api/article/1337"
1494
+ $mockResponse->getRequestUrl();
1495
+
1496
+ // returns ["Accept: */*", "Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l"]
1497
+ $mockResponse->getRequestOptions()['headers'];
1498
+
1499
+
1464
1500
.. _`cURL PHP extension` : https://www.php.net/curl
1465
1501
.. _`PSR-17` : https://www.php-fig.org/psr/psr-17/
1466
1502
.. _`PSR-18` : https://www.php-fig.org/psr/psr-18/
0 commit comments