44
55namespace Codeception \Lib \Connector ;
66
7- use Aws \Credentials \Credentials ;
8- use Aws \Signature \SignatureV4 ;
7+ use Aws \Credentials \Credentials as AwsCredentials ;
8+ use Aws \Signature \SignatureV4 as AwsSignatureV4 ;
99use Codeception \Util \Uri ;
1010use GuzzleHttp \Client as GuzzleClient ;
11- use GuzzleHttp \Cookie \CookieJar ;
11+ use GuzzleHttp \Cookie \CookieJar as GuzzleCookieJar ;
1212use GuzzleHttp \Cookie \SetCookie ;
1313use GuzzleHttp \Exception \RequestException ;
1414use GuzzleHttp \Handler \CurlHandler ;
1515use GuzzleHttp \Handler \StreamHandler ;
16- use GuzzleHttp \HandlerStack ;
16+ use GuzzleHttp \HandlerStack as GuzzleHandlerStack ;
1717use GuzzleHttp \Psr7 \Request as Psr7Request ;
1818use GuzzleHttp \Psr7 \Response as Psr7Response ;
1919use GuzzleHttp \Psr7 \Uri as Psr7Uri ;
2323
2424class Guzzle extends AbstractBrowser
2525{
26- /**
27- * @var array
28- */
29- protected $ requestOptions = [
26+ protected array $ requestOptions = [
3027 'allow_redirects ' => false ,
3128 'headers ' => [],
3229 ];
3330
34- /**
35- * @var int
36- */
37- protected $ refreshMaxInterval = 0 ;
31+ protected int $ refreshMaxInterval = 0 ;
3832
39- /**
40- * @var \Aws\Credentials\Credentials|null
41- */
42- protected $ awsCredentials ;
33+ protected ?AwsCredentials $ awsCredentials = null ;
4334
44- /**
45- * @var \Aws\Signature\SignatureV4|null
46- */
47- protected $ awsSignature ;
35+ protected ?AwsSignatureV4 $ awsSignature = null ;
4836
49- /**
50- * @var GuzzleClient
51- */
52- protected $ client ;
37+ protected ?GuzzleClient $ client = null ;
5338
5439 /**
5540 * Sets the maximum allowable timeout interval for a meta tag refresh to
@@ -108,13 +93,12 @@ public function setAuth(string $username, string $password, string $type = 'basi
10893 unset($ this ->requestOptions ['auth ' ]);
10994 return ;
11095 }
96+
11197 $ this ->requestOptions ['auth ' ] = [$ username , $ password , $ type ];
11298 }
11399
114100 /**
115101 * Taken from Mink\BrowserKitDriver
116- *
117- * @return BrowserKitResponse
118102 */
119103 protected function createResponse (Psr7Response $ psr7Response ): BrowserKitResponse
120104 {
@@ -126,6 +110,7 @@ protected function createResponse(Psr7Response $psr7Response): BrowserKitRespons
126110 if (isset ($ headers ['Content-Type ' ])) {
127111 $ contentType = reset ($ headers ['Content-Type ' ]);
128112 }
113+
129114 if (!$ contentType ) {
130115 $ contentType = 'text/html ' ;
131116 }
@@ -134,6 +119,7 @@ protected function createResponse(Psr7Response $psr7Response): BrowserKitRespons
134119 if (preg_match ('#<meta[^>]+charset *= *[" \']?([a-zA-Z\-0-9]+)#i ' , $ body , $ matches )) {
135120 $ contentType .= ';charset= ' . $ matches [1 ];
136121 }
122+
137123 $ headers ['Content-Type ' ] = [$ contentType ];
138124 }
139125
@@ -182,17 +168,19 @@ protected function getAbsoluteUri($uri)
182168
183169 return Uri::appendPath ((string )$ baseUri , $ uri );
184170 }
171+
185172 // relative url
186173 if (!$ this ->getHistory ()->isEmpty ()) {
187174 return Uri::mergeUrls ((string )$ this ->getHistory ()->current ()->getUri (), $ uri );
188175 }
189176 }
177+
190178 return Uri::mergeUrls ((string )$ baseUri , $ uri );
191179 }
192180
193181 protected function doRequest ($ request )
194182 {
195- /** @var $request BrowserKitRequest **/
183+ /** @var $request BrowserKitRequest **/
196184 $ guzzleRequest = new Psr7Request (
197185 $ request ->getMethod (),
198186 $ request ->getUri (),
@@ -217,15 +205,20 @@ protected function doRequest($request)
217205 } else {
218206 $ response = $ this ->client ->send ($ guzzleRequest , $ options );
219207 }
220- } catch (RequestException $ e ) {
221- if (!$ e ->hasResponse ()) {
222- throw $ e ;
208+ } catch (RequestException $ exception ) {
209+ if (!$ exception ->hasResponse ()) {
210+ throw $ exception ;
223211 }
224- $ response = $ e ->getResponse ();
212+
213+ $ response = $ exception ->getResponse ();
225214 }
215+
226216 return $ this ->createResponse ($ response );
227217 }
228218
219+ /**
220+ * @return array<string, mixed>
221+ */
229222 protected function extractHeaders (BrowserKitRequest $ request ): array
230223 {
231224 $ headers = [];
@@ -240,6 +233,7 @@ protected function extractHeaders(BrowserKitRequest $request): array
240233 $ headers [$ header ] = $ val ;
241234 }
242235 }
236+
243237 return $ headers ;
244238 }
245239
@@ -255,9 +249,11 @@ protected function extractFormData(BrowserKitRequest $browserKitRequest): ?array
255249 if (isset ($ headers ['HTTP_CONTENT_TYPE ' ]) && $ headers ['HTTP_CONTENT_TYPE ' ] !== 'application/x-www-form-urlencoded ' ) {
256250 return null ;
257251 }
252+
258253 if ($ browserKitRequest ->getContent () !== null ) {
259254 return null ;
260255 }
256+
261257 return $ browserKitRequest ->getParameters ();
262258 }
263259
@@ -275,6 +271,7 @@ protected function extractMultipartFormData(BrowserKitRequest $browserKitRequest
275271 foreach ($ browserKitRequest ->getParameters () as $ k => $ parameter ) {
276272 $ parts = $ this ->formatMultipart ($ parts , $ k , $ parameter );
277273 }
274+
278275 return $ parts ;
279276 }
280277
@@ -284,8 +281,10 @@ protected function formatMultipart($parts, $key, $value)
284281 foreach ($ value as $ subKey => $ subValue ) {
285282 $ parts = array_merge ($ this ->formatMultipart ([], $ key .sprintf ('[%s] ' , $ subKey ), $ subValue ), $ parts );
286283 }
284+
287285 return $ parts ;
288286 }
287+
289288 $ parts [] = ['name ' => $ key , 'contents ' => (string ) $ value ];
290289 return $ parts ;
291290 }
@@ -313,6 +312,7 @@ protected function mapFiles($requestFiles, $arrayName = ''): array
313312 'content-type ' => $ info ['type ' ]
314313 ];
315314 }
315+
316316 $ files [] = $ file ;
317317 }
318318 } else {
@@ -329,7 +329,7 @@ protected function mapFiles($requestFiles, $arrayName = ''): array
329329 return $ files ;
330330 }
331331
332- protected function extractCookies ($ host ): \ GuzzleHttp \ Cookie \ CookieJar
332+ protected function extractCookies ($ host ): GuzzleCookieJar
333333 {
334334 $ jar = [];
335335 $ cookies = $ this ->getCookieJar ()->all ();
@@ -338,34 +338,41 @@ protected function extractCookies($host): \GuzzleHttp\Cookie\CookieJar
338338 if (!$ setCookie ->getDomain ()) {
339339 $ setCookie ->setDomain ($ host );
340340 }
341+
341342 $ jar [] = $ setCookie ;
342343 }
343- return new CookieJar (false , $ jar );
344+
345+ return new GuzzleCookieJar (false , $ jar );
344346 }
345347
346- public static function createHandler ($ handler ): \ GuzzleHttp \ HandlerStack
348+ public static function createHandler ($ handler ): GuzzleHandlerStack
347349 {
348- if ($ handler instanceof HandlerStack ) {
350+ if ($ handler instanceof GuzzleHandlerStack ) {
349351 return $ handler ;
350352 }
353+
351354 if ($ handler === 'curl ' ) {
352- return HandlerStack ::create (new CurlHandler ());
355+ return GuzzleHandlerStack ::create (new CurlHandler ());
353356 }
357+
354358 if ($ handler === 'stream ' ) {
355- return HandlerStack ::create (new StreamHandler ());
359+ return GuzzleHandlerStack ::create (new StreamHandler ());
356360 }
361+
357362 if (is_string ($ handler ) && class_exists ($ handler )) {
358- return HandlerStack ::create (new $ handler );
363+ return GuzzleHandlerStack ::create (new $ handler );
359364 }
365+
360366 if (is_callable ($ handler )) {
361- return HandlerStack ::create ($ handler );
367+ return GuzzleHandlerStack ::create ($ handler );
362368 }
363- return HandlerStack::create ();
369+
370+ return GuzzleHandlerStack::create ();
364371 }
365372
366373 public function setAwsAuth ($ config ): void
367374 {
368- $ this ->awsCredentials = new Credentials ($ config ['key ' ], $ config ['secret ' ]);
369- $ this ->awsSignature = new SignatureV4 ($ config ['service ' ], $ config ['region ' ]);
375+ $ this ->awsCredentials = new AwsCredentials ($ config ['key ' ], $ config ['secret ' ]);
376+ $ this ->awsSignature = new AwsSignatureV4 ($ config ['service ' ], $ config ['region ' ]);
370377 }
371378}
0 commit comments