From 0439009624769942aed659ac06afd8bb1e594d86 Mon Sep 17 00:00:00 2001 From: Sam Rapaport Date: Mon, 6 Feb 2017 21:12:19 -0800 Subject: [PATCH 1/3] Add documentation for the MockClientStrategy See [#90](https://github.com/php-http/discovery/pull/90). --- discovery.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/discovery.rst b/discovery.rst index 6c01b03..82f7725 100644 --- a/discovery.rst +++ b/discovery.rst @@ -13,6 +13,7 @@ Currently available discovery services: - PSR-7 Message Factory Discovery - PSR-7 URI Factory Discovery - PSR-7 Stream Factory Discovery +- Mock Client Discovery (not enabled by default) The principle is always the same: you call the static ``find`` method on the discovery service if no explicit implementation was specified. The discovery service will try to locate a suitable implementation. @@ -233,6 +234,35 @@ This type of discovery finds a URI factory for a PSR-7_ URI implementation:: } } +Mock Client Discovery +--------------------------- + +You may find yourself testing parts of your application that are dependent on an HTTP Client using the Discovery Service, but do not necessarily need to perform the request nor contain any special configuration. In this case, the ``Http\Mock\Client`` from the ``php-http/mock-client`` package is typically used to fake requests and keep your tests nicely decoupled. However, for the best stability in a production environment, the mock client is not set to be found via the Discovery Service. Attempting to run a test which relies on discovery and uses a mock client will result in an ``Http\Discovery\Exception\NotFoundException``. Thankfully, Discovery gives us a Mock Client strategy that can be added straight to the Discovery. Let's take a look:: + + use MyCustomService; + use Http\Mock\Client as MockClient; + use Http\Discovery\HttpClientDiscovery; + use Http\Discovery\Strategy\MockClientStrategy; + + class MyCustomServiceTest extends TestCase + { + public function setUp() + { + HttpClientDiscovery::prependStrategy(MockClientStrategy::class); + + $this->service = new ThirdPartyService; + } + + public function testMyCustomServiceDoesSomething() + { + // Test... + } + } + +In the example of a test class above, we have our ``MyCustomService`` which relies on an HTTP Client implementation. We do not need to test that the actual request our custom service makes is successful in this test class, so it makes sense to use the Mock Client. However, we do want to make sure that our dependency injection using the Discovery service properly works, as this is a major feature of our service. By calling the ``HttpClientDiscovery``'s ``prependStrategy`` method and passing in the ``MockClientStrategy`` namespace, we have now added the ability to discover the mock client and our tests will work as desired. + +It is important to note that you must explicitly enable the ``MockClientStrategy`` and that it is not used by the Discovery Service by default. It is simply provided as a convenient option when writing tests. + Integrating your own implementation with the discovery mechanism using Puli --------------------------------------------------------------------------- From ffd8dc9339162064608c2924d20bf49fe3efa508 Mon Sep 17 00:00:00 2001 From: Sam Rapaport Date: Mon, 6 Feb 2017 21:27:27 -0800 Subject: [PATCH 2/3] Update discovery.rst --- discovery.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discovery.rst b/discovery.rst index 82f7725..9055175 100644 --- a/discovery.rst +++ b/discovery.rst @@ -250,7 +250,7 @@ You may find yourself testing parts of your application that are dependent on an { HttpClientDiscovery::prependStrategy(MockClientStrategy::class); - $this->service = new ThirdPartyService; + $this->service = new MyCustomService; } public function testMyCustomServiceDoesSomething() From c1af05eecb523835f455c9a199f18b1afe8121e0 Mon Sep 17 00:00:00 2001 From: Sam Rapaport Date: Tue, 7 Feb 2017 10:23:41 -0800 Subject: [PATCH 3/3] Break lines at 80 characters. --- discovery.rst | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/discovery.rst b/discovery.rst index 9055175..5fd2692 100644 --- a/discovery.rst +++ b/discovery.rst @@ -237,7 +237,16 @@ This type of discovery finds a URI factory for a PSR-7_ URI implementation:: Mock Client Discovery --------------------------- -You may find yourself testing parts of your application that are dependent on an HTTP Client using the Discovery Service, but do not necessarily need to perform the request nor contain any special configuration. In this case, the ``Http\Mock\Client`` from the ``php-http/mock-client`` package is typically used to fake requests and keep your tests nicely decoupled. However, for the best stability in a production environment, the mock client is not set to be found via the Discovery Service. Attempting to run a test which relies on discovery and uses a mock client will result in an ``Http\Discovery\Exception\NotFoundException``. Thankfully, Discovery gives us a Mock Client strategy that can be added straight to the Discovery. Let's take a look:: +You may find yourself testing parts of your application that are dependent on an +HTTP Client using the Discovery Service, but do not necessarily need to perform +the request nor contain any special configuration. In this case, the +``Http\Mock\Client`` from the ``php-http/mock-client`` package is typically used +to fake requests and keep your tests nicely decoupled. However, for the best +stability in a production environment, the mock client is not set to be found +via the Discovery Service. Attempting to run a test which relies on discovery +and uses a mock client will result in an ``Http\Discovery\Exception\NotFoundException``. +Thankfully, Discovery gives us a Mock Client strategy that can be added straight +to the Discovery. Let's take a look:: use MyCustomService; use Http\Mock\Client as MockClient; @@ -259,9 +268,19 @@ You may find yourself testing parts of your application that are dependent on an } } -In the example of a test class above, we have our ``MyCustomService`` which relies on an HTTP Client implementation. We do not need to test that the actual request our custom service makes is successful in this test class, so it makes sense to use the Mock Client. However, we do want to make sure that our dependency injection using the Discovery service properly works, as this is a major feature of our service. By calling the ``HttpClientDiscovery``'s ``prependStrategy`` method and passing in the ``MockClientStrategy`` namespace, we have now added the ability to discover the mock client and our tests will work as desired. - -It is important to note that you must explicitly enable the ``MockClientStrategy`` and that it is not used by the Discovery Service by default. It is simply provided as a convenient option when writing tests. +In the example of a test class above, we have our ``MyCustomService`` which +relies on an HTTP Client implementation. We do not need to test that the actual +request our custom service makes is successful in this test class, so it makes +sense to use the Mock Client. However, we do want to make sure that our +dependency injection using the Discovery service properly works, as this is a +major feature of our service. By calling the ``HttpClientDiscovery``'s +``prependStrategy`` method and passing in the ``MockClientStrategy`` namespace, +we have now added the ability to discover the mock client and our tests will +work as desired. + +It is important to note that you must explicitly enable the ``MockClientStrategy`` +and that it is not used by the Discovery Service by default. It is simply +provided as a convenient option when writing tests. Integrating your own implementation with the discovery mechanism using Puli ---------------------------------------------------------------------------