diff --git a/README.md b/README.md index ffdbe74..6498553 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,6 @@ In order to use unsupported serializer you should create an adapter which implem - [x] Form Data body matcher (partial & exact) - [x] Multipart form body matcher (just like callback matcher but parses the body as a multipart form data) - [x] **BREAKING CHANGE:** Rename serializer decorators to serializer adapters. +- [x] Real network response for mocked & unmatched requests. - [ ] `symfony/http-client` support. -- [ ] Real network response for mocked & unmatched requests. - [ ] Document everything (with examples if it’s feasible). diff --git a/src/PockBuilder.php b/src/PockBuilder.php index 9b596da..e521702 100644 --- a/src/PockBuilder.php +++ b/src/PockBuilder.php @@ -46,6 +46,8 @@ use Pock\Traits\JsonDecoderTrait; use Pock\Traits\JsonSerializerAwareTrait; use Pock\Traits\XmlSerializerAwareTrait; use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; use RuntimeException; use Throwable; @@ -645,6 +647,22 @@ class PockBuilder $this->replyWithFactory(new CallbackReplyFactory($callback)); } + /** + * Reply to the request using provided client. Can be used to send real network request. + * + * @param \Psr\Http\Client\ClientInterface $client + * @SuppressWarnings(unused) + */ + public function replyWithClient(ClientInterface $client): void + { + $this->replyWithCallback(function ( + RequestInterface $request, + PockResponseBuilder $responseBuilder + ) use ($client): ResponseInterface { + return $client->sendRequest($request); + }); + } + /** * Resets the builder. * diff --git a/tests/src/PockBuilderTest.php b/tests/src/PockBuilderTest.php index a8b8be2..70963c5 100644 --- a/tests/src/PockBuilderTest.php +++ b/tests/src/PockBuilderTest.php @@ -1035,10 +1035,29 @@ EOF; throw new RuntimeException('Exception from the callback'); }); - $builder->getClient()->sendRequest(self::getPsr17Factory()->createRequest( - RequestMethod::GET, - self::TEST_URI - )); + $builder->getClient()->sendRequest(self::getPsr17Factory()->createRequest( + RequestMethod::GET, + self::TEST_URI + )); + } + + public function testReplyWithClient(): void + { + $inlined = new PockBuilder(); + $inlined->reply(429); + + $builder = new PockBuilder(); + $builder->matchMethod(RequestMethod::GET) + ->matchUri(self::TEST_URI) + ->always() + ->replyWithClient($inlined->getClient()); + + $response = $builder->getClient()->sendRequest(self::getPsr17Factory()->createRequest( + RequestMethod::GET, + self::TEST_URI + )); + + self::assertEquals(429, $response->getStatusCode()); } public function matchXmlNoXslProvider(): array