From a3b29af385db7441a3fe01ae9ae9d9b971e45a69 Mon Sep 17 00:00:00 2001 From: Lee Boynton Date: Fri, 8 Jul 2016 13:28:03 +0100 Subject: [PATCH] Add method to get last request sent When using this convenience client to send requests, you do not generally create the request object itself. The method added here provides you with a way to access the request that was created when using methods such as get, post, patch etc. --- spec/HttpMethodsClientSpec.php | 10 ++++++++++ src/HttpMethodsClient.php | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/spec/HttpMethodsClientSpec.php b/spec/HttpMethodsClientSpec.php index 07c0b47..85da4d0 100644 --- a/spec/HttpMethodsClientSpec.php +++ b/spec/HttpMethodsClientSpec.php @@ -85,6 +85,16 @@ function it_sends_request_with_underlying_client(HttpClient $client, MessageFact $this->beConstructedWith($client, $messageFactory); $this->sendRequest($request)->shouldReturn($response); } + + function it_returns_last_request(HttpClient $client, MessageFactory $messageFactory, RequestInterface $request, ResponseInterface $response) + { + $client->sendRequest($request)->shouldBeCalled()->willReturn($response); + + $this->beConstructedWith($client, $messageFactory); + $this->getLastRequest()->shouldReturn(null); + $this->sendRequest($request)->shouldReturn($response); + $this->getLastRequest()->shouldReturn($request); + } } class HttpMethodsClientStub extends HttpMethodsClient diff --git a/src/HttpMethodsClient.php b/src/HttpMethodsClient.php index 10b80e3..4dc7ff3 100644 --- a/src/HttpMethodsClient.php +++ b/src/HttpMethodsClient.php @@ -36,6 +36,11 @@ class HttpMethodsClient implements HttpClient */ private $messageFactory; + /** + * @var RequestInterface + */ + private $lastRequest; + /** * @param HttpClient $httpClient The client to send requests with. * @param MessageFactory $messageFactory The message factory to create requests. @@ -200,6 +205,20 @@ public function send($method, $uri, array $headers = [], $body = null) */ public function sendRequest(RequestInterface $request) { + $this->lastRequest = $request; + return $this->httpClient->sendRequest($request); } + + /** + * Get the last request that was sent via this client. + * + * Note that this will not return requests made directly through the underlying client's sendRequest method. + * + * @return RequestInterface|null + */ + public function getLastRequest() + { + return $this->lastRequest; + } }