1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Scriptotek\Alma; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Response; |
6
|
|
|
use Http\Client\Common\Exception\ClientErrorException; |
7
|
|
|
use Http\Mock\Client as MockHttp; |
8
|
|
|
use PhpSpec\ObjectBehavior; |
9
|
|
|
use Scriptotek\Alma\Exception\InvalidApiKey; |
10
|
|
|
use Scriptotek\Alma\Exception\RequestFailed; |
11
|
|
|
use Scriptotek\Alma\Exception\ResourceNotFound; |
12
|
|
|
use Scriptotek\Alma\Zones; |
13
|
|
|
|
14
|
|
|
function str_random() |
15
|
|
|
{ |
16
|
|
|
return strval(rand(10000, 99999)); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
class ClientSpec extends ObjectBehavior |
20
|
|
|
{ |
21
|
|
|
public function let() |
22
|
|
|
{ |
23
|
|
|
$http = new MockHttp(); |
24
|
|
|
$apiKey = 'DummyApiKey'; |
25
|
|
|
$region = 'eu'; |
26
|
|
|
$this->beConstructedWith($apiKey, $region, Zones::INSTITUTION, $http); |
27
|
|
|
|
28
|
|
|
return $http; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function httpWithResponseBody($body, $statusCode = 200, $headers = []) |
32
|
|
|
{ |
33
|
|
|
$http = $this->let(); |
34
|
|
|
$http->addResponse(new Response($statusCode, $headers, $body)); |
35
|
|
|
|
36
|
|
|
return $http; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function it_is_initializable() |
40
|
|
|
{ |
41
|
|
|
$this->shouldHaveType('Scriptotek\Alma\Client'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function it_only_accepts_valid_regions() |
45
|
|
|
{ |
46
|
|
|
$this->shouldThrow('Scriptotek\Alma\Exception\ClientException') |
47
|
|
|
->duringSetRegion('ux'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function it_can_make_GET_requests() |
51
|
|
|
{ |
52
|
|
|
$responseBody = str_random(); |
53
|
|
|
$this->httpWithResponseBody($responseBody); |
54
|
|
|
$this->get(str_random())->shouldBe($responseBody); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function it_sends_an_API_key_with_each_request() |
58
|
|
|
{ |
59
|
|
|
$http = $this->httpWithResponseBody(str_random()); |
60
|
|
|
$this->getJSON(str_random()); |
61
|
|
|
|
62
|
|
|
// Query string should include apikey |
63
|
|
|
expect($http->getRequests()[0])->getUri()->getQuery()->toContain('apikey=DummyApiKey'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function it_can_request_and_parse_JSON() |
67
|
|
|
{ |
68
|
|
|
$responseBody = json_encode(['some_key' => 'some_value']); |
69
|
|
|
$http = $this->httpWithResponseBody($responseBody); |
70
|
|
|
|
71
|
|
|
$this->getJSON(str_random())->some_key->shouldBe('some_value'); |
72
|
|
|
|
73
|
|
|
$request = $http->getRequests()[0]; |
74
|
|
|
expect($request->getHeader('Accept')[0])->toBe('application/json'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function it_can_request_and_parse_XML() |
78
|
|
|
{ |
79
|
|
|
$responseBody = "<?xml version=\"1.0\"?>\n<some_key>some_value</some_key>\n"; |
80
|
|
|
$http = $this->httpWithResponseBody($responseBody); |
81
|
|
|
|
82
|
|
|
$xml = $this->getXML(str_random()); |
83
|
|
|
|
84
|
|
|
// Request headers should include Accept: application/xml |
85
|
|
|
expect($http->getRequests()[0])->getHeader('Accept')[0]->toBe('application/xml'); |
86
|
|
|
|
87
|
|
|
// Response should be of type QuiteSimpleXMLElement |
88
|
|
|
$xml->shouldHaveType('Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement'); |
89
|
|
|
|
90
|
|
|
// and have the expected value |
91
|
|
|
$xml->asXML()->shouldBe($responseBody); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function it_can_make_PUT_requests() |
95
|
|
|
{ |
96
|
|
|
$responseBody = str_random(); |
97
|
|
|
$http = $this->httpWithResponseBody($responseBody); |
98
|
|
|
|
99
|
|
|
$this->put(str_random(), str_random(), 'application/json')->shouldBe(true); |
100
|
|
|
|
101
|
|
|
expect($http->getRequests())->toHaveCount(1); |
102
|
|
|
expect($http->getRequests()[0])->getMethod()->toBe('PUT'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function it_can_get_redirect_locations() |
106
|
|
|
{ |
107
|
|
|
$http = $this->let(); |
108
|
|
|
$response = new Response(); |
109
|
|
|
$response = $response->withHeader('Location', 'http://test.test'); |
110
|
|
|
$http->addResponse($response); |
111
|
|
|
|
112
|
|
|
$this->getRedirectLocation('/')->shouldBe('http://test.test'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
public function it_processes_json_error_responses(ClientErrorException $exception) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$exception->getResponse()->willReturn(new Response( |
118
|
|
|
400, |
119
|
|
|
['Content-Type' => 'application/json;charset=utf-8'], |
120
|
|
|
SpecHelper::getDummyData('error_response.json', false) |
121
|
|
|
)); |
122
|
|
|
|
123
|
|
|
$http = $this->let(); |
124
|
|
|
$http->addException($exception->getWrappedObject()); |
125
|
|
|
|
126
|
|
|
$this->shouldThrow(new RequestFailed( |
127
|
|
|
'Mandatory field is missing: library', |
128
|
|
|
'401664' |
129
|
|
|
))->during('getJSON', ['/items/123']); |
130
|
|
|
|
131
|
|
|
expect($http->getRequests())->toHaveCount(1); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
public function it_processes_xml_error_responses(ClientErrorException $exception) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$exception->getResponse()->willReturn(new Response( |
137
|
|
|
400, |
138
|
|
|
['Content-Type' => 'application/xml;charset=utf-8'], |
139
|
|
|
SpecHelper::getDummyData('error_response.xml', false) |
140
|
|
|
)); |
141
|
|
|
|
142
|
|
|
$http = $this->let(); |
143
|
|
|
$http->addException($exception->getWrappedObject()); |
144
|
|
|
|
145
|
|
|
$this->shouldThrow(new RequestFailed( |
146
|
|
|
'Mandatory field is missing: library', |
147
|
|
|
'401664' |
148
|
|
|
))->during('getXML', ['/items/123']); |
149
|
|
|
|
150
|
|
|
expect($http->getRequests())->toHaveCount(1); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
View Code Duplication |
public function it_can_throw_resource_not_found(ClientErrorException $exception) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$exception->getResponse()->willReturn(new Response( |
156
|
|
|
400, |
157
|
|
|
['Content-Type' => 'application/json;charset=utf-8'], |
158
|
|
|
SpecHelper::getDummyData('item_barcode_error_response.json', false) |
159
|
|
|
)); |
160
|
|
|
|
161
|
|
|
$http = $this->let(); |
162
|
|
|
$http->addException($exception->getWrappedObject()); |
163
|
|
|
|
164
|
|
|
$this->shouldThrow(new ResourceNotFound('No items found for barcode 123.', '401689')) |
165
|
|
|
->during('getJSON', ['/items/123']); |
166
|
|
|
|
167
|
|
|
expect($http->getRequests())->toHaveCount(1); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
View Code Duplication |
public function it_can_throw_invalid_api_key(ClientErrorException $exception) |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
$exception->getResponse()->willReturn(new Response( |
173
|
|
|
400, |
174
|
|
|
['Content-Type' => 'text/plain;charset=UTF-8'], |
175
|
|
|
'Invalid API Key' |
176
|
|
|
)); |
177
|
|
|
|
178
|
|
|
$http = $this->let(); |
179
|
|
|
$http->addException($exception->getWrappedObject()); |
180
|
|
|
|
181
|
|
|
$this->shouldThrow(new InvalidApiKey('Invalid API Key', 0)) |
182
|
|
|
->during('getJSON', ['/items/123']); |
183
|
|
|
|
184
|
|
|
expect($http->getRequests())->toHaveCount(1); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function it_will_retry_when_reaching_rate_limit(ClientErrorException $exception) |
188
|
|
|
{ |
189
|
|
|
$exception->getResponse()->willReturn(new Response( |
190
|
|
|
400, |
191
|
|
|
['Content-Type' => 'application/json;charset=utf-8'], |
192
|
|
|
SpecHelper::getDummyData('per_second_threshold_error_response.json', false) |
193
|
|
|
)); |
194
|
|
|
|
195
|
|
|
$http = $this->let(); |
196
|
|
|
$http->addException($exception->getWrappedObject()); |
197
|
|
|
|
198
|
|
|
$this->getJSON('/items/123'); |
199
|
|
|
|
200
|
|
|
expect($http->getRequests())->toHaveCount(2); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.