1
1
<?php
2
+
3
+ declare (strict_types=1 );
4
+
2
5
namespace Codeception \Lib \Connector ;
3
6
4
7
use Aws \Credentials \Credentials ;
14
17
use GuzzleHttp \Psr7 \Request as Psr7Request ;
15
18
use GuzzleHttp \Psr7 \Response as Psr7Response ;
16
19
use GuzzleHttp \Psr7 \Uri as Psr7Uri ;
17
- use Symfony \Component \BrowserKit \AbstractBrowser as Client ;
20
+ use Symfony \Component \BrowserKit \AbstractBrowser ;
18
21
use Symfony \Component \BrowserKit \Request as BrowserKitRequest ;
19
22
use Symfony \Component \BrowserKit \Response as BrowserKitResponse ;
20
23
21
- class Guzzle extends Client
24
+ class Guzzle extends AbstractBrowser
22
25
{
26
+ /**
27
+ * @var array
28
+ */
23
29
protected $ requestOptions = [
24
30
'allow_redirects ' => false ,
25
31
'headers ' => [],
26
32
];
33
+
34
+ /**
35
+ * @var int
36
+ */
27
37
protected $ refreshMaxInterval = 0 ;
28
38
39
+ /**
40
+ * @var \Aws\Credentials\Credentials|null
41
+ */
29
42
protected $ awsCredentials ;
43
+
44
+ /**
45
+ * @var \Aws\Signature\SignatureV4|null
46
+ */
30
47
protected $ awsSignature ;
31
48
32
- /** @var GuzzleClient */
49
+ /**
50
+ * @var GuzzleClient
51
+ */
33
52
protected $ client ;
34
53
35
54
/**
@@ -43,14 +62,14 @@ class Guzzle extends Client
43
62
*
44
63
* @param int $seconds Number of seconds
45
64
*/
46
- public function setRefreshMaxInterval ($ seconds )
65
+ public function setRefreshMaxInterval (int $ seconds ): void
47
66
{
48
67
$ this ->refreshMaxInterval = $ seconds ;
49
68
}
50
69
51
- public function setClient (GuzzleClient $ client )
70
+ public function setClient (GuzzleClient $ guzzleClient ): void
52
71
{
53
- $ this ->client = $ client ;
72
+ $ this ->client = $ guzzleClient ;
54
73
}
55
74
56
75
/**
@@ -63,9 +82,9 @@ public function setClient(GuzzleClient $client)
63
82
* @param string $name the name of the header
64
83
* @param string $value the value of the header
65
84
*/
66
- public function setHeader ($ name , $ value )
85
+ public function setHeader (string $ name , string $ value ): void
67
86
{
68
- if (( string ) $ value === '' ) {
87
+ if ($ value === '' ) {
69
88
$ this ->deleteHeader ($ name );
70
89
} else {
71
90
$ this ->requestOptions ['headers ' ][$ name ] = $ value ;
@@ -78,19 +97,14 @@ public function setHeader($name, $value)
78
97
*
79
98
* @param string $name the name of the header to delete.
80
99
*/
81
- public function deleteHeader ($ name )
100
+ public function deleteHeader (string $ name ): void
82
101
{
83
102
unset($ this ->requestOptions ['headers ' ][$ name ]);
84
103
}
85
104
86
- /**
87
- * @param string $username
88
- * @param string $password
89
- * @param string $type Default: 'basic'
90
- */
91
- public function setAuth ($ username , $ password , $ type = 'basic ' )
105
+ public function setAuth (string $ username , string $ password , string $ type = 'basic ' ): void
92
106
{
93
- if (! $ username ) {
107
+ if ($ username === '' ) {
94
108
unset($ this ->requestOptions ['auth ' ]);
95
109
return ;
96
110
}
@@ -100,14 +114,12 @@ public function setAuth($username, $password, $type = 'basic')
100
114
/**
101
115
* Taken from Mink\BrowserKitDriver
102
116
*
103
- * @param Psr7Response $response
104
- *
105
117
* @return BrowserKitResponse
106
118
*/
107
- protected function createResponse (Psr7Response $ response )
119
+ protected function createResponse (Psr7Response $ psr7Response ): BrowserKitResponse
108
120
{
109
- $ body = (string ) $ response ->getBody ();
110
- $ headers = $ response ->getHeaders ();
121
+ $ body = (string ) $ psr7Response ->getBody ();
122
+ $ headers = $ psr7Response ->getHeaders ();
111
123
112
124
$ contentType = null ;
113
125
@@ -119,26 +131,26 @@ protected function createResponse(Psr7Response $response)
119
131
}
120
132
121
133
if (strpos ($ contentType , 'charset= ' ) === false ) {
122
- if (preg_match ('/ <meta[^>]+charset *= *[" \']?([a-zA-Z\-0-9]+)/ i ' , $ body , $ matches )) {
134
+ if (preg_match ('# <meta[^>]+charset *= *[" \']?([a-zA-Z\-0-9]+)# i ' , $ body , $ matches )) {
123
135
$ contentType .= ';charset= ' . $ matches [1 ];
124
136
}
125
137
$ headers ['Content-Type ' ] = [$ contentType ];
126
138
}
127
139
128
- $ status = $ response ->getStatusCode ();
140
+ $ status = $ psr7Response ->getStatusCode ();
129
141
if ($ status < 300 || $ status >= 400 ) {
130
142
$ matches = [];
131
143
132
144
$ matchesMeta = preg_match (
133
- '/ <meta[^>]+http-equiv="refresh" content="\s*(\d*)\s*;\s*url=(.*?)"/ i ' ,
145
+ '# <meta[^>]+http-equiv="refresh" content="\s*(\d*)\s*;\s*url=(.*?)"# i ' ,
134
146
$ body ,
135
147
$ matches
136
148
);
137
149
138
150
if (!$ matchesMeta && isset ($ headers ['Refresh ' ])) {
139
151
// match by header
140
152
preg_match (
141
- '/ ^\s*(\d*)\s*;\s*url=(.*)/ i ' ,
153
+ '# ^\s*(\d*)\s*;\s*url=(.*)# i ' ,
142
154
(string ) reset ($ headers ['Refresh ' ]),
143
155
$ matches
144
156
);
@@ -150,15 +162,15 @@ protected function createResponse(Psr7Response $response)
150
162
151
163
if ($ uri ->withFragment ('' ) !== $ currentUri ->withFragment ('' )) {
152
164
$ status = 302 ;
153
- $ headers ['Location ' ] = $ matchesMeta ? htmlspecialchars_decode ($ uri ) : (string )$ uri ;
165
+ $ headers ['Location ' ] = $ matchesMeta ? htmlspecialchars_decode (( string ) $ uri ) : (string )$ uri ;
154
166
}
155
167
}
156
168
}
157
169
158
170
return new BrowserKitResponse ($ body , $ status , $ headers );
159
171
}
160
172
161
- public function getAbsoluteUri ($ uri )
173
+ protected function getAbsoluteUri ($ uri )
162
174
{
163
175
$ baseUri = $ this ->client ->getConfig ('base_uri ' );
164
176
if (strpos ($ uri , ':// ' ) === false && strpos ($ uri , '// ' ) !== 0 ) {
@@ -172,7 +184,7 @@ public function getAbsoluteUri($uri)
172
184
}
173
185
// relative url
174
186
if (!$ this ->getHistory ()->isEmpty ()) {
175
- return Uri::mergeUrls (( string ) $ this ->getHistory ()->current ()->getUri (), $ uri );
187
+ return Uri::mergeUrls ($ this ->getHistory ()->current ()->getUri (), $ uri );
176
188
}
177
189
}
178
190
return Uri::mergeUrls ($ baseUri , $ uri );
@@ -214,7 +226,7 @@ protected function doRequest($request)
214
226
return $ this ->createResponse ($ response );
215
227
}
216
228
217
- protected function extractHeaders (BrowserKitRequest $ request )
229
+ protected function extractHeaders (BrowserKitRequest $ request ): array
218
230
{
219
231
$ headers = [];
220
232
$ server = $ request ->getServer ();
@@ -231,39 +243,37 @@ protected function extractHeaders(BrowserKitRequest $request)
231
243
return $ headers ;
232
244
}
233
245
234
- protected function extractFormData (BrowserKitRequest $ request )
246
+ protected function extractFormData (BrowserKitRequest $ browserKitRequest ): ? array
235
247
{
236
- if (!in_array (strtoupper ($ request ->getMethod ()), ['POST ' , 'PUT ' , 'PATCH ' , 'DELETE ' ])) {
248
+ if (!in_array (strtoupper ($ browserKitRequest ->getMethod ()), ['POST ' , 'PUT ' , 'PATCH ' , 'DELETE ' ])) {
237
249
return null ;
238
250
}
239
251
240
252
// guessing if it is a form data
241
- $ headers = $ request ->getServer ();
242
- if (isset ($ headers ['HTTP_CONTENT_TYPE ' ])) {
243
- // not a form
244
- if ($ headers ['HTTP_CONTENT_TYPE ' ] !== 'application/x-www-form-urlencoded ' ) {
245
- return null ;
246
- }
253
+ $ headers = $ browserKitRequest ->getServer ();
254
+ // not a form
255
+ if (isset ($ headers ['HTTP_CONTENT_TYPE ' ]) && $ headers ['HTTP_CONTENT_TYPE ' ] !== 'application/x-www-form-urlencoded ' ) {
256
+ return null ;
247
257
}
248
- if ($ request ->getContent () !== null ) {
258
+ if ($ browserKitRequest ->getContent () !== null ) {
249
259
return null ;
250
260
}
251
- return $ request ->getParameters ();
261
+ return $ browserKitRequest ->getParameters ();
252
262
}
253
263
254
- protected function extractMultipartFormData (BrowserKitRequest $ request )
264
+ protected function extractMultipartFormData (BrowserKitRequest $ browserKitRequest )
255
265
{
256
- if (!in_array (strtoupper ($ request ->getMethod ()), ['POST ' , 'PUT ' , 'PATCH ' ])) {
266
+ if (!in_array (strtoupper ($ browserKitRequest ->getMethod ()), ['POST ' , 'PUT ' , 'PATCH ' ])) {
257
267
return [];
258
268
}
259
269
260
- $ parts = $ this ->mapFiles ($ request ->getFiles ());
270
+ $ parts = $ this ->mapFiles ($ browserKitRequest ->getFiles ());
261
271
if (empty ($ parts )) {
262
272
return [];
263
273
}
264
274
265
- foreach ($ request ->getParameters () as $ k => $ v ) {
266
- $ parts = $ this ->formatMultipart ($ parts , $ k , $ v );
275
+ foreach ($ browserKitRequest ->getParameters () as $ k => $ parameter ) {
276
+ $ parts = $ this ->formatMultipart ($ parts , $ k , $ parameter );
267
277
}
268
278
return $ parts ;
269
279
}
@@ -272,15 +282,15 @@ protected function formatMultipart($parts, $key, $value)
272
282
{
273
283
if (is_array ($ value )) {
274
284
foreach ($ value as $ subKey => $ subValue ) {
275
- $ parts = array_merge ($ this ->formatMultipart ([], $ key ." [ $ subKey] " , $ subValue ), $ parts );
285
+ $ parts = array_merge ($ this ->formatMultipart ([], $ key .sprintf ( ' [%s] ' , $ subKey) , $ subValue ), $ parts );
276
286
}
277
287
return $ parts ;
278
288
}
279
289
$ parts [] = ['name ' => $ key , 'contents ' => (string ) $ value ];
280
290
return $ parts ;
281
291
}
282
292
283
- protected function mapFiles ($ requestFiles , $ arrayName = '' )
293
+ protected function mapFiles ($ requestFiles , $ arrayName = '' ): array
284
294
{
285
295
$ files = [];
286
296
foreach ($ requestFiles as $ name => $ info ) {
@@ -292,7 +302,7 @@ protected function mapFiles($requestFiles, $arrayName = '')
292
302
if (isset ($ info ['tmp_name ' ])) {
293
303
if ($ info ['tmp_name ' ]) {
294
304
$ handle = fopen ($ info ['tmp_name ' ], 'rb ' );
295
- $ filename = isset ( $ info ['name ' ]) ? $ info [ ' name ' ] : null ;
305
+ $ filename = $ info ['name ' ] ?? null ;
296
306
$ file = [
297
307
'name ' => $ name ,
298
308
'contents ' => $ handle ,
@@ -319,7 +329,7 @@ protected function mapFiles($requestFiles, $arrayName = '')
319
329
return $ files ;
320
330
}
321
331
322
- protected function extractCookies ($ host )
332
+ protected function extractCookies ($ host ): \ GuzzleHttp \ Cookie \ CookieJar
323
333
{
324
334
$ jar = [];
325
335
$ cookies = $ this ->getCookieJar ()->all ();
@@ -333,7 +343,7 @@ protected function extractCookies($host)
333
343
return new CookieJar (false , $ jar );
334
344
}
335
345
336
- public static function createHandler ($ handler )
346
+ public static function createHandler ($ handler ): \ GuzzleHttp \ HandlerStack
337
347
{
338
348
if ($ handler instanceof HandlerStack) {
339
349
return $ handler ;
@@ -353,7 +363,7 @@ public static function createHandler($handler)
353
363
return HandlerStack::create ();
354
364
}
355
365
356
- public function setAwsAuth ($ config )
366
+ public function setAwsAuth ($ config ): void
357
367
{
358
368
$ this ->awsCredentials = new Credentials ($ config ['key ' ], $ config ['secret ' ]);
359
369
$ this ->awsSignature = new SignatureV4 ($ config ['service ' ], $ config ['region ' ]);
0 commit comments