Skip to content

Commit 0caf6a4

Browse files
GaryPEGEOTdbu
authored andcommitted
Add "Mock responses in functional tests" part (#220)
1 parent 2e22c0c commit 0caf6a4

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

clients/mock-client.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,9 @@ Or set a default exception::
140140
}
141141
}
142142

143+
144+
.. hint::
145+
146+
If you're using the :doc:`/integrations/symfony-bundle`, the mock client is available as a service with ``httplug.client.mock`` id.
147+
143148
.. include:: includes/further-reading-async.inc

integrations/symfony-bundle.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ services are:
205205
* ``httplug.factory.guzzle6``
206206
* ``httplug.factory.react``
207207
* ``httplug.factory.socket``
208+
* ``httplug.factory.mock`` (Install ``php-http/mock-client`` first)
208209

209210
Plugins
210211
```````
@@ -342,6 +343,34 @@ The only steps they need is ``require`` one of the adapter implementations in
342343
their projects ``composer.json`` and instantiating the ``HttplugBundle`` in
343344
their kernel.
344345

346+
Mock Responses In Functional Tests
347+
``````````````````````````````````
348+
349+
First thing to do is add the :doc:`php-http/mock-client </clients/mock-client>` to your ``require-dev`` section.
350+
Then, use the mock client factory in your test environment configuration:
351+
352+
.. code-block:: yaml
353+
354+
# config_test.yml
355+
httplug:
356+
clients:
357+
my_awesome_client:
358+
factory: 'httplug.factory.mock' # replace factory
359+
360+
To mock a response in your tests, do:
361+
362+
.. code-block:: php
363+
364+
// SomeWebTestCase.php
365+
$client = static::createClient();
366+
367+
// If your test has the client (BrowserKit) make multiple requests, you need to disable reboot as the kernel is rebooted on each request.
368+
// $client->disableReboot();
369+
370+
$response = $this->createMock('Psr\Http\Message\ResponseInterface');
371+
$response->method('getBody')->willReturn(/* Psr\Http\Message\Interface instance containing expected response content. */);
372+
$client->getContainer()->get('httplug.client.mock')->addResponse($response);
373+
345374
.. |clearfloat| raw:: html
346375

347376
<div style="clear:left"></div>

0 commit comments

Comments
 (0)