8
8
use Codeception \Lib \Connector \Yii2 \Logger ;
9
9
use Codeception \Lib \Connector \Yii2 \TestMailer ;
10
10
use Codeception \Util \Debug ;
11
+ use InvalidArgumentException ;
11
12
use Symfony \Component \BrowserKit \AbstractBrowser as Client ;
12
13
use Symfony \Component \BrowserKit \Cookie ;
13
14
use Symfony \Component \BrowserKit \CookieJar ;
@@ -114,7 +115,7 @@ public function getApplication(): \yii\base\Application
114
115
public function resetApplication (bool $ closeSession = true ): void
115
116
{
116
117
codecept_debug ('Destroying application ' );
117
- if (true === $ closeSession ) {
118
+ if ($ closeSession ) {
118
119
$ this ->closeSession ();
119
120
}
120
121
Yii::$ app = null ;
@@ -141,13 +142,13 @@ public function findAndLoginUser(int|string|IdentityInterface $user): void
141
142
throw new ConfigurationException ('The user component is not configured ' );
142
143
}
143
144
144
- if ($ user instanceof \ yii \ web \ IdentityInterface) {
145
+ if ($ user instanceof IdentityInterface) {
145
146
$ identity = $ user ;
146
147
} else {
147
148
// class name implementing IdentityInterface
148
149
$ identityClass = $ userComponent ->identityClass ;
149
- $ identity = call_user_func ([ $ identityClass, ' findIdentity ' ], $ user );
150
- if (! isset ( $ identity) ) {
150
+ $ identity = $ identityClass:: findIdentity ( $ user );
151
+ if ($ identity === null ) {
151
152
throw new \RuntimeException ('User not found ' );
152
153
}
153
154
}
@@ -181,7 +182,7 @@ public function getInternalDomains(): array
181
182
if ($ urlManager ->enablePrettyUrl ) {
182
183
foreach ($ urlManager ->rules as $ rule ) {
183
184
/** @var \yii\web\UrlRule $rule */
184
- if (isset ( $ rule ->host ) ) {
185
+ if ($ rule ->host !== null ) {
185
186
$ domains [] = $ this ->getDomainRegex ($ rule ->host );
186
187
}
187
188
}
@@ -228,12 +229,12 @@ private function getDomainRegex(string $template): string
228
229
$ template = $ matches [1 ];
229
230
}
230
231
$ parameters = [];
231
- if (strpos ($ template , '< ' ) !== false ) {
232
+ if (str_contains ($ template , '< ' )) {
232
233
$ template = preg_replace_callback (
233
234
'/<(?:\w+):?([^>]+)?>/u ' ,
234
- function ($ matches ) use (&$ parameters ) {
235
+ function ($ matches ) use (&$ parameters ): string {
235
236
$ key = '__ ' . count ($ parameters ) . '__ ' ;
236
- $ parameters [$ key ] = isset ( $ matches [1 ]) ? $ matches [ 1 ] : '\w+ ' ;
237
+ $ parameters [$ key ] = $ matches [1 ] ?? '\w+ ' ;
237
238
return $ key ;
238
239
},
239
240
$ template
@@ -258,24 +259,18 @@ public function startApp(?\yii\log\Logger $logger = null): void
258
259
codecept_debug ('Starting application ' );
259
260
$ config = require ($ this ->configFile );
260
261
if (!isset ($ config ['class ' ])) {
261
- if (null !== $ this ->applicationClass ) {
262
- $ config ['class ' ] = $ this ->applicationClass ;
263
- } else {
264
- $ config ['class ' ] = 'yii\web\Application ' ;
265
- }
262
+ $ config ['class ' ] = $ this ->applicationClass ?? \yii \web \Application::class;
266
263
}
267
264
268
- if (isset ($ config ['container ' ]))
269
- {
265
+ if (isset ($ config ['container ' ])) {
270
266
Yii::configure (Yii::$ container , $ config ['container ' ]);
271
267
unset($ config ['container ' ]);
272
268
}
273
269
274
270
$ config = $ this ->mockMailer ($ config );
275
- /** @var \yii\base\Application $app */
276
271
Yii::$ app = Yii::createObject ($ config );
277
272
278
- if ($ logger !== null ) {
273
+ if ($ logger instanceof \ yii \ log \Logger ) {
279
274
Yii::setLogger ($ logger );
280
275
} else {
281
276
Yii::setLogger (new Logger ());
@@ -326,9 +321,6 @@ public function doRequest(object $request): \Symfony\Component\BrowserKit\Respon
326
321
$ target ->enabled = false ;
327
322
}
328
323
329
-
330
-
331
-
332
324
$ yiiRequest = $ app ->getRequest ();
333
325
if ($ request ->getContent () !== null ) {
334
326
$ yiiRequest ->setRawBody ($ request ->getContent ());
@@ -441,7 +433,7 @@ protected function mockMailer(array $config): array
441
433
442
434
$ mailerConfig = [
443
435
'class ' => TestMailer::class,
444
- 'callback ' => function (MessageInterface $ message ) {
436
+ 'callback ' => function (MessageInterface $ message ): void {
445
437
$ this ->emails [] = $ message ;
446
438
}
447
439
];
@@ -474,7 +466,7 @@ public function getContext(): array
474
466
{
475
467
return [
476
468
'cookieJar ' => $ this ->cookieJar ,
477
- 'history ' => $ this ->history ,
469
+ 'history ' => $ this ->history ,
478
470
];
479
471
}
480
472
@@ -509,11 +501,12 @@ protected function resetResponse(Application $app): void
509
501
{
510
502
$ method = $ this ->responseCleanMethod ;
511
503
// First check the current response object.
512
- if (($ app ->response ->hasEventHandlers (\yii \web \Response::EVENT_BEFORE_SEND )
513
- || $ app ->response ->hasEventHandlers (\yii \web \Response::EVENT_AFTER_SEND )
514
- || $ app ->response ->hasEventHandlers (\yii \web \Response::EVENT_AFTER_PREPARE )
515
- || count ($ app ->response ->getBehaviors ()) > 0
516
- ) && $ method === self ::CLEAN_RECREATE
504
+ if (
505
+ ($ app ->response ->hasEventHandlers (YiiResponse::EVENT_BEFORE_SEND )
506
+ || $ app ->response ->hasEventHandlers (YiiResponse::EVENT_AFTER_SEND )
507
+ || $ app ->response ->hasEventHandlers (YiiResponse::EVENT_AFTER_PREPARE )
508
+ || count ($ app ->response ->getBehaviors ()) > 0 )
509
+ && $ method === self ::CLEAN_RECREATE
517
510
) {
518
511
Debug::debug (<<<TEXT
519
512
[WARNING] You are attaching event handlers or behaviors to the response object. But the Yii2 module is configured to recreate
@@ -525,17 +518,12 @@ protected function resetResponse(Application $app): void
525
518
$ method = self ::CLEAN_CLEAR ;
526
519
}
527
520
528
- switch ($ method ) {
529
- case self ::CLEAN_FORCE_RECREATE :
530
- case self ::CLEAN_RECREATE :
531
- $ app ->set ('response ' , $ app ->getComponents ()['response ' ]);
532
- break ;
533
- case self ::CLEAN_CLEAR :
534
- $ app ->response ->clear ();
535
- break ;
536
- case self ::CLEAN_MANUAL :
537
- break ;
538
- }
521
+ match ($ method ) {
522
+ self ::CLEAN_FORCE_RECREATE , self ::CLEAN_RECREATE => $ app ->set ('response ' , $ app ->getComponents ()['response ' ]),
523
+ self ::CLEAN_CLEAR => $ app ->response ->clear (),
524
+ self ::CLEAN_MANUAL => null ,
525
+ default => throw new InvalidArgumentException ("Unknown method: $ method " ),
526
+ };
539
527
}
540
528
541
529
protected function resetRequest (Application $ app ): void
@@ -555,12 +543,9 @@ protected function resetRequest(Application $app): void
555
543
$ method = self ::CLEAN_CLEAR ;
556
544
}
557
545
558
- switch ($ method ) {
559
- case self ::CLEAN_FORCE_RECREATE :
560
- case self ::CLEAN_RECREATE :
561
- $ app ->set ('request ' , $ app ->getComponents ()['request ' ]);
562
- break ;
563
- case self ::CLEAN_CLEAR :
546
+ match ($ method ) {
547
+ self ::CLEAN_FORCE_RECREATE , self ::CLEAN_RECREATE => $ app ->set ('request ' , $ app ->getComponents ()['request ' ]),
548
+ self ::CLEAN_CLEAR => (function () use ($ request ): void {
564
549
$ request ->getHeaders ()->removeAll ();
565
550
$ request ->setBaseUrl (null );
566
551
$ request ->setHostInfo (null );
@@ -572,11 +557,10 @@ protected function resetRequest(Application $app): void
572
557
$ request ->setSecurePort (0 );
573
558
$ request ->setAcceptableContentTypes (null );
574
559
$ request ->setAcceptableLanguages (null );
575
-
576
- break ;
577
- case self ::CLEAN_MANUAL :
578
- break ;
579
- }
560
+ })(),
561
+ self ::CLEAN_MANUAL => null ,
562
+ default => throw new InvalidArgumentException ("Unknown method: $ method " ),
563
+ };
580
564
}
581
565
582
566
/**
0 commit comments