1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Scriptotek\Alma; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Response; |
6
|
|
|
use Http\Client\Common\Exception\ServerErrorException; |
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_post_and_parse_JSON() |
78
|
|
|
{ |
79
|
|
|
$http = $this->httpWithResponseBody(SpecHelper::getDummyData('create_loan_response.json', false)); |
80
|
|
|
|
81
|
|
|
$this->postJSON(str_random())->loan_id->shouldBe('7329587120002204'); |
82
|
|
|
|
83
|
|
|
$request = $http->getRequests()[0]; |
84
|
|
|
expect($request->getHeader('Content-Type')[0])->toBe('application/json'); |
85
|
|
|
expect($request->getHeader('Accept')[0])->toBe('application/json'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function it_can_request_and_parse_XML() |
89
|
|
|
{ |
90
|
|
|
$responseBody = "<?xml version=\"1.0\"?>\n<some_key>some_value</some_key>\n"; |
91
|
|
|
$http = $this->httpWithResponseBody($responseBody); |
92
|
|
|
|
93
|
|
|
$xml = $this->getXML(str_random()); |
94
|
|
|
|
95
|
|
|
// Request headers should include Accept: application/xml |
96
|
|
|
expect($http->getRequests()[0])->getHeader('Accept')[0]->toBe('application/xml'); |
97
|
|
|
|
98
|
|
|
// Response should be of type QuiteSimpleXMLElement |
99
|
|
|
$xml->shouldHaveType('Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement'); |
100
|
|
|
|
101
|
|
|
// and have the expected value |
102
|
|
|
$xml->asXML()->shouldBe($responseBody); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function it_can_make_PUT_requests() |
106
|
|
|
{ |
107
|
|
|
$responseBody = str_random(); |
108
|
|
|
$http = $this->httpWithResponseBody($responseBody); |
109
|
|
|
|
110
|
|
|
$this->put(str_random(), str_random(), 'application/json')->shouldBe($responseBody); |
111
|
|
|
|
112
|
|
|
expect($http->getRequests())->toHaveCount(1); |
113
|
|
|
expect($http->getRequests()[0])->getMethod()->toBe('PUT'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function it_can_get_redirect_locations() |
117
|
|
|
{ |
118
|
|
|
$http = $this->let(); |
119
|
|
|
$response = new Response(); |
120
|
|
|
$response = $response->withHeader('Location', 'http://test.test'); |
121
|
|
|
$http->addResponse($response); |
122
|
|
|
|
123
|
|
|
$this->getRedirectLocation('/')->shouldBe('http://test.test'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function it_processes_json_error_responses() |
127
|
|
|
{ |
128
|
|
|
$http = $this->let(); |
129
|
|
|
|
130
|
|
|
$exception = SpecHelper::makeExceptionResponse( |
131
|
|
|
SpecHelper::getDummyData('error_response.json', false) |
132
|
|
|
); |
133
|
|
|
$http->addException($exception); |
134
|
|
|
|
135
|
|
|
$this->shouldThrow(new RequestFailed( |
136
|
|
|
'Mandatory field is missing: library', |
137
|
|
|
'401664' |
138
|
|
|
))->during('getJSON', ['/items/123']); |
139
|
|
|
|
140
|
|
|
expect($http->getRequests())->toHaveCount(1); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
View Code Duplication |
public function it_processes_xml_error_responses() |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
$http = $this->let(); |
146
|
|
|
|
147
|
|
|
$exception = SpecHelper::makeExceptionResponse( |
148
|
|
|
SpecHelper::getDummyData('error_response.xml', false), |
149
|
|
|
400, |
150
|
|
|
'application/xml;charset=utf-8' |
151
|
|
|
); |
152
|
|
|
$http->addException($exception); |
153
|
|
|
|
154
|
|
|
$this->shouldThrow(new RequestFailed( |
155
|
|
|
'Mandatory field is missing: library', |
156
|
|
|
'401664' |
157
|
|
|
))->during('getXML', ['/items/123']); |
158
|
|
|
|
159
|
|
|
expect($http->getRequests())->toHaveCount(1); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function it_can_throw_resource_not_found() |
163
|
|
|
{ |
164
|
|
|
$http = $this->let(); |
165
|
|
|
|
166
|
|
|
$exception = SpecHelper::makeExceptionResponse( |
167
|
|
|
SpecHelper::getDummyData('item_barcode_error_response.json', false) |
168
|
|
|
); |
169
|
|
|
$http->addException($exception); |
170
|
|
|
|
171
|
|
|
$this->shouldThrow(new ResourceNotFound('No items found for barcode 123.', '401689')) |
172
|
|
|
->during('getJSON', ['/items/123']); |
173
|
|
|
|
174
|
|
|
expect($http->getRequests())->toHaveCount(1); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function it_can_throw_resource_not_found_for_500_errors_too() |
178
|
|
|
{ |
179
|
|
|
$http = $this->let(); |
180
|
|
|
|
181
|
|
|
// For Analytics reports, Alma will return 500, not 4xx |
182
|
|
|
$exception = SpecHelper::makeExceptionResponse( |
183
|
|
|
SpecHelper::getDummyData('report_not_found_response.xml', false), |
184
|
|
|
500, |
185
|
|
|
'application/xml;charset=utf-8', |
186
|
|
|
ServerErrorException::class |
187
|
|
|
); |
188
|
|
|
$http->addException($exception); |
189
|
|
|
|
190
|
|
|
$this->shouldThrow(new ResourceNotFound('Path not found (/test/path)', 'INTERNAL_SERVER_ERROR')) |
191
|
|
|
->during('getXML', ['/analytics/reports', ['path' => '/test/path']]); |
192
|
|
|
|
193
|
|
|
expect($http->getRequests())->toHaveCount(1); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
View Code Duplication |
public function it_can_throw_invalid_api_key() |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
$http = $this->let(); |
199
|
|
|
|
200
|
|
|
$exception = SpecHelper::makeExceptionResponse( |
201
|
|
|
'Invalid API Key', |
202
|
|
|
400, |
203
|
|
|
'text/plain;charset=UTF-8' |
204
|
|
|
); |
205
|
|
|
$http->addException($exception); |
206
|
|
|
|
207
|
|
|
$this->shouldThrow(new InvalidApiKey('Invalid API Key', 0)) |
208
|
|
|
->during('getJSON', ['/items/123']); |
209
|
|
|
|
210
|
|
|
expect($http->getRequests())->toHaveCount(1); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function it_will_retry_when_reaching_rate_limit() |
214
|
|
|
{ |
215
|
|
|
$http = $this->let(); |
216
|
|
|
|
217
|
|
|
$exception = SpecHelper::makeExceptionResponse( |
218
|
|
|
SpecHelper::getDummyData('per_second_threshold_error_response.json', false), |
219
|
|
|
400, |
220
|
|
|
'application/json;charset=utf-8' |
221
|
|
|
); |
222
|
|
|
$http->addException($exception); |
223
|
|
|
|
224
|
|
|
$this->getJSON('/items/123'); |
225
|
|
|
|
226
|
|
|
expect($http->getRequests())->toHaveCount(2); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function it_uses_the_lang_parameter() { |
230
|
|
|
$this->setLang("fr"); |
231
|
|
|
$url = $this->buildUrl("/items/123")->getWrappedObject()->__toString(); |
232
|
|
|
expect($url)->shouldEndWith("&lang=fr"); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function it_does_not_use_empty_lang_parameter() { |
236
|
|
|
$url = $this->buildUrl("/items/123")->getWrappedObject()->__toString(); |
237
|
|
|
expect($url)->shouldNotContain("&lang="); |
238
|
|
|
print_r($url); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
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.