diff --git a/clients/mock-client.rst b/clients/mock-client.rst index c4b9faf..5f7b927 100644 --- a/clients/mock-client.rst +++ b/clients/mock-client.rst @@ -226,6 +226,9 @@ Or pass a callable, and return a response or exception based on the request:: .. hint:: - If you're using the :doc:`/integrations/symfony-bundle`, the mock client is available as a service with ``httplug.client.mock`` id. + If you're using the :doc:`/integrations/symfony-bundle`, the mock client is + available as a service with ``httplug.client.mock`` id. + + See :ref:`symfony-functional-tests` for more on how to use the mock client in Symfony. .. include:: includes/further-reading-async.inc diff --git a/integrations/symfony-bundle.rst b/integrations/symfony-bundle.rst index 3a81efa..25cf100 100644 --- a/integrations/symfony-bundle.rst +++ b/integrations/symfony-bundle.rst @@ -507,6 +507,8 @@ The only steps they need is ``require`` one of the adapter implementations in their projects ``composer.json`` and instantiating the ``HttplugBundle`` in their kernel. +.. _symfony-functional-tests: + Mock Responses In Functional Tests `````````````````````````````````` @@ -522,8 +524,13 @@ Then, use the mock client factory in your test environment configuration: factory: 'httplug.factory.mock' # replace factory The client is always wrapped into a plugin client. Therefore you need to access -the inner client, which has `.client` appended. For the example above, the full -name is `httplug.clients.my_awesome_backend.client`. +the inner client to get the mock client. It is available in the container with +the suffix ``.inner``. For the example above, the full name is +``httplug.clients.my_awesome_backend.inner``. + +If you enable a decorator like ``http_methods_client: true``, the actual mock +client will be at ``httplug.client.my_awesome_backend.http_methods.inner``. Use +the ``container:debug`` command to make sure you grab the correct service. To mock a response in your tests, do: @@ -539,6 +546,22 @@ To mock a response in your tests, do: $response->method('getBody')->willReturn(/* Psr\Http\Message\Interface instance containing expected response content. */); $client->getContainer()->get('httplug.clients.my_awesome_backend.client')->addResponse($response); +.. hint:: + + If you do not specify the factory in your configuration, you can directly + overwrite the httplug services: + + # config/services_test.yaml + services: + # overwrite the http clients for mocking + httplug.client.my_awesome_backend: + class: Http\Mock\Client + public: true + + With this method, the plugin client is not applied. However, if you + configure a decorator, your mock client will still be decorated and the + mock available as service ``....inner``. + Read more on how the mock client works in the :doc:`mock client documentation `. .. |clearfloat| raw:: html