33namespace spec \Scriptotek \Alma ;
44
55use GuzzleHttp \Psr7 \Response ;
6- use Http \Client \Common \Exception \ClientErrorException ;
76use Http \Client \Common \Exception \ServerErrorException ;
87use Http \Mock \Client as MockHttp ;
98use PhpSpec \ObjectBehavior ;
@@ -124,16 +123,14 @@ public function it_can_get_redirect_locations()
124123 $ this ->getRedirectLocation ('/ ' )->shouldBe ('http://test.test ' );
125124 }
126125
127- public function it_processes_json_error_responses (ClientErrorException $ exception )
126+ public function it_processes_json_error_responses ()
128127 {
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-
135128 $ 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 );
137134
138135 $ this ->shouldThrow (new RequestFailed (
139136 'Mandatory field is missing: library ' ,
@@ -143,16 +140,16 @@ public function it_processes_json_error_responses(ClientErrorException $exceptio
143140 expect ($ http ->getRequests ())->toHaveCount (1 );
144141 }
145142
146- public function it_processes_xml_error_responses (ClientErrorException $ exception )
143+ public function it_processes_xml_error_responses ()
147144 {
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-
154145 $ 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 );
156153
157154 $ this ->shouldThrow (new RequestFailed (
158155 'Mandatory field is missing: library ' ,
@@ -162,68 +159,67 @@ public function it_processes_xml_error_responses(ClientErrorException $exception
162159 expect ($ http ->getRequests ())->toHaveCount (1 );
163160 }
164161
165- public function it_can_throw_resource_not_found (ClientErrorException $ exception )
162+ public function it_can_throw_resource_not_found ()
166163 {
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-
173164 $ 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 );
175170
176171 $ this ->shouldThrow (new ResourceNotFound ('No items found for barcode 123. ' , '401689 ' ))
177172 ->during ('getJSON ' , ['/items/123 ' ]);
178173
179174 expect ($ http ->getRequests ())->toHaveCount (1 );
180175 }
181176
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 ()
183178 {
179+ $ http = $ this ->let ();
180+
184181 // 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 ),
186184 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 );
193189
194190 $ this ->shouldThrow (new ResourceNotFound ('Path not found (/test/path) ' , 'INTERNAL_SERVER_ERROR ' ))
195191 ->during ('getXML ' , ['/analytics/reports ' , ['path ' => '/test/path ' ]]);
196192
197193 expect ($ http ->getRequests ())->toHaveCount (1 );
198194 }
199195
200- public function it_can_throw_invalid_api_key (ClientErrorException $ exception )
196+ public function it_can_throw_invalid_api_key ()
201197 {
202- $ exception ->getResponse ()->willReturn (new Response (
203- 400 ,
204- ['Content-Type ' => 'text/plain;charset=UTF-8 ' ],
205- 'Invalid API Key '
206- ));
207-
208198 $ 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 );
210206
211207 $ this ->shouldThrow (new InvalidApiKey ('Invalid API Key ' , 0 ))
212208 ->during ('getJSON ' , ['/items/123 ' ]);
213209
214210 expect ($ http ->getRequests ())->toHaveCount (1 );
215211 }
216212
217- public function it_will_retry_when_reaching_rate_limit (ClientErrorException $ exception )
213+ public function it_will_retry_when_reaching_rate_limit ()
218214 {
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-
225215 $ 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 );
227223
228224 $ this ->getJSON ('/items/123 ' );
229225
0 commit comments