real networking for mocked requests

This commit is contained in:
Pavel 2021-09-23 20:06:42 +03:00
parent f7832865a1
commit dcc642a0ea
3 changed files with 42 additions and 5 deletions

View File

@ -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 its feasible).

View File

@ -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.
*

View File

@ -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