Skip to content
This repository was archived by the owner on Jan 16, 2018. It is now read-only.

adjust BatchResult to interface cleanup #12

Merged
merged 1 commit into from
Oct 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions spec/BatchResultSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,21 @@ 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)
{
$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);
}
}
69 changes: 26 additions & 43 deletions src/BatchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,6 +30,14 @@ public function __construct()
$this->exceptions = new \SplObjectStorage();
}

/**
* {@inheritDoc}
*/
public function hasResponses()
{
return $this->responses->count() > 0;
}

/**
* {@inheritDoc}
*/
Expand All @@ -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);
}
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);
}
}

/**
Expand Down