Skip to content

[HttpClient] Mention list of callbacks for MockHttpClient #17985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions http_client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1758,6 +1758,33 @@ responses dynamically when it's called::
$client = new MockHttpClient($callback);
$response = $client->request('...'); // calls $callback to get the response

You can also pass a list of callbacks if you need to perform specific
assertions on the request before returning the mocked response::

$expectedRequests = [
function ($method, $url, $options) {
$this->assertSame('GET', $method);
$this->assertSame('https://example.com/api/v1/customer', $url);

return new MockResponse('...');
},
function ($method, $url, $options) {
$this->assertSame('POST', $method);
$this->assertSame('https://example.com/api/v1/customer/1/products', $url);

return new MockResponse('...');
},
];

$client = new MockHttpClient($expectedRequest);

// ...

.. versionadded:: 5.1

Passing a list of callbacks to the ``MockHttpClient`` was introduced
in Symfony 5.1.

.. tip::

Instead of using the first argument, you can also set the (list of)
Expand Down