@@ -13,6 +13,7 @@ Currently available discovery services:
13
13
- PSR-7 Message Factory Discovery
14
14
- PSR-7 URI Factory Discovery
15
15
- PSR-7 Stream Factory Discovery
16
+ - Mock Client Discovery (not enabled by default)
16
17
17
18
The principle is always the same: you call the static ``find `` method on the discovery service if no explicit
18
19
implementation was specified. The discovery service will try to locate a suitable implementation.
@@ -233,6 +234,54 @@ This type of discovery finds a URI factory for a PSR-7_ URI implementation::
233
234
}
234
235
}
235
236
237
+ Mock Client Discovery
238
+ ---------------------------
239
+
240
+ You may find yourself testing parts of your application that are dependent on an
241
+ HTTP Client using the Discovery Service, but do not necessarily need to perform
242
+ the request nor contain any special configuration. In this case, the
243
+ ``Http\Mock\Client `` from the ``php-http/mock-client `` package is typically used
244
+ to fake requests and keep your tests nicely decoupled. However, for the best
245
+ stability in a production environment, the mock client is not set to be found
246
+ via the Discovery Service. Attempting to run a test which relies on discovery
247
+ and uses a mock client will result in an ``Http\Discovery\Exception\NotFoundException ``.
248
+ Thankfully, Discovery gives us a Mock Client strategy that can be added straight
249
+ to the Discovery. Let's take a look::
250
+
251
+ use MyCustomService;
252
+ use Http\Mock\Client as MockClient;
253
+ use Http\Discovery\HttpClientDiscovery;
254
+ use Http\Discovery\Strategy\MockClientStrategy;
255
+
256
+ class MyCustomServiceTest extends TestCase
257
+ {
258
+ public function setUp()
259
+ {
260
+ HttpClientDiscovery::prependStrategy(MockClientStrategy::class);
261
+
262
+ $this->service = new MyCustomService;
263
+ }
264
+
265
+ public function testMyCustomServiceDoesSomething()
266
+ {
267
+ // Test...
268
+ }
269
+ }
270
+
271
+ In the example of a test class above, we have our ``MyCustomService `` which
272
+ relies on an HTTP Client implementation. We do not need to test that the actual
273
+ request our custom service makes is successful in this test class, so it makes
274
+ sense to use the Mock Client. However, we do want to make sure that our
275
+ dependency injection using the Discovery service properly works, as this is a
276
+ major feature of our service. By calling the ``HttpClientDiscovery ``'s
277
+ ``prependStrategy `` method and passing in the ``MockClientStrategy `` namespace,
278
+ we have now added the ability to discover the mock client and our tests will
279
+ work as desired.
280
+
281
+ It is important to note that you must explicitly enable the ``MockClientStrategy ``
282
+ and that it is not used by the Discovery Service by default. It is simply
283
+ provided as a convenient option when writing tests.
284
+
236
285
Integrating your own implementation with the discovery mechanism using Puli
237
286
---------------------------------------------------------------------------
238
287
0 commit comments