3
3
namespace spec \Scriptotek \Alma ;
4
4
5
5
use GuzzleHttp \Psr7 \Response ;
6
- use Http \Client \Common \Exception \ClientErrorException ;
7
6
use Http \Client \Common \Exception \ServerErrorException ;
8
7
use Http \Mock \Client as MockHttp ;
9
8
use PhpSpec \ObjectBehavior ;
@@ -124,16 +123,14 @@ public function it_can_get_redirect_locations()
124
123
$ this ->getRedirectLocation ('/ ' )->shouldBe ('http://test.test ' );
125
124
}
126
125
127
- public function it_processes_json_error_responses (ClientErrorException $ exception )
126
+ public function it_processes_json_error_responses ()
128
127
{
129
- $ exception ->getResponse ()->willReturn (new Response (
130
- 400 ,
131
- ['Content-Type ' => 'application/json;charset=utf-8 ' ],
132
- SpecHelper::getDummyData ('error_response.json ' , false )
133
- ));
134
-
135
128
$ http = $ this ->let ();
136
- $ http ->addException ($ exception ->getWrappedObject ());
129
+
130
+ $ exception = SpecHelper::makeExceptionResponse (
131
+ SpecHelper::getDummyData ('error_response.json ' , false )
132
+ );
133
+ $ http ->addException ($ exception );
137
134
138
135
$ this ->shouldThrow (new RequestFailed (
139
136
'Mandatory field is missing: library ' ,
@@ -143,16 +140,16 @@ public function it_processes_json_error_responses(ClientErrorException $exceptio
143
140
expect ($ http ->getRequests ())->toHaveCount (1 );
144
141
}
145
142
146
- public function it_processes_xml_error_responses (ClientErrorException $ exception )
143
+ public function it_processes_xml_error_responses ()
147
144
{
148
- $ exception ->getResponse ()->willReturn (new Response (
149
- 400 ,
150
- ['Content-Type ' => 'application/xml;charset=utf-8 ' ],
151
- SpecHelper::getDummyData ('error_response.xml ' , false )
152
- ));
153
-
154
145
$ http = $ this ->let ();
155
- $ http ->addException ($ exception ->getWrappedObject ());
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 );
156
153
157
154
$ this ->shouldThrow (new RequestFailed (
158
155
'Mandatory field is missing: library ' ,
@@ -162,68 +159,67 @@ public function it_processes_xml_error_responses(ClientErrorException $exception
162
159
expect ($ http ->getRequests ())->toHaveCount (1 );
163
160
}
164
161
165
- public function it_can_throw_resource_not_found (ClientErrorException $ exception )
162
+ public function it_can_throw_resource_not_found ()
166
163
{
167
- $ exception ->getResponse ()->willReturn (new Response (
168
- 400 ,
169
- ['Content-Type ' => 'application/json;charset=utf-8 ' ],
170
- SpecHelper::getDummyData ('item_barcode_error_response.json ' , false )
171
- ));
172
-
173
164
$ http = $ this ->let ();
174
- $ http ->addException ($ exception ->getWrappedObject ());
165
+
166
+ $ exception = SpecHelper::makeExceptionResponse (
167
+ SpecHelper::getDummyData ('item_barcode_error_response.json ' , false )
168
+ );
169
+ $ http ->addException ($ exception );
175
170
176
171
$ this ->shouldThrow (new ResourceNotFound ('No items found for barcode 123. ' , '401689 ' ))
177
172
->during ('getJSON ' , ['/items/123 ' ]);
178
173
179
174
expect ($ http ->getRequests ())->toHaveCount (1 );
180
175
}
181
176
182
- public function it_can_throw_resource_not_found_for_500_errors_too (ServerErrorException $ exception )
177
+ public function it_can_throw_resource_not_found_for_500_errors_too ()
183
178
{
179
+ $ http = $ this ->let ();
180
+
184
181
// For Analytics reports, Alma will return 500, not 4xx
185
- $ exception ->getResponse ()->willReturn (new Response (
182
+ $ exception = SpecHelper::makeExceptionResponse (
183
+ SpecHelper::getDummyData ('report_not_found_response.xml ' , false ),
186
184
500 ,
187
- ['Content-Type ' => 'application/xml;charset=utf-8 ' ],
188
- SpecHelper::getDummyData ('report_not_found_response.xml ' , false )
189
- ));
190
-
191
- $ http = $ this ->let ();
192
- $ http ->addException ($ exception ->getWrappedObject ());
185
+ 'application/xml;charset=utf-8 ' ,
186
+ ServerErrorException::class
187
+ );
188
+ $ http ->addException ($ exception );
193
189
194
190
$ this ->shouldThrow (new ResourceNotFound ('Path not found (/test/path) ' , 'INTERNAL_SERVER_ERROR ' ))
195
191
->during ('getXML ' , ['/analytics/reports ' , ['path ' => '/test/path ' ]]);
196
192
197
193
expect ($ http ->getRequests ())->toHaveCount (1 );
198
194
}
199
195
200
- public function it_can_throw_invalid_api_key (ClientErrorException $ exception )
196
+ public function it_can_throw_invalid_api_key ()
201
197
{
202
- $ exception ->getResponse ()->willReturn (new Response (
203
- 400 ,
204
- ['Content-Type ' => 'text/plain;charset=UTF-8 ' ],
205
- 'Invalid API Key '
206
- ));
207
-
208
198
$ http = $ this ->let ();
209
- $ http ->addException ($ exception ->getWrappedObject ());
199
+
200
+ $ exception = SpecHelper::makeExceptionResponse (
201
+ 'Invalid API Key ' ,
202
+ 400 ,
203
+ 'text/plain;charset=UTF-8 '
204
+ );
205
+ $ http ->addException ($ exception );
210
206
211
207
$ this ->shouldThrow (new InvalidApiKey ('Invalid API Key ' , 0 ))
212
208
->during ('getJSON ' , ['/items/123 ' ]);
213
209
214
210
expect ($ http ->getRequests ())->toHaveCount (1 );
215
211
}
216
212
217
- public function it_will_retry_when_reaching_rate_limit (ClientErrorException $ exception )
213
+ public function it_will_retry_when_reaching_rate_limit ()
218
214
{
219
- $ exception ->getResponse ()->willReturn (new Response (
220
- 400 ,
221
- ['Content-Type ' => 'application/json;charset=utf-8 ' ],
222
- SpecHelper::getDummyData ('per_second_threshold_error_response.json ' , false )
223
- ));
224
-
225
215
$ http = $ this ->let ();
226
- $ http ->addException ($ exception ->getWrappedObject ());
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 );
227
223
228
224
$ this ->getJSON ('/items/123 ' );
229
225
0 commit comments