7
7
use Codeception \Util \Debug ;
8
8
use Symfony \Component \BrowserKit \AbstractBrowser as Client ;
9
9
use Symfony \Component \BrowserKit \Cookie ;
10
+ use Symfony \Component \BrowserKit \CookieJar ;
11
+ use Symfony \Component \BrowserKit \History ;
10
12
use Symfony \Component \BrowserKit \Response ;
11
13
use Yii ;
12
14
use yii \base \ExitException ;
15
17
use yii \mail \MessageInterface ;
16
18
use yii \web \Application ;
17
19
use yii \web \ErrorHandler ;
20
+ use yii \web \IdentityInterface ;
18
21
use yii \web \Request ;
19
22
use yii \web \Response as YiiResponse ;
23
+ use yii \web \User ;
20
24
21
25
class Yii2 extends Client
22
26
{
@@ -81,34 +85,30 @@ class Yii2 extends Client
81
85
/**
82
86
* @var bool whether to close the session in between requests inside a single test, if recreateApplication is set to true
83
87
*/
84
- public $ closeSessionOnRecreateApplication = true ;
88
+ public bool $ closeSessionOnRecreateApplication = true ;
85
89
86
90
/**
87
- * @var string The FQN of the application class to use. In a default Yii setup, should be either `yii\web\Application`
91
+ * @var class- string The FQN of the application class to use. In a default Yii setup, should be either `yii\web\Application`
88
92
* or `yii\console\Application`
89
93
*/
90
- public $ applicationClass = null ;
94
+ public string | null $ applicationClass = null ;
91
95
92
96
93
- private $ emails = [];
97
+ private array $ emails = [];
94
98
95
99
/**
96
- * @return \yii\web\Application
97
- *
98
100
* @deprecated since 2.5, will become protected in 3.0. Directly access to \Yii::$app if you need to interact with it.
101
+ * @internal
99
102
*/
100
- public function getApplication ()
103
+ public function getApplication (): \ yii \ base \ Application
101
104
{
102
105
if (!isset (Yii::$ app )) {
103
106
$ this ->startApp ();
104
107
}
105
108
return Yii::$ app ;
106
109
}
107
110
108
- /**
109
- * @param bool $closeSession
110
- */
111
- public function resetApplication ($ closeSession = true )
111
+ public function resetApplication (bool $ closeSession = true ): void
112
112
{
113
113
codecept_debug ('Destroying application ' );
114
114
if (true === $ closeSession ) {
@@ -127,40 +127,28 @@ public function resetApplication($closeSession = true)
127
127
/**
128
128
* Finds and logs in a user
129
129
* @internal
130
- * @param $user
131
130
* @throws ConfigurationException
132
131
* @throws \RuntimeException
133
132
*/
134
- public function findAndLoginUser ($ user )
133
+ public function findAndLoginUser (int | string | IdentityInterface $ user ): void
135
134
{
136
135
$ app = $ this ->getApplication ();
137
- if (!$ app ->has ('user ' )) {
136
+ $ user = $ app ->get ('user ' );
137
+ if (!$ user instanceof User) {
138
138
throw new ConfigurationException ('The user component is not configured ' );
139
139
}
140
140
141
141
if ($ user instanceof \yii \web \IdentityInterface) {
142
142
$ identity = $ user ;
143
143
} else {
144
144
// class name implementing IdentityInterface
145
- $ identityClass = $ app -> user ->identityClass ;
145
+ $ identityClass = $ user ->identityClass ;
146
146
$ identity = call_user_func ([$ identityClass , 'findIdentity ' ], $ user );
147
147
if (!isset ($ identity )) {
148
148
throw new \RuntimeException ('User not found ' );
149
149
}
150
150
}
151
- $ app ->user ->login ($ identity );
152
- }
153
-
154
- /**
155
- * Masks a value
156
- * @internal
157
- * @param string $val
158
- * @return string
159
- * @see \yii\base\Security::maskToken
160
- */
161
- public function maskToken ($ val )
162
- {
163
- return $ this ->getApplication ()->security ->maskToken ($ val );
151
+ $ user ->login ($ identity );
164
152
}
165
153
166
154
/**
@@ -169,7 +157,7 @@ public function maskToken($val)
169
157
* @param string $value The value of the cookie
170
158
* @return string The value to send to the browser
171
159
*/
172
- public function hashCookieData ($ name , $ value )
160
+ public function hashCookieData ($ name , $ value ): string
173
161
{
174
162
$ app = $ this ->getApplication ();
175
163
if (!$ app ->request ->enableCookieValidation ) {
@@ -182,7 +170,7 @@ public function hashCookieData($name, $value)
182
170
* @internal
183
171
* @return array List of regex patterns for recognized domain names
184
172
*/
185
- public function getInternalDomains ()
173
+ public function getInternalDomains (): array
186
174
{
187
175
/** @var \yii\web\UrlManager $urlManager */
188
176
$ urlManager = $ this ->getApplication ()->urlManager ;
@@ -202,7 +190,7 @@ public function getInternalDomains()
202
190
* @internal
203
191
* @return array List of sent emails
204
192
*/
205
- public function getEmails ()
193
+ public function getEmails (): array
206
194
{
207
195
return $ this ->emails ;
208
196
}
@@ -211,7 +199,7 @@ public function getEmails()
211
199
* Deletes all stored emails.
212
200
* @internal
213
201
*/
214
- public function clearEmails ()
202
+ public function clearEmails (): void
215
203
{
216
204
$ this ->emails = [];
217
205
}
@@ -230,11 +218,8 @@ public function getComponent($name)
230
218
231
219
/**
232
220
* Getting domain regex from rule host template
233
- *
234
- * @param string $template
235
- * @return string
236
221
*/
237
- private function getDomainRegex ($ template )
222
+ private function getDomainRegex (string $ template ): string
238
223
{
239
224
if (preg_match ('#https?://(.*)# ' , $ template , $ matches )) {
240
225
$ template = $ matches [1 ];
@@ -259,24 +244,13 @@ function ($matches) use (&$parameters) {
259
244
/**
260
245
* Gets the name of the CSRF param.
261
246
* @internal
262
- * @return string
263
247
*/
264
- public function getCsrfParamName ()
248
+ public function getCsrfParamName (): string
265
249
{
266
250
return $ this ->getApplication ()->request ->csrfParam ;
267
251
}
268
252
269
- /**
270
- * @internal
271
- * @param $params
272
- * @return mixed
273
- */
274
- public function createUrl ($ params )
275
- {
276
- return is_array ($ params ) ?$ this ->getApplication ()->getUrlManager ()->createUrl ($ params ) : $ params ;
277
- }
278
-
279
- public function startApp (\yii \log \Logger $ logger = null )
253
+ public function startApp (\yii \log \Logger $ logger = null ): void
280
254
{
281
255
codecept_debug ('Starting application ' );
282
256
$ config = require ($ this ->configFile );
@@ -306,12 +280,9 @@ public function startApp(\yii\log\Logger $logger = null)
306
280
}
307
281
308
282
/**
309
- *
310
283
* @param \Symfony\Component\BrowserKit\Request $request
311
- *
312
- * @return \Symfony\Component\BrowserKit\Response
313
284
*/
314
- public function doRequest (object $ request )
285
+ public function doRequest (object $ request ): \ Symfony \ Component \ BrowserKit \ Response
315
286
{
316
287
$ _COOKIE = $ request ->getCookies ();
317
288
$ _SERVER = $ request ->getServer ();
@@ -343,6 +314,9 @@ public function doRequest(object $request)
343
314
$ this ->beforeRequest ();
344
315
345
316
$ app = $ this ->getApplication ();
317
+ if (!$ app instanceof Application) {
318
+ throw new ConfigurationException ("Application is not a web application " );
319
+ }
346
320
347
321
// disabling logging. Logs are slowing test execution down
348
322
foreach ($ app ->log ->targets as $ target ) {
@@ -407,22 +381,22 @@ protected function revertErrorHandler()
407
381
408
382
/**
409
383
* Encodes the cookies and adds them to the headers.
410
- * @param \yii\web\Response $response
411
384
* @throws \yii\base\InvalidConfigException
412
385
*/
413
386
protected function encodeCookies (
414
387
YiiResponse $ response ,
415
388
Request $ request ,
416
389
Security $ security
417
- ) {
390
+ ): void {
418
391
if ($ request ->enableCookieValidation ) {
419
392
$ validationKey = $ request ->cookieValidationKey ;
420
393
}
421
394
422
395
foreach ($ response ->getCookies () as $ cookie ) {
423
396
/** @var \yii\web\Cookie $cookie */
424
397
$ value = $ cookie ->value ;
425
- if ($ cookie ->expire != 1 && isset ($ validationKey )) {
398
+ // Expire = 1 means we're removing the cookie
399
+ if ($ cookie ->expire !== 1 && isset ($ validationKey )) {
426
400
$ data = version_compare (Yii::getVersion (), '2.0.2 ' , '> ' )
427
401
? [$ cookie ->name , $ cookie ->value ]
428
402
: $ cookie ->value ;
@@ -443,10 +417,10 @@ protected function encodeCookies(
443
417
444
418
/**
445
419
* Replace mailer with in memory mailer
446
- * @param array $config Original configuration
447
- * @return array New configuration
420
+ * @param array<string, mixed> $config Original configuration
421
+ * @return array<string, mixed> New configuration
448
422
*/
449
- protected function mockMailer (array $ config )
423
+ protected function mockMailer (array $ config ): array
450
424
{
451
425
// options that make sense for mailer mock
452
426
$ allowedOptions = [
@@ -489,30 +463,23 @@ public function restart(): void
489
463
/**
490
464
* Return an assoc array with the client context: cookieJar, history.
491
465
*
492
- * @return array
466
+ * @internal
467
+ * @return array{ cookieJar: CookieJar, history: History }
493
468
*/
494
- public function getContext ()
469
+ public function getContext (): array
495
470
{
496
471
return [
497
472
'cookieJar ' => $ this ->cookieJar ,
498
473
'history ' => $ this ->history ,
499
474
];
500
475
}
501
476
502
- /**
503
- * Reset the client context: empty cookieJar and history.
504
- */
505
- public function removeContext ()
506
- {
507
- parent ::restart ();
508
- }
509
-
510
477
/**
511
478
* Set the context, see getContext().
512
479
*
513
- * @param array $context
480
+ * @param array{ cookieJar: CookieJar, history: History } $context
514
481
*/
515
- public function setContext (array $ context )
482
+ public function setContext (array $ context ): void
516
483
{
517
484
$ this ->cookieJar = $ context ['cookieJar ' ];
518
485
$ this ->history = $ context ['history ' ];
@@ -522,18 +489,19 @@ public function setContext(array $context)
522
489
* This functions closes the session of the application, if the application exists and has a session.
523
490
* @internal
524
491
*/
525
- public function closeSession ()
492
+ public function closeSession (): void
526
493
{
527
- if (isset (\Yii::$ app ) && \Yii::$ app ->has ('session ' , true )) {
528
- \Yii::$ app ->session ->close ();
494
+ $ app = \Yii::$ app ;
495
+ if ($ app instanceof \yii \web \Application && $ app ->has ('session ' , true )) {
496
+ $ app ->session ->close ();
529
497
}
530
498
}
531
499
532
500
/**
533
501
* Resets the applications' response object.
534
502
* The method used depends on the module configuration.
535
503
*/
536
- protected function resetResponse (Application $ app )
504
+ protected function resetResponse (Application $ app ): void
537
505
{
538
506
$ method = $ this ->responseCleanMethod ;
539
507
// First check the current response object.
@@ -566,7 +534,7 @@ protected function resetResponse(Application $app)
566
534
}
567
535
}
568
536
569
- protected function resetRequest (Application $ app )
537
+ protected function resetRequest (Application $ app ): void
570
538
{
571
539
$ method = $ this ->requestCleanMethod ;
572
540
$ request = $ app ->request ;
@@ -596,8 +564,8 @@ protected function resetRequest(Application $app)
596
564
$ request ->setScriptFile (null );
597
565
$ request ->setScriptUrl (null );
598
566
$ request ->setUrl (null );
599
- $ request ->setPort (null );
600
- $ request ->setSecurePort (null );
567
+ $ request ->setPort (0 );
568
+ $ request ->setSecurePort (0 );
601
569
$ request ->setAcceptableContentTypes (null );
602
570
$ request ->setAcceptableLanguages (null );
603
571
@@ -610,7 +578,7 @@ protected function resetRequest(Application $app)
610
578
/**
611
579
* Called before each request, preparation happens here.
612
580
*/
613
- protected function beforeRequest ()
581
+ protected function beforeRequest (): void
614
582
{
615
583
if ($ this ->recreateApplication ) {
616
584
$ this ->resetApplication ($ this ->closeSessionOnRecreateApplication );
@@ -619,6 +587,9 @@ protected function beforeRequest()
619
587
620
588
$ application = $ this ->getApplication ();
621
589
590
+ if (!$ application instanceof Application) {
591
+ throw new ConfigurationException ('Application must be an instance of web application when doing requests ' );
592
+ }
622
593
$ this ->resetResponse ($ application );
623
594
$ this ->resetRequest ($ application );
624
595
0 commit comments