Skip to content

Commit 2e16453

Browse files
committed
Merge pull request #48 from php-http/batch_response
Add BatchResult
2 parents 67baf37 + 44e8c01 commit 2e16453

File tree

7 files changed

+339
-40
lines changed

7 files changed

+339
-40
lines changed

spec/BatchResultSpec.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace spec\Http\Client;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class BatchResultSpec extends ObjectBehavior
10+
{
11+
function it_is_initializable()
12+
{
13+
$this->shouldHaveType('Http\Client\BatchResult');
14+
}
15+
16+
function it_is_immutable(RequestInterface $request, ResponseInterface $response)
17+
{
18+
$new = $this->addResponse($request, $response);
19+
20+
$this->getResponses()->shouldReturn([]);
21+
$new->shouldHaveType('Http\Client\BatchResult');
22+
$new->getResponses()->shouldReturn([$response]);
23+
}
24+
25+
function it_has_a_responses(RequestInterface $request, ResponseInterface $response)
26+
{
27+
$new = $this->addResponse($request, $response);
28+
29+
$this->hasResponses()->shouldReturn(false);
30+
$this->getResponses()->shouldReturn([]);
31+
$new->hasResponses()->shouldReturn(true);
32+
$new->getResponses()->shouldReturn([$response]);
33+
}
34+
35+
function it_has_a_response_for_a_request(RequestInterface $request, ResponseInterface $response)
36+
{
37+
$new = $this->addResponse($request, $response);
38+
39+
$this->shouldThrow('Http\Client\Exception\UnexpectedValueException')->duringGetResponseFor($request);
40+
$this->hasResponseFor($request)->shouldReturn(false);
41+
$new->getResponseFor($request)->shouldReturn($response);
42+
$new->hasResponseFor($request)->shouldReturn(true);
43+
}
44+
}

spec/Exception/BatchExceptionSpec.php

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,64 @@
22

33
namespace spec\Http\Client\Exception;
44

5-
use Http\Client\Exception\TransferException;
6-
use Psr\Http\Message\ResponseInterface;
5+
use Http\Client\BatchResult;
6+
use Http\Client\Exception;
7+
use Psr\Http\Message\RequestInterface;
78
use PhpSpec\ObjectBehavior;
89

910
class BatchExceptionSpec extends ObjectBehavior
1011
{
11-
function let(TransferException $e, ResponseInterface $response)
12+
function it_is_initializable()
1213
{
13-
$this->beConstructedWith([$e], [$response]);
14+
$this->shouldHaveType('Http\Client\Exception\BatchException');
1415
}
1516

16-
function it_is_initializable()
17+
function it_is_an_exception()
1718
{
18-
$this->shouldHaveType('Http\Client\Exception\BatchException');
19+
$this->shouldImplement('Http\Client\Exception');
20+
$this->shouldHaveType('Exception');
21+
}
22+
23+
function it_has_a_result()
24+
{
25+
$this->setResult($result = new BatchResult());
26+
$this->getResult()->shouldReturn($result);
27+
}
28+
29+
function it_throws_an_exception_if_the_result_is_already_set()
30+
{
31+
$this->getResult()->shouldHaveType('Http\Client\BatchResult');
32+
$this->shouldThrow('Http\Client\Exception\InvalidArgumentException')->duringSetResult(new BatchResult());
1933
}
2034

21-
function it_is_a_transfer_exception()
35+
function it_has_an_exception_for_a_request(RequestInterface $request, Exception $exception)
2236
{
23-
$this->shouldHaveType('Http\Client\Exception\TransferException');
37+
$this->shouldThrow('Http\Client\Exception\UnexpectedValueException')->duringGetExceptionFor($request);
38+
$this->hasExceptionFor($request)->shouldReturn(false);
39+
40+
$this->addException($request, $exception);
41+
42+
$this->getExceptionFor($request)->shouldReturn($exception);
43+
$this->hasExceptionFor($request)->shouldReturn(true);
2444
}
2545

26-
function it_has_exceptions(TransferException $e, TransferException $e2)
46+
function it_has_exceptions(RequestInterface $request, Exception $exception)
2747
{
28-
$this->getExceptions()->shouldReturn([$e]);
29-
$this->hasException($e)->shouldReturn(true);
30-
$this->hasException($e2)->shouldReturn(false);
31-
$this->hasExceptions()->shouldReturn(true);
48+
$this->getExceptions()->shouldReturn([]);
49+
50+
$this->addException($request, $exception);
51+
52+
$this->getExceptions()->shouldReturn([$exception]);
3253
}
3354

34-
function it_has_responses(ResponseInterface $response, ResponseInterface $response2)
55+
function it_checks_if_a_request_failed(RequestInterface $request, Exception $exception)
3556
{
36-
$this->getResponses()->shouldReturn([$response]);
37-
$this->hasResponse($response)->shouldReturn(true);
38-
$this->hasResponse($response2)->shouldReturn(false);
39-
$this->hasResponses()->shouldReturn(true);
57+
$this->isSuccessful($request)->shouldReturn(false);
58+
$this->isFailed($request)->shouldReturn(false);
59+
60+
$this->addException($request, $exception);
61+
62+
$this->isSuccessful($request)->shouldReturn(false);
63+
$this->isFailed($request)->shouldReturn(true);
4064
}
4165
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use PhpSpec\ObjectBehavior;
6+
7+
class UnexpectedValueExceptionSpec extends ObjectBehavior
8+
{
9+
function it_is_initializable()
10+
{
11+
$this->shouldHaveType('Http\Client\Exception\UnexpectedValueException');
12+
}
13+
14+
function it_is_invalid_argument_exception()
15+
{
16+
$this->shouldHaveType('UnexpectedValueException');
17+
}
18+
19+
function it_is_exception()
20+
{
21+
$this->shouldImplement('Http\Client\Exception');
22+
}
23+
}

src/BatchResult.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace Http\Client;
4+
5+
use Http\Client\Exception\UnexpectedValueException;
6+
use Psr\Http\Message\RequestInterface;
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
/**
10+
* Successful responses returned from parallel request execution
11+
*
12+
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
13+
*/
14+
final class BatchResult
15+
{
16+
/**
17+
* @var \SplObjectStorage
18+
*/
19+
private $responses;
20+
21+
public function __construct()
22+
{
23+
$this->responses = new \SplObjectStorage();
24+
}
25+
26+
/**
27+
* Returns all successful responses
28+
*
29+
* @return ResponseInterface[]
30+
*/
31+
public function getResponses()
32+
{
33+
$responses = [];
34+
35+
foreach ($this->responses as $request) {
36+
$responses[] = $this->responses[$request];
37+
}
38+
39+
return $responses;
40+
}
41+
42+
/**
43+
* Returns a response of a request
44+
*
45+
* @param RequestInterface $request
46+
*
47+
* @return ResponseInterface
48+
*
49+
* @throws UnexpectedValueException
50+
*/
51+
public function getResponseFor(RequestInterface $request)
52+
{
53+
try {
54+
return $this->responses[$request];
55+
} catch (\UnexpectedValueException $e) {
56+
throw new UnexpectedValueException('Request not found', $e->getCode(), $e);
57+
}
58+
}
59+
60+
/**
61+
* Checks if there are any successful responses at all
62+
*
63+
* @return boolean
64+
*/
65+
public function hasResponses()
66+
{
67+
return $this->responses->count() > 0;
68+
}
69+
70+
/**
71+
* Checks if there is a response of a request
72+
*
73+
* @param RequestInterface $request
74+
*
75+
* @return ResponseInterface
76+
*/
77+
public function hasResponseFor(RequestInterface $request)
78+
{
79+
return $this->responses->contains($request);
80+
}
81+
82+
/**
83+
* Adds a response in an immutable way
84+
*
85+
* @param RequestInterface $request
86+
* @param ResponseInterface $response
87+
*
88+
* @return BatchResult
89+
*
90+
* @internal
91+
*/
92+
public function addResponse(RequestInterface $request, ResponseInterface $response)
93+
{
94+
$new = clone $this;
95+
$new->responses->attach($request, $response);
96+
97+
return $new;
98+
}
99+
100+
public function __clone()
101+
{
102+
$this->responses = clone $this->responses;
103+
}
104+
}

0 commit comments

Comments
 (0)