From 1c6d0e17a9593b79a33edb676a0b37f3b853dba7 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Fri, 23 Oct 2015 17:21:36 +0200 Subject: [PATCH] adjust BatchResult to interface cleanup --- spec/BatchResultSpec.php | 8 +++-- src/BatchResult.php | 69 +++++++++++++++------------------------- 2 files changed, 32 insertions(+), 45 deletions(-) diff --git a/spec/BatchResultSpec.php b/spec/BatchResultSpec.php index 93f359a..84df31d 100644 --- a/spec/BatchResultSpec.php +++ b/spec/BatchResultSpec.php @@ -39,9 +39,9 @@ function it_has_a_response_for_a_request(RequestInterface $request, ResponseInte $new = $this->addResponse($request, $response); $this->shouldThrow('UnexpectedValueException')->duringGetResponseFor($request); - $this->hasResponseFor($request)->shouldReturn(false); + $this->isSuccessful($request)->shouldReturn(false); $new->getResponseFor($request)->shouldReturn($response); - $new->hasResponseFor($request)->shouldReturn(true); + $new->isSuccessful($request)->shouldReturn(true); } function it_keeps_exception_after_add_request(RequestInterface $request1, Exception $exception, RequestInterface $request2, ResponseInterface $response) @@ -49,7 +49,11 @@ function it_keeps_exception_after_add_request(RequestInterface $request1, Except $new = $this->addException($request1, $exception); $new = $new->addResponse($request2, $response); + $new->isSuccessful($request2)->shouldReturn(true); + $new->isFailed($request2)->shouldReturn(false); $new->getResponseFor($request2)->shouldReturn($response); + $new->isSuccessful($request1)->shouldReturn(false); + $new->isFailed($request1)->shouldReturn(true); $new->getExceptionFor($request1)->shouldReturn($exception); } } diff --git a/src/BatchResult.php b/src/BatchResult.php index 7e20b3c..ac11663 100644 --- a/src/BatchResult.php +++ b/src/BatchResult.php @@ -6,7 +6,6 @@ use Http\Client\BatchResult as BatchResultInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -use UnexpectedValueException; /** * Responses and exceptions returned from parallel request execution @@ -31,6 +30,14 @@ public function __construct() $this->exceptions = new \SplObjectStorage(); } + /** + * {@inheritDoc} + */ + public function hasResponses() + { + return $this->responses->count() > 0; + } + /** * {@inheritDoc} */ @@ -48,29 +55,21 @@ public function getResponses() /** * {@inheritDoc} */ - public function getResponseFor(RequestInterface $request) - { - try { - return $this->responses[$request]; - } catch (\UnexpectedValueException $e) { - throw new \UnexpectedValueException('Request not found', $e->getCode(), $e); - } - } - - /** - * {@inheritDoc} - */ - public function hasResponses() + public function isSuccessful(RequestInterface $request) { - return $this->responses->count() > 0; + return $this->responses->contains($request); } /** * {@inheritDoc} */ - public function hasResponseFor(RequestInterface $request) + public function getResponseFor(RequestInterface $request) { - return $this->responses->contains($request); + try { + return $this->responses[$request]; + } catch (\UnexpectedValueException $e) { + throw new \UnexpectedValueException('Request not found', $e->getCode(), $e); + } } /** @@ -87,17 +86,9 @@ public function addResponse(RequestInterface $request, ResponseInterface $respon /** * {@inheritDoc} */ - public function isSuccessful(RequestInterface $request) - { - return $this->responses->contains($request); - } - - /** - * {@inheritDoc} - */ - public function isFailed(RequestInterface $request) + public function hasExceptions() { - return $this->exceptions->contains($request); + return $this->exceptions->count() > 0; } /** @@ -117,29 +108,21 @@ public function getExceptions() /** * {@inheritDoc} */ - public function getExceptionFor(RequestInterface $request) - { - try { - return $this->exceptions[$request]; - } catch (\UnexpectedValueException $e) { - throw new UnexpectedValueException('Request not found', $e->getCode(), $e); - } - } - - /** - * {@inheritDoc} - */ - public function hasExceptions() + public function isFailed(RequestInterface $request) { - return $this->exceptions->count() > 0; + return $this->exceptions->contains($request); } /** * {@inheritDoc} */ - public function hasExceptionFor(RequestInterface $request) + public function getExceptionFor(RequestInterface $request) { - return $this->exceptions->contains($request); + try { + return $this->exceptions[$request]; + } catch (\UnexpectedValueException $e) { + throw new \UnexpectedValueException('Request not found', $e->getCode(), $e); + } } /**