-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathClientSpec.php
162 lines (130 loc) · 4.67 KB
/
ClientSpec.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace spec\Http\Mock;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Message\RequestMatcher;
use Http\Message\ResponseFactory;
use Http\Mock\Client;
use Http\Client\Exception as ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;
class ClientSpec extends ObjectBehavior
{
function let(ResponseFactory $responseFactory)
{
$this->beConstructedWith($responseFactory);
}
function it_is_initializable()
{
$this->shouldHaveType(Client::class);
}
function it_is_an_http_client()
{
$this->shouldImplement(HttpClient::class);
}
function it_is_an_async_http_client()
{
$this->shouldImplement(HttpAsyncClient::class);
}
function it_returns_a_response_for_a_request(RequestInterface $request, ResponseInterface $response)
{
$this->addResponse($response);
$this->sendRequest($request)->shouldReturn($response);
}
function it_returns_the_default_response_for_a_request(RequestInterface $request, ResponseInterface $response)
{
$this->setDefaultResponse($response);
$this->sendRequest($request)->shouldReturn($response);
}
function it_throws_an_exception_for_a_request(RequestInterface $request)
{
$this->addException(new Exception());
$this->shouldThrow(Exception::class)->duringSendRequest($request);
}
function it_throws_the_default_exception_for_a_request(RequestInterface $request)
{
$this->setDefaultException(new Exception());
$this->shouldThrow(Exception::class)->duringSendRequest($request);
}
function it_creates_an_empty_response_when_none_is_added(
RequestInterface $request,
ResponseFactory $responseFactory,
ResponseInterface $response
) {
$responseFactory->createResponse()->willReturn($response);
$this->sendRequest($request)->shouldReturn($response);
}
function it_returns_the_last_request(RequestInterface $request, ResponseInterface $response)
{
// we need to set something that sendRequest can return.
$this->addResponse($response);
$this->sendRequest($request);
$this->getLastRequest()->shouldReturn($request);
}
function it_returns_null_when_there_is_no_last_request()
{
$this->getLastRequest()->shouldReturn(null);
}
function it_reset(
ResponseFactory $responseFactory,
RequestInterface $request,
ResponseInterface $response,
ResponseInterface $newResponse
) {
$this->addResponse($response);
$this->setDefaultResponse($response);
$this->addException(new Exception());
$this->setDefaultException(new Exception());
$responseFactory->createResponse()->willReturn($newResponse);
$this->reset();
$this->sendRequest($request)->shouldReturn($newResponse);
$this->reset();
$this->getRequests()->shouldReturn([]);
}
function it_returns_response_if_request_matcher_matches(
RequestMatcher $matcher,
RequestInterface $request,
ResponseInterface $response
) {
$matcher->matches($request)->willReturn(true);
$this->on($matcher, $response);
$this->sendRequest($request)->shouldReturn($response);
}
function it_throws_exception_if_request_matcher_matches(
RequestMatcher $matcher,
RequestInterface $request
) {
$matcher->matches($request)->willReturn(true);
$this->on($matcher, new Exception());
$this->shouldThrow(Exception::class)->duringSendRequest($request);
}
function it_skips_conditional_response_if_matcher_returns_false(
RequestMatcher $matcher,
RequestInterface $request,
ResponseInterface $expectedResponse,
ResponseInterface $skippedResponse
) {
$matcher->matches($request)->willReturn(false);
$this->on($matcher, $skippedResponse);
$this->addResponse($expectedResponse);
$this->sendRequest($request)->shouldReturn($expectedResponse);
}
function it_calls_callable_with_request_as_argument_when_matcher_returns_true(
RequestMatcher $matcher,
RequestInterface $request,
ResponseInterface $response
) {
$matcher->matches($request)->willReturn(true);
$this->on(
$matcher,
function(RequestInterface $request) use ($response) {
return $response->getWrappedObject();
}
);
$this->sendRequest($request)->shouldReturn($response);
}
}
class Exception extends \Exception implements ClientExceptionInterface
{
}