From 7a05d83b76ba169b277d9af8ee1d07628df108dc Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Wed, 3 Jan 2018 17:32:39 +0100 Subject: [PATCH 1/5] Add "Mock responses in functional tests" part --- integrations/symfony-bundle.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/integrations/symfony-bundle.rst b/integrations/symfony-bundle.rst index 2d2241a..f4f3730 100644 --- a/integrations/symfony-bundle.rst +++ b/integrations/symfony-bundle.rst @@ -205,6 +205,7 @@ services are: * ``httplug.factory.guzzle6`` * ``httplug.factory.react`` * ``httplug.factory.socket`` +* ``httplug.factory.mock`` (Install ``php-http/mock-client``first) Plugins ``````` @@ -342,6 +343,26 @@ The only steps they need is ``require`` one of the adapter implementations in their projects ``composer.json`` and instantiating the ``HttplugBundle`` in their kernel. +Mock responses in functional tests +`````````````````````````````````` +To mock responses in your functional tests, proceed as follow: + +.. code-block:: yaml + # config_test.yml + httplug: + clients: + my_awesome_client: + factory: 'httplug.factory.mock' # replace factory +And in your tests: +.. code-block:: php + // SomeWebTestCase.php + $client = static::createClient(); + // $client->disableReboot(); You might uncomment this if your client (BrowserKit) make multiple requests as kernel is rebooted on each request. + + $response = $this->createMock('Psr\Http\Message\ResponseInterface'); + $response->method('getBody')->willReturn(/* Psr\Http\Message\Interface instance containing expected response content. */); + $client->getContainer()->get('httplug.client.mock')->addResponse($response); + .. |clearfloat| raw:: html
From 42d06977aeda488a1f5e09a3e994f7818f15777b Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Wed, 3 Jan 2018 17:59:29 +0100 Subject: [PATCH 2/5] Fix missing WS --- integrations/symfony-bundle.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/integrations/symfony-bundle.rst b/integrations/symfony-bundle.rst index f4f3730..75420ff 100644 --- a/integrations/symfony-bundle.rst +++ b/integrations/symfony-bundle.rst @@ -353,6 +353,7 @@ To mock responses in your functional tests, proceed as follow: clients: my_awesome_client: factory: 'httplug.factory.mock' # replace factory + And in your tests: .. code-block:: php // SomeWebTestCase.php From 8a85c0895e28ac1723afdba5c01d38f39e343c85 Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Wed, 3 Jan 2018 18:04:50 +0100 Subject: [PATCH 3/5] Fix missing WS --- integrations/symfony-bundle.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/integrations/symfony-bundle.rst b/integrations/symfony-bundle.rst index 75420ff..ece955a 100644 --- a/integrations/symfony-bundle.rst +++ b/integrations/symfony-bundle.rst @@ -348,6 +348,7 @@ Mock responses in functional tests To mock responses in your functional tests, proceed as follow: .. code-block:: yaml + # config_test.yml httplug: clients: @@ -355,7 +356,9 @@ To mock responses in your functional tests, proceed as follow: factory: 'httplug.factory.mock' # replace factory And in your tests: + .. code-block:: php + // SomeWebTestCase.php $client = static::createClient(); // $client->disableReboot(); You might uncomment this if your client (BrowserKit) make multiple requests as kernel is rebooted on each request. From d82bec10b83518ac2f64f66baa06b8babf5beed0 Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Thu, 4 Jan 2018 11:11:21 +0100 Subject: [PATCH 4/5] Add link to mock-client lib, update wording. --- integrations/symfony-bundle.rst | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/integrations/symfony-bundle.rst b/integrations/symfony-bundle.rst index ece955a..807283b 100644 --- a/integrations/symfony-bundle.rst +++ b/integrations/symfony-bundle.rst @@ -205,7 +205,7 @@ services are: * ``httplug.factory.guzzle6`` * ``httplug.factory.react`` * ``httplug.factory.socket`` -* ``httplug.factory.mock`` (Install ``php-http/mock-client``first) +* ``httplug.factory.mock`` (Install ``php-http/mock-client`` first) Plugins ``````` @@ -343,9 +343,11 @@ The only steps they need is ``require`` one of the adapter implementations in their projects ``composer.json`` and instantiating the ``HttplugBundle`` in their kernel. -Mock responses in functional tests +Mock Responses In Functional Tests `````````````````````````````````` -To mock responses in your functional tests, proceed as follow: + +First thing to do is add the :doc:`php-http/mock-client ` to your ``require-dev`` section. +Then, use the mock client factory in your test environment configuration: .. code-block:: yaml @@ -355,13 +357,15 @@ To mock responses in your functional tests, proceed as follow: my_awesome_client: factory: 'httplug.factory.mock' # replace factory -And in your tests: +To mock a response in your tests, do: .. code-block:: php // SomeWebTestCase.php $client = static::createClient(); - // $client->disableReboot(); You might uncomment this if your client (BrowserKit) make multiple requests as kernel is rebooted on each request. + + // If your test has the client (BrowserKit) make multiple requests, you need to disable reboot as the kernel is rebooted on each request. + // $client->disableReboot(); $response = $this->createMock('Psr\Http\Message\ResponseInterface'); $response->method('getBody')->willReturn(/* Psr\Http\Message\Interface instance containing expected response content. */); From 231a0a58bc4e6d8e81c065002eca8443bde101ff Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Thu, 4 Jan 2018 11:32:16 +0100 Subject: [PATCH 5/5] Add hint about Symfony bundle. --- clients/mock-client.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/clients/mock-client.rst b/clients/mock-client.rst index 3e7e010..ecf3e74 100644 --- a/clients/mock-client.rst +++ b/clients/mock-client.rst @@ -140,4 +140,9 @@ Or set a default exception:: } } + +.. hint:: + + If you're using the :doc:`/integrations/symfony-bundle`, the mock client is available as a service with ``httplug.client.mock`` id. + .. include:: includes/further-reading-async.inc