44
55use Akeneo \Pim \ApiClient \Exception \BadRequestHttpException ;
66use Akeneo \Pim \ApiClient \Exception \ClientErrorHttpException ;
7+ use Akeneo \Pim \ApiClient \Exception \ForbiddenHttpException ;
8+ use Akeneo \Pim \ApiClient \Exception \MethodNotAllowedHttpException ;
9+ use Akeneo \Pim \ApiClient \Exception \NotAcceptableHttpException ;
710use Akeneo \Pim \ApiClient \Exception \NotFoundHttpException ;
811use Akeneo \Pim \ApiClient \Exception \RedirectionHttpException ;
912use Akeneo \Pim \ApiClient \Exception \ServerErrorHttpException ;
13+ use Akeneo \Pim \ApiClient \Exception \TooManyRequestsHttpException ;
1014use Akeneo \Pim \ApiClient \Exception \UnauthorizedHttpException ;
1115use Akeneo \Pim \ApiClient \Exception \UnprocessableEntityHttpException ;
1216use Akeneo \Pim \ApiClient \Client \HttpExceptionHandler ;
17+ use Akeneo \Pim \ApiClient \Exception \UnsupportedMediaTypeHttpException ;
1318use PhpSpec \ObjectBehavior ;
1419use Psr \Http \Message \RequestInterface ;
1520use Psr \Http \Message \ResponseInterface ;
@@ -82,6 +87,26 @@ function it_throws_unauthorized_request_exception_when_status_code_401(
8287 ->during ('transformResponseToException ' , [$ request , $ response ]);
8388 }
8489
90+ function it_throws_forbidden_exception_when_status_code_403 (
91+ RequestInterface $ request ,
92+ ResponseInterface $ response ,
93+ StreamInterface $ responseBody
94+ ) {
95+ $ response ->getStatusCode ()->willReturn (403 );
96+ $ response ->getBody ()->willReturn ($ responseBody );
97+ $ responseBody ->getContents ()->willReturn ('{"code": 403, "message": "Access forbidden."} ' );
98+ $ responseBody ->rewind ()->shouldBeCalled ();
99+ $ this
100+ ->shouldThrow (
101+ new ForbiddenHttpException (
102+ 'Access forbidden. ' ,
103+ $ request ->getWrappedObject (),
104+ $ response ->getWrappedObject ()
105+ )
106+ )
107+ ->during ('transformResponseToException ' , [$ request , $ response ]);
108+ }
109+
85110 function it_throws_not_found_exception_when_status_code_404 (
86111 RequestInterface $ request ,
87112 ResponseInterface $ response ,
@@ -102,6 +127,81 @@ function it_throws_not_found_exception_when_status_code_404(
102127 ->during ('transformResponseToException ' , [$ request , $ response ]);
103128 }
104129
130+ function it_throws_method_not_allowed_exception_when_status_code_405 (
131+ RequestInterface $ request ,
132+ ResponseInterface $ response ,
133+ StreamInterface $ responseBody
134+ ) {
135+ $ response ->getStatusCode ()->willReturn (405 );
136+ $ response ->getBody ()->willReturn ($ responseBody );
137+ $ responseBody ->getContents ()->willReturn (<<<JSON
138+ {
139+ "code": 405,
140+ "message": "No route found for 'POST /api/rest/v1/products/myproduct': Method Not Allowed (Allow: GET, PATCH, DELETE)"
141+ }
142+ JSON );
143+ $ responseBody ->rewind ()->shouldBeCalled ();
144+ $ this
145+ ->shouldThrow (
146+ new MethodNotAllowedHttpException (
147+ 'No route found for \'POST /api/rest/v1/products/myproduct \': Method Not Allowed (Allow: GET, PATCH, DELETE) ' ,
148+ $ request ->getWrappedObject (),
149+ $ response ->getWrappedObject ()
150+ )
151+ )
152+ ->during ('transformResponseToException ' , [$ request , $ response ]);
153+ }
154+
155+ function it_throws_method_not_allowed_exception_when_status_code_406 (
156+ RequestInterface $ request ,
157+ ResponseInterface $ response ,
158+ StreamInterface $ responseBody
159+ ) {
160+ $ response ->getStatusCode ()->willReturn (406 );
161+ $ response ->getBody ()->willReturn ($ responseBody );
162+ $ responseBody ->getContents ()->willReturn (<<<JSON
163+ {
164+ "code": 406,
165+ "message": "‘xxx’ in ‘Accept‘ header is not valid. Only ‘application/json‘ is allowed."
166+ }
167+ JSON );
168+ $ responseBody ->rewind ()->shouldBeCalled ();
169+ $ this
170+ ->shouldThrow (
171+ new NotAcceptableHttpException (
172+ '‘xxx’ in ‘Accept‘ header is not valid. Only ‘application/json‘ is allowed. ' ,
173+ $ request ->getWrappedObject (),
174+ $ response ->getWrappedObject ()
175+ )
176+ )
177+ ->during ('transformResponseToException ' , [$ request , $ response ]);
178+ }
179+
180+ function it_throws_method_not_allowed_exception_when_status_code_415 (
181+ RequestInterface $ request ,
182+ ResponseInterface $ response ,
183+ StreamInterface $ responseBody
184+ ) {
185+ $ response ->getStatusCode ()->willReturn (415 );
186+ $ response ->getBody ()->willReturn ($ responseBody );
187+ $ responseBody ->getContents ()->willReturn (<<<JSON
188+ {
189+ "code": 415,
190+ "message": "The ‘Content-type’ header is missing. ‘application/json’ has to specified as value."
191+ }
192+ JSON );
193+ $ responseBody ->rewind ()->shouldBeCalled ();
194+ $ this
195+ ->shouldThrow (
196+ new UnsupportedMediaTypeHttpException (
197+ 'The ‘Content-type’ header is missing. ‘application/json’ has to specified as value. ' ,
198+ $ request ->getWrappedObject (),
199+ $ response ->getWrappedObject ()
200+ )
201+ )
202+ ->during ('transformResponseToException ' , [$ request , $ response ]);
203+ }
204+
105205 function it_throws_bad_request_exception_when_status_code_422 (
106206 RequestInterface $ request ,
107207 ResponseInterface $ response ,
@@ -122,14 +222,34 @@ function it_throws_bad_request_exception_when_status_code_422(
122222 ->during ('transformResponseToException ' , [$ request , $ response ]);
123223 }
124224
225+ function it_throws_bad_request_exception_when_status_code_429 (
226+ RequestInterface $ request ,
227+ ResponseInterface $ response ,
228+ StreamInterface $ responseBody
229+ ) {
230+ $ response ->getStatusCode ()->willReturn (429 );
231+ $ response ->getBody ()->willReturn ($ responseBody );
232+ $ responseBody ->getContents ()->willReturn ('Too Many Requests ' );
233+ $ responseBody ->getContents ()->shouldBeCalled ();
234+ $ this
235+ ->shouldThrow (
236+ new TooManyRequestsHttpException (
237+ 'Too Many Requests ' ,
238+ $ request ->getWrappedObject (),
239+ $ response ->getWrappedObject ()
240+ )
241+ )
242+ ->during ('transformResponseToException ' , [$ request , $ response ]);
243+ }
244+
125245 function it_throws_bad_request_exception_when_status_code_4xx (
126246 RequestInterface $ request ,
127247 ResponseInterface $ response ,
128248 StreamInterface $ responseBody
129249 ) {
130- $ response ->getStatusCode ()->willReturn (405 );
250+ $ response ->getStatusCode ()->willReturn (418 );
131251 $ response ->getBody ()->willReturn ($ responseBody );
132- $ responseBody ->getContents ()->willReturn ('{"code": 405 , "message": "Not allowed."} ' );
252+ $ responseBody ->getContents ()->willReturn ('{"code": 418 , "message": "Not allowed."} ' );
133253 $ responseBody ->rewind ()->shouldBeCalled ();
134254 $ this
135255 ->shouldThrow (
0 commit comments