11<?php
2-
32use GuzzleHttp \Exception \BadResponseException ;
43use GuzzleHttp \Psr7 \Request ;
54use GuzzleHttp \Psr7 \Response ;
65use Microsoft \Graph \Core \ExceptionWrapper ;
7- use Microsoft \Graph \Exception \GraphException ;
86use PHPUnit \Framework \TestCase ;
97
108class ExceptionWrapperTest extends TestCase
119{
12- public function testWrapBadResponseExceptionReturnsGraphException ()
10+ protected $ responseBodies ;
11+ protected $ autoBadResponseExceptions ;
12+ protected $ manualBadResponseExceptions ;
13+
14+ public function setUp (): void
1315 {
14- $ responseBody = json_encode (array ('body ' => 'content ' ));
15- $ ex = new BadResponseException ("Error: API returned 400 " , new Request ("GET " , "/endpoint " ), new Response (400 , [], $ responseBody ));
16- $ graphException = ExceptionWrapper::wrapGuzzleBadResponseException ($ ex );
17- $ this ->assertInstanceOf (GraphException::class, $ graphException );
16+ $ this ->responseBodies = array (
17+ 'short ' => json_encode (array ('body ' => 'content ' )), // not truncated by Guzzle
18+ 'long ' => json_encode (array ('body ' => base64_encode (random_bytes (120 )) . '. ' )), // truncated by Guzzle
19+ );
20+
21+ $ this ->autoBadResponseExceptions = array ();
22+ $ this ->manualBadResponseExceptions = array ();
23+ foreach ($ this ->responseBodies as $ name => $ responseBody ) {
24+ $ autoBadResponseException = GuzzleHttp \Exception \RequestException::create (new Request ("GET " , "/endpoint " ), new Response (400 , [], $ responseBody ));
25+ assert ($ autoBadResponseException instanceof BadResponseException);
26+ $ this ->autoBadResponseExceptions [$ name ] = $ autoBadResponseException ;
27+
28+ $ manualBadResponseException = new BadResponseException ("Error: API returned 400 " , new Request ("GET " , "/endpoint " ), new Response (400 , [], $ responseBody ));
29+ $ this ->manualBadResponseExceptions [$ name ] = $ manualBadResponseException ;
30+ }
1831 }
1932
20- public function testWrapBadResponseExceptionHasResponseBody ()
33+ public function testWrapBadResponseExceptionReturnsInstanceOfSameClass ()
2134 {
22- $ responseBody = json_encode (array ('body ' => 'content ' ));
23- $ ex = new BadResponseException ("Error: API returned 400 " , new Request ("GET " , "/endpoint " ), new Response (400 , [], $ responseBody ));
24- $ graphException = ExceptionWrapper::wrapGuzzleBadResponseException ($ ex );
25- $ this ->assertStringContainsString ($ responseBody , $ graphException ->getMessage ());
35+ $ name = 'short ' ;
36+
37+ $ ex = $ this ->autoBadResponseExceptions [$ name ];
38+ $ wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException ($ ex );
39+ $ this ->assertInstanceOf (get_class ($ ex ), $ wrappedException );
40+
41+ $ ex = $ this ->manualBadResponseExceptions [$ name ];
42+ $ wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException ($ ex );
43+ $ this ->assertInstanceOf (get_class ($ ex ), $ wrappedException );
44+
45+ $ name = 'long ' ;
46+
47+ $ ex = $ this ->autoBadResponseExceptions [$ name ];
48+ $ wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException ($ ex );
49+ $ this ->assertInstanceOf (get_class ($ ex ), $ wrappedException );
50+
51+ $ ex = $ this ->manualBadResponseExceptions [$ name ];
52+ $ wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException ($ ex );
53+ $ this ->assertInstanceOf (get_class ($ ex ), $ wrappedException );
54+ }
55+
56+ public function testWrapAutoBadResponseExceptionHasResponseBody ()
57+ {
58+ $ name = 'short ' ;
59+ $ responseBody = $ this ->responseBodies [$ name ];
60+ $ ex = $ this ->autoBadResponseExceptions [$ name ];
61+ $ wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException ($ ex );
62+ $ this ->assertStringContainsString ($ responseBody , $ wrappedException ->getMessage ());
63+
64+ $ name = 'long ' ;
65+ $ responseBody = $ this ->responseBodies [$ name ];
66+ $ ex = $ this ->autoBadResponseExceptions [$ name ];
67+ $ wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException ($ ex );
68+ $ this ->assertStringContainsString ($ responseBody , $ wrappedException ->getMessage ());
69+ }
70+
71+ public function testWrapManualBadResponseExceptionHasNotResponseBody ()
72+ {
73+ $ name = 'short ' ;
74+ $ responseBody = $ this ->responseBodies [$ name ];
75+ $ ex = $ this ->manualBadResponseExceptions [$ name ];
76+ $ wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException ($ ex );
77+ $ this ->assertStringNotContainsString ($ responseBody , $ wrappedException ->getMessage ());
78+
79+ $ name = 'long ' ;
80+ $ responseBody = $ this ->responseBodies [$ name ];
81+ $ ex = $ this ->manualBadResponseExceptions [$ name ];
82+ $ wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException ($ ex );
83+ $ this ->assertStringNotContainsString ($ responseBody , $ wrappedException ->getMessage ());
2684 }
2785
2886 public function testWrapBadResponseExceptionWithInvalidInput ()
2987 {
3088 $ this ->expectException (TypeError::class);
3189 ExceptionWrapper::wrapGuzzleBadResponseException (null );
3290 }
33- }
91+ }
0 commit comments