From 64b169c10def3aa9fd987e5ccf0936ccaf366d93 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Thu, 22 Oct 2015 20:24:12 +0200 Subject: [PATCH] Update to last change of httplug - Remove Body implementation according to #3 - Remove options from sendRequest - Readapt BatchResult with changes from BatchResult / BatchException --- spec/BatchRequestSpec.php | 28 +++---- spec/BatchResultSpec.php | 19 ++++- spec/Body/CombinedMultipartSpec.php | 44 ----------- spec/Body/FilesSpec.php | 44 ----------- spec/Body/MultipartDataSpec.php | 40 ---------- spec/Body/UrlencodedDataSpec.php | 33 -------- ...hodsSpec.php => HttpMethodsClientSpec.php} | 42 +++++----- src/BatchRequest.php | 17 ++--- src/BatchResult.php | 76 +++++-------------- src/Body/CombinedMultipart.php | 45 ----------- src/Body/Files.php | 53 ------------- src/Body/Multipart.php | 40 ---------- src/Body/MultipartData.php | 67 ---------------- src/Body/UrlencodedData.php | 42 ---------- src/BodyGenerator.php | 30 -------- 15 files changed, 76 insertions(+), 544 deletions(-) delete mode 100644 spec/Body/CombinedMultipartSpec.php delete mode 100644 spec/Body/FilesSpec.php delete mode 100644 spec/Body/MultipartDataSpec.php delete mode 100644 spec/Body/UrlencodedDataSpec.php rename spec/{HttpMethodsSpec.php => HttpMethodsClientSpec.php} (67%) delete mode 100644 src/Body/CombinedMultipart.php delete mode 100644 src/Body/Files.php delete mode 100644 src/Body/Multipart.php delete mode 100644 src/Body/MultipartData.php delete mode 100644 src/Body/UrlencodedData.php delete mode 100644 src/BodyGenerator.php diff --git a/spec/BatchRequestSpec.php b/spec/BatchRequestSpec.php index 8a4d40f..4f11880 100644 --- a/spec/BatchRequestSpec.php +++ b/spec/BatchRequestSpec.php @@ -2,7 +2,7 @@ namespace spec\Http\Client\Utils; -use Http\Client\HttpPsrClient; +use Http\Client\HttpClient; use Http\Client\Utils\BatchRequest; use PhpSpec\ObjectBehavior; use Psr\Http\Message\RequestInterface; @@ -10,35 +10,35 @@ class BatchRequestSpec extends ObjectBehavior { - function let(HttpPsrClient $client) + function let(HttpClient $client) { $this->beAnInstanceOf('spec\Http\Client\Utils\BatchRequestStub', [$client]); } - function it_send_multiple_request_using_send_request(HttpPsrClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response1, ResponseInterface $response2) + function it_send_multiple_request_using_send_request(HttpClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response1, ResponseInterface $response2) { - $client->sendRequest($request1, [])->willReturn($response1); - $client->sendRequest($request2, [])->willReturn($response2); + $client->sendRequest($request1)->willReturn($response1); + $client->sendRequest($request2)->willReturn($response2); - $this->sendRequests([$request1, $request2], [])->shouldReturnAnInstanceOf('\Http\Client\BatchResult'); + $this->sendRequests([$request1, $request2])->shouldReturnAnInstanceOf('Http\Client\BatchResult'); } - function it_throw_batch_exception_if_one_or_more_request_failed(HttpPsrClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response) + function it_throw_batch_exception_if_one_or_more_request_failed(HttpClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response) { - $client->sendRequest($request1, [])->willReturn($response); - $client->sendRequest($request2, [])->willThrow('\Http\Client\Exception\HttpException'); + $client->sendRequest($request1)->willReturn($response); + $client->sendRequest($request2)->willThrow('Http\Client\Exception\HttpException'); - $this->shouldThrow('\Http\Client\Exception\BatchException')->duringSendRequests([$request1, $request2], []); + $this->shouldThrow('Http\Client\Exception\BatchException')->duringSendRequests([$request1, $request2]); } } -class BatchRequestStub implements HttpPsrClient +class BatchRequestStub implements HttpClient { use BatchRequest; protected $client; - public function __construct(HttpPsrClient $client) + public function __construct(HttpClient $client) { $this->client = $client; } @@ -46,8 +46,8 @@ public function __construct(HttpPsrClient $client) /** * {@inheritdoc} */ - public function sendRequest(RequestInterface $request, array $options = []) + public function sendRequest(RequestInterface $request) { - return $this->client->sendRequest($request, $options); + return $this->client->sendRequest($request); } } diff --git a/spec/BatchResultSpec.php b/spec/BatchResultSpec.php index 429541b..93f359a 100644 --- a/spec/BatchResultSpec.php +++ b/spec/BatchResultSpec.php @@ -1,7 +1,8 @@ shouldHaveType('Http\Client\BatchResult'); + $this->beAnInstanceOf('Http\Client\Utils\BatchResult'); + $this->shouldImplement('Http\Client\BatchResult'); } function it_is_immutable(RequestInterface $request, ResponseInterface $response) @@ -18,7 +20,7 @@ function it_is_immutable(RequestInterface $request, ResponseInterface $response) $new = $this->addResponse($request, $response); $this->getResponses()->shouldReturn([]); - $new->shouldHaveType('Http\Client\BatchResult'); + $new->shouldHaveType('Http\Client\Utils\BatchResult'); $new->getResponses()->shouldReturn([$response]); } @@ -36,9 +38,18 @@ function it_has_a_response_for_a_request(RequestInterface $request, ResponseInte { $new = $this->addResponse($request, $response); - $this->shouldThrow('Http\Client\Exception\UnexpectedValueException')->duringGetResponseFor($request); + $this->shouldThrow('UnexpectedValueException')->duringGetResponseFor($request); $this->hasResponseFor($request)->shouldReturn(false); $new->getResponseFor($request)->shouldReturn($response); $new->hasResponseFor($request)->shouldReturn(true); } + + function it_keeps_exception_after_add_request(RequestInterface $request1, Exception $exception, RequestInterface $request2, ResponseInterface $response) + { + $new = $this->addException($request1, $exception); + $new = $new->addResponse($request2, $response); + + $new->getResponseFor($request2)->shouldReturn($response); + $new->getExceptionFor($request1)->shouldReturn($exception); + } } diff --git a/spec/Body/CombinedMultipartSpec.php b/spec/Body/CombinedMultipartSpec.php deleted file mode 100644 index 194b497..0000000 --- a/spec/Body/CombinedMultipartSpec.php +++ /dev/null @@ -1,44 +0,0 @@ -file = tempnam(sys_get_temp_dir(), 'multipart'); - - $this->beConstructedWith(['data' => 1], ['file' => $this->file], 'boundary'); - } - - function it_is_initializable() - { - $this->shouldHaveType('Http\Client\Body\CombinedMultipart'); - } - - function it_is_body() - { - $this->shouldImplement('Http\Client\Body'); - } - - function it_is_multipart() - { - $this->shouldHaveType('Http\Client\Body\Multipart'); - } - - function it_has_content_header() - { - $this->getContentHeaders()->shouldReturn(['Content-Type' => 'multipart/form-data; boundary=boundary']); - } - - function it_is_streamable() - { - $body = sprintf("--boundary\r\nContent-Disposition: form-data; name=\"data\"\r\n\r\n1\r\n--boundary\r\nContent-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n\r\n\r\n", basename($this->file)); - - $this->toStreamable()->shouldReturn($body); - } -} diff --git a/spec/Body/FilesSpec.php b/spec/Body/FilesSpec.php deleted file mode 100644 index 0f5774c..0000000 --- a/spec/Body/FilesSpec.php +++ /dev/null @@ -1,44 +0,0 @@ -file = tempnam(sys_get_temp_dir(), 'multipart'); - - $this->beConstructedWith(['file' => $this->file], 'boundary'); - } - - function it_is_initializable() - { - $this->shouldHaveType('Http\Client\Body\Files'); - } - - function it_is_body() - { - $this->shouldImplement('Http\Client\Body'); - } - - function it_is_multipart() - { - $this->shouldHaveType('Http\Client\Body\Multipart'); - } - - function it_has_content_header() - { - $this->getContentHeaders()->shouldReturn(['Content-Type' => 'multipart/form-data; boundary=boundary']); - } - - function it_is_streamable() - { - $body = sprintf("--boundary\r\nContent-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n\r\n\r\n", basename($this->file)); - - $this->toStreamable()->shouldReturn($body); - } -} diff --git a/spec/Body/MultipartDataSpec.php b/spec/Body/MultipartDataSpec.php deleted file mode 100644 index 94265ab..0000000 --- a/spec/Body/MultipartDataSpec.php +++ /dev/null @@ -1,40 +0,0 @@ -beConstructedWith(['data' => 1], 'boundary'); - } - - function it_is_initializable() - { - $this->shouldHaveType('Http\Client\Body\MultipartData'); - } - - function it_is_body() - { - $this->shouldImplement('Http\Client\Body'); - } - - function it_is_multipart() - { - $this->shouldHaveType('Http\Client\Body\Multipart'); - } - - function it_has_content_header() - { - $this->getContentHeaders()->shouldReturn(['Content-Type' => 'multipart/form-data; boundary=boundary']); - } - - function it_is_streamable() - { - $body = "--boundary\r\nContent-Disposition: form-data; name=\"data\"\r\n\r\n1\r\n"; - - $this->toStreamable()->shouldReturn($body); - } -} diff --git a/spec/Body/UrlencodedDataSpec.php b/spec/Body/UrlencodedDataSpec.php deleted file mode 100644 index 5e4526b..0000000 --- a/spec/Body/UrlencodedDataSpec.php +++ /dev/null @@ -1,33 +0,0 @@ -beConstructedWith(['data1' => 1, 'data2' => 2]); - } - - function it_is_initializable() - { - $this->shouldHaveType('Http\Client\Body\UrlencodedData'); - } - - function it_is_body() - { - $this->shouldImplement('Http\Client\Body'); - } - - function it_has_content_header() - { - $this->getContentHeaders()->shouldReturn(['Content-Type' => 'application/x-www-form-urlencoded']); - } - - function it_is_streamable() - { - $this->toStreamable()->shouldReturn('data1=1&data2=2'); - } -} diff --git a/spec/HttpMethodsSpec.php b/spec/HttpMethodsClientSpec.php similarity index 67% rename from spec/HttpMethodsSpec.php rename to spec/HttpMethodsClientSpec.php index 08b5fc9..80e508a 100644 --- a/spec/HttpMethodsSpec.php +++ b/spec/HttpMethodsClientSpec.php @@ -2,70 +2,77 @@ namespace spec\Http\Client\Utils; +use Http\Client\HttpClient; use Http\Client\Utils\HttpMethodsClient; +use Http\Message\MessageFactory; use PhpSpec\ObjectBehavior; -class HttpMethodsSpec extends ObjectBehavior +class HttpMethodsClientSpec extends ObjectBehavior { - function let() + function let(HttpClient $client, MessageFactory $messageFactory) { - $this->beAnInstanceOf('spec\Http\Client\Utils\HttpMethodsClientStub'); + $this->beAnInstanceOf( + 'spec\Http\Client\Utils\HttpMethodsClientStub', [ + $client, + $messageFactory + ] + ); } function it_sends_a_get_request() { $data = HttpMethodsClientStub::$requestData; - $this->get($data['uri'], $data['headers'], $data['options'])->shouldReturn(true); + $this->get($data['uri'], $data['headers'])->shouldReturn(true); } function it_sends_a_head_request() { $data = HttpMethodsClientStub::$requestData; - $this->head($data['uri'], $data['headers'], $data['options'])->shouldReturn(true); + $this->head($data['uri'], $data['headers'])->shouldReturn(true); } function it_sends_a_trace_request() { $data = HttpMethodsClientStub::$requestData; - $this->trace($data['uri'], $data['headers'], $data['options'])->shouldReturn(true); + $this->trace($data['uri'], $data['headers'])->shouldReturn(true); } function it_sends_a_post_request() { $data = HttpMethodsClientStub::$requestData; - $this->post($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true); + $this->post($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); } function it_sends_a_put_request() { $data = HttpMethodsClientStub::$requestData; - $this->put($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true); + $this->put($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); } function it_sends_a_patch_request() { $data = HttpMethodsClientStub::$requestData; - $this->patch($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true); + $this->patch($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); } function it_sends_a_delete_request() { $data = HttpMethodsClientStub::$requestData; - $this->delete($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true); + $this->delete($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); } function it_sends_a_options_request() { $data = HttpMethodsClientStub::$requestData; - $this->options($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true); + $this->options($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); } } @@ -76,28 +83,23 @@ class HttpMethodsClientStub extends HttpMethodsClient 'headers' => [ 'Content-Type' => 'text/plain', ], - 'body' => 'body', - 'options' => [ - 'timeout' => 60, - ], + 'body' => 'body' ]; /** * {@inheritdoc} */ - protected function send($method, $uri, array $headers = [], $body = null, array $options = []) + protected function send($method, $uri, array $headers = [], $body = null) { if (in_array($method, ['GET', 'HEAD', 'TRACE'])) { return $uri === self::$requestData['uri'] && $headers === self::$requestData['headers'] && - is_null($body) && - $options === self::$requestData['options']; + is_null($body); } return in_array($method, ['POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']) && $uri === self::$requestData['uri'] && $headers === self::$requestData['headers'] && - $body === self::$requestData['body'] && - $options === self::$requestData['options']; + $body === self::$requestData['body']; } } diff --git a/src/BatchRequest.php b/src/BatchRequest.php index 86fc47d..e1f62fa 100644 --- a/src/BatchRequest.php +++ b/src/BatchRequest.php @@ -20,27 +20,26 @@ trait BatchRequest /** * {@inheritdoc} */ - abstract public function sendRequest(RequestInterface $request, array $options = []); + abstract public function sendRequest(RequestInterface $request); /** * {@inheritdoc} */ - public function sendRequests(array $requests, array $options = []) + public function sendRequests(array $requests) { - $batchResult = new BatchResult(); - $batchException = new BatchException(); - $batchException->setResult($batchResult); + $batchResult = new BatchResult(); foreach ($requests as $request) { try { - $batchResult->addResponse($request, $this->sendRequest($request, $options)); + $response = $this->sendRequest($request); + $batchResult = $batchResult->addResponse($request, $response); } catch (Exception $e) { - $batchException->addException($request, $e); + $batchResult = $batchResult->addException($request, $e); } } - if (count($batchException->getExceptions()) > 0) { - throw $batchException; + if ($batchResult->hasExceptions()) { + throw new BatchException($batchResult); } return $batchResult; diff --git a/src/BatchResult.php b/src/BatchResult.php index ccb510d..7e20b3c 100644 --- a/src/BatchResult.php +++ b/src/BatchResult.php @@ -32,9 +32,7 @@ public function __construct() } /** - * Returns all successful responses - * - * @return ResponseInterface[] + * {@inheritDoc} */ public function getResponses() { @@ -48,13 +46,7 @@ public function getResponses() } /** - * Returns a response of a request - * - * @param RequestInterface $request - * - * @return ResponseInterface - * - * @throws \UnexpectedValueException + * {@inheritDoc} */ public function getResponseFor(RequestInterface $request) { @@ -66,9 +58,7 @@ public function getResponseFor(RequestInterface $request) } /** - * Checks if there are any successful responses at all - * - * @return boolean + * {@inheritDoc} */ public function hasResponses() { @@ -76,11 +66,7 @@ public function hasResponses() } /** - * Checks if there is a response of a request - * - * @param RequestInterface $request - * - * @return ResponseInterface + * {@inheritDoc} */ public function hasResponseFor(RequestInterface $request) { @@ -88,14 +74,7 @@ public function hasResponseFor(RequestInterface $request) } /** - * Adds a response in an immutable way - * - * @param RequestInterface $request - * @param ResponseInterface $response - * - * @return BatchResult - * - * @internal + * {@inheritDoc} */ public function addResponse(RequestInterface $request, ResponseInterface $response) { @@ -106,11 +85,7 @@ public function addResponse(RequestInterface $request, ResponseInterface $respon } /** - * Checks if a request is successful - * - * @param RequestInterface $request - * - * @return boolean + * {@inheritDoc} */ public function isSuccessful(RequestInterface $request) { @@ -118,11 +93,7 @@ public function isSuccessful(RequestInterface $request) } /** - * Checks if a request is failed - * - * @param RequestInterface $request - * - * @return boolean + * {@inheritDoc} */ public function isFailed(RequestInterface $request) { @@ -130,9 +101,7 @@ public function isFailed(RequestInterface $request) } /** - * Returns all exceptions - * - * @return Exception[] + * {@inheritDoc} */ public function getExceptions() { @@ -146,13 +115,7 @@ public function getExceptions() } /** - * Returns an exception for a request - * - * @param RequestInterface $request - * - * @return Exception - * - * @throws UnexpectedValueException + * {@inheritDoc} */ public function getExceptionFor(RequestInterface $request) { @@ -164,9 +127,7 @@ public function getExceptionFor(RequestInterface $request) } /** - * Checks if there are any exceptions at all - * - * @return boolean + * {@inheritDoc} */ public function hasExceptions() { @@ -174,11 +135,7 @@ public function hasExceptions() } /** - * Checks if there is an exception for a request - * - * @param RequestInterface $request - * - * @return boolean + * {@inheritDoc} */ public function hasExceptionFor(RequestInterface $request) { @@ -186,18 +143,19 @@ public function hasExceptionFor(RequestInterface $request) } /** - * Adds an exception - * - * @param RequestInterface $request - * @param Exception $exception + * {@inheritDoc} */ public function addException(RequestInterface $request, Exception $exception) { - $this->exceptions->attach($request, $exception); + $new = clone $this; + $new->exceptions->attach($request, $exception); + + return $new; } public function __clone() { $this->responses = clone $this->responses; + $this->exceptions = clone $this->exceptions; } } diff --git a/src/Body/CombinedMultipart.php b/src/Body/CombinedMultipart.php deleted file mode 100644 index f032bec..0000000 --- a/src/Body/CombinedMultipart.php +++ /dev/null @@ -1,45 +0,0 @@ - - */ -class CombinedMultipart extends Multipart -{ - /** - * @var MultipartData - */ - protected $data; - - /** - * @var Files - */ - protected $files; - - /** - * @param array $data - * @param array $files - * @param string|null $boundary - */ - public function __construct(array $data, array $files, $boundary = null) - { - parent::__construct($boundary); - - $this->data = new MultipartData($data, $this->boundary); - $this->files = new Files($files, $this->boundary); - } - - /** - * {@inheritdoc} - */ - public function toStreamable() - { - $body = $this->data->toStreamable(); - $body .= $this->files->toStreamable(); - - return $body; - } -} diff --git a/src/Body/Files.php b/src/Body/Files.php deleted file mode 100644 index ed9be1c..0000000 --- a/src/Body/Files.php +++ /dev/null @@ -1,53 +0,0 @@ - - */ -class Files extends Multipart -{ - /** - * @var array - */ - protected $files; - - /** - * @param array $files - * @param string|null $boundary - */ - public function __construct(array $files, $boundary = null) - { - $this->files = $files; - - parent::__construct($boundary); - } - - /** - * {@inheritdoc} - */ - public function toStreamable() - { - $body = ''; - - foreach ($this->files as $name => $file) { - if (!is_file($file)) { - throw new RuntimeException(sprintf('File "%s" does not exist', $file)); - } - - $body .= sprintf( - "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n\r\n%s\r\n", - $this->boundary, - $name, - basename($file), - file_get_contents($file) - ); - } - - return $body; - } -} diff --git a/src/Body/Multipart.php b/src/Body/Multipart.php deleted file mode 100644 index 62170ae..0000000 --- a/src/Body/Multipart.php +++ /dev/null @@ -1,40 +0,0 @@ - - */ -abstract class Multipart implements Body -{ - /** - * @var string - */ - protected $boundary; - - /** - * @param string|null $boundary - */ - public function __construct($boundary = null) - { - if (is_null($boundary)) { - $boundary = sha1(microtime()); - } - - $this->boundary = $boundary; - } - - /** - * {@inheritdoc} - */ - public function getContentHeaders() - { - return [ - 'Content-Type' => 'multipart/form-data; boundary='.$this->boundary, - ]; - } -} diff --git a/src/Body/MultipartData.php b/src/Body/MultipartData.php deleted file mode 100644 index b6d4f6a..0000000 --- a/src/Body/MultipartData.php +++ /dev/null @@ -1,67 +0,0 @@ - - */ -class MultipartData extends Multipart -{ - /** - * @var array - */ - protected $data; - - /** - * @param array $data - * @param string|null $boundary - */ - public function __construct(array $data, $boundary = null) - { - $this->data = $data; - - parent::__construct($boundary); - } - - /** - * {@inheritdoc} - */ - public function toStreamable() - { - return $this->prepareData(null, $this->data); - } - - /** - * @param string|integer|null $name - * @param mixed $data - * - * @return string - */ - protected function prepareData($name, $data) - { - $body = ''; - - if (is_array($data)) { - foreach ($data as $subName => $subData) { - if (!is_null($name)) { - $subName = sprintf('%s[%s]', $name, $subName); - } - - $body .= $this->prepareData($subName, $subData); - } - - return $body; - } - - $body .= sprintf( - "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n", - $this->boundary, - $name, - $data - ); - - return $body; - } -} diff --git a/src/Body/UrlencodedData.php b/src/Body/UrlencodedData.php deleted file mode 100644 index ea90e57..0000000 --- a/src/Body/UrlencodedData.php +++ /dev/null @@ -1,42 +0,0 @@ - - */ -class UrlencodedData implements Body -{ - /** - * @var array - */ - protected $data; - - /** - * @param array $data - */ - public function __construct(array $data) - { - $this->data = $data; - } - - /** - * {@inheritdoc} - */ - public function getContentHeaders() - { - return [ - 'Content-Type' => 'application/x-www-form-urlencoded', - ]; - } - - /** - * {@inheritdoc} - */ - public function toStreamable() - { - return http_build_query($this->data, null, '&'); - } -} diff --git a/src/BodyGenerator.php b/src/BodyGenerator.php deleted file mode 100644 index 7baaac1..0000000 --- a/src/BodyGenerator.php +++ /dev/null @@ -1,30 +0,0 @@ - - */ -interface BodyGenerator -{ - /** - * Returns a set of headers which is needed to correctly send the body - * - * Content-Length is calculated automatically if possible - * - * @return array - */ - public function getHeaders(); - - /** - * Convert data to a format which can be used to create a proper PSR-7 Stream - * - * @return string|StreamInterface - */ - public function getContent(); -}