15
15
use Symfony \Component \BrowserKit \CookieJar ;
16
16
use Symfony \Component \BrowserKit \History ;
17
17
use Symfony \Component \BrowserKit \Request as BrowserkitRequest ;
18
- use yii \web \Request as YiiRequest ;
19
18
use Symfony \Component \BrowserKit \Response ;
20
19
use Yii ;
21
20
use yii \base \Component ;
24
23
use yii \base \Security ;
25
24
use yii \base \UserException ;
26
25
use yii \mail \BaseMessage ;
27
- use yii \mail \MessageInterface ;
28
26
use yii \web \Application ;
29
- use yii \web \ErrorHandler ;
30
27
use yii \web \IdentityInterface ;
31
28
use yii \web \Request ;
29
+ use yii \web \Request as YiiRequest ;
32
30
use yii \web \Response as YiiResponse ;
33
31
use yii \web \User ;
34
32
35
33
/**
36
34
* @extends Client<BrowserkitRequest, Response>
37
35
*/
38
- class Yii2 extends Client
36
+ final class Yii2 extends Client
39
37
{
40
38
use Shared \PhpSuperGlobalsConverter;
41
39
@@ -45,18 +43,22 @@ class Yii2 extends Client
45
43
self ::CLEAN_FORCE_RECREATE ,
46
44
self ::CLEAN_MANUAL ,
47
45
];
46
+
48
47
/**
49
48
* Clean the response object by recreating it.
50
49
* This might lose behaviors / event handlers / other changes that are done in the application bootstrap phase.
51
50
*/
52
51
public const CLEAN_RECREATE = 'recreate ' ;
52
+
53
53
/**
54
54
* Same as recreate but will not warn when behaviors / event handlers are lost.
55
55
*/
56
56
public const CLEAN_FORCE_RECREATE = 'force_recreate ' ;
57
+
57
58
/**
58
59
* Clean the response object by resetting specific properties via its' `clear()` method.
59
60
* This will keep behaviors / event handlers, but could inadvertently leave some changes intact.
61
+ *
60
62
* @see \yii\web\Response::clear()
61
63
*/
62
64
public const CLEAN_CLEAR = 'clear ' ;
@@ -67,7 +69,6 @@ class Yii2 extends Client
67
69
*/
68
70
public const CLEAN_MANUAL = 'manual ' ;
69
71
70
-
71
72
/**
72
73
* @var string application config file
73
74
*/
@@ -91,6 +92,7 @@ class Yii2 extends Client
91
92
/**
92
93
* This option is there primarily for backwards compatibility.
93
94
* It means you cannot make any modification to application state inside your app, since they will get discarded.
95
+ *
94
96
* @var bool whether to recreate the whole application before each request
95
97
*/
96
98
public $ recreateApplication = false ;
@@ -106,7 +108,6 @@ class Yii2 extends Client
106
108
*/
107
109
public string |null $ applicationClass = null ;
108
110
109
-
110
111
/**
111
112
* @var list<BaseMessage>
112
113
*/
@@ -117,7 +118,7 @@ class Yii2 extends Client
117
118
*/
118
119
protected function getApplication (): \yii \base \Application
119
120
{
120
- if (!isset (Yii::$ app )) {
121
+ if (! isset (Yii::$ app )) {
121
122
$ this ->startApp ();
122
123
}
123
124
return Yii::$ app ?? throw new \RuntimeException ('Failed to create Yii2 application ' );
@@ -126,7 +127,7 @@ protected function getApplication(): \yii\base\Application
126
127
private function getWebRequest (): YiiRequest
127
128
{
128
129
$ request = $ this ->getApplication ()->request ;
129
- if (!$ request instanceof YiiRequest) {
130
+ if (! $ request instanceof YiiRequest) {
130
131
throw new \RuntimeException ('Request component is not of type ' . YiiRequest::class);
131
132
}
132
133
return $ request ;
@@ -148,15 +149,16 @@ public function resetApplication(bool $closeSession = true): void
148
149
149
150
/**
150
151
* Finds and logs in a user
152
+ *
151
153
* @internal
152
- * @throws ConfigurationException
153
- * @throws \RuntimeException
154
+ * @throws ConfigurationException
155
+ * @throws \RuntimeException
154
156
*/
155
157
public function findAndLoginUser (int |string |IdentityInterface $ user ): void
156
158
{
157
159
$ app = $ this ->getApplication ();
158
160
$ userComponent = $ app ->get ('user ' );
159
- if (!$ userComponent instanceof User) {
161
+ if (! $ userComponent instanceof User) {
160
162
throw new ConfigurationException ('The user component is not configured ' );
161
163
}
162
164
@@ -175,22 +177,22 @@ public function findAndLoginUser(int|string|IdentityInterface $user): void
175
177
176
178
/**
177
179
* @internal
178
- * @param string $name The name of the cookie
179
- * @param string $value The value of the cookie
180
- * @return string The value to send to the browser
180
+ * @param string $name The name of the cookie
181
+ * @param string $value The value of the cookie
182
+ * @return string The value to send to the browser
181
183
*/
182
184
public function hashCookieData (string $ name , string $ value ): string
183
185
{
184
186
$ request = $ this ->getWebRequest ();
185
- if (!$ request ->enableCookieValidation ) {
187
+ if (! $ request ->enableCookieValidation ) {
186
188
return $ value ;
187
189
}
188
190
return $ this ->getApplication ()->security ->hashData (serialize ([$ name , $ value ]), $ request ->cookieValidationKey );
189
191
}
190
192
191
193
/**
192
194
* @internal
193
- * @return non-empty-list<string> List of regex patterns for recognized domain names
195
+ * @return non-empty-list<string> List of regex patterns for recognized domain names
194
196
*/
195
197
public function getInternalDomains (): array
196
198
{
@@ -199,7 +201,9 @@ public function getInternalDomains(): array
199
201
$ domains = [$ this ->getDomainRegex ($ urlManager ->hostInfo )];
200
202
if ($ urlManager ->enablePrettyUrl ) {
201
203
foreach ($ urlManager ->rules as $ rule ) {
202
- /** @var \yii\web\UrlRule $rule */
204
+ /**
205
+ * @var \yii\web\UrlRule $rule
206
+ */
203
207
if ($ rule ->host !== null ) {
204
208
$ domains [] = $ this ->getDomainRegex ($ rule ->host );
205
209
}
@@ -210,7 +214,7 @@ public function getInternalDomains(): array
210
214
211
215
/**
212
216
* @internal
213
- * @return list<BaseMessage> List of sent emails
217
+ * @return list<BaseMessage> List of sent emails
214
218
*/
215
219
public function getEmails (): array
216
220
{
@@ -219,6 +223,7 @@ public function getEmails(): array
219
223
220
224
/**
221
225
* Deletes all stored emails.
226
+ *
222
227
* @internal
223
228
*/
224
229
public function clearEmails (): void
@@ -233,7 +238,7 @@ public function getComponent(string $name): object|null
233
238
{
234
239
$ app = $ this ->getApplication ();
235
240
$ result = $ app ->get ($ name , false );
236
- if (!isset ($ result )) {
241
+ if (! isset ($ result )) {
237
242
throw new ConfigurationException ("Component $ name is not available in current application " );
238
243
}
239
244
return $ result ;
@@ -269,6 +274,7 @@ function ($matches) use (&$parameters): string {
269
274
270
275
/**
271
276
* Gets the name of the CSRF param.
277
+ *
272
278
* @internal
273
279
*/
274
280
public function getCsrfParamName (): string
@@ -279,8 +285,8 @@ public function getCsrfParamName(): string
279
285
public function startApp (?\yii \log \Logger $ logger = null ): void
280
286
{
281
287
codecept_debug ('Starting application ' );
282
- $ config = require ( $ this ->configFile ) ;
283
- if (!isset ($ config ['class ' ])) {
288
+ $ config = include $ this ->configFile ;
289
+ if (! isset ($ config ['class ' ])) {
284
290
$ config ['class ' ] = $ this ->applicationClass ?? \yii \web \Application::class;
285
291
}
286
292
@@ -291,7 +297,7 @@ public function startApp(?\yii\log\Logger $logger = null): void
291
297
292
298
$ config = $ this ->mockMailer ($ config );
293
299
$ app = Yii::createObject ($ config );
294
- if (!$ app instanceof \yii \base \Application) {
300
+ if (! $ app instanceof \yii \base \Application) {
295
301
throw new ModuleConfigException ($ this , "Failed to initialize Yii2 app " );
296
302
}
297
303
\Yii::$ app = $ app ;
@@ -338,7 +344,7 @@ public function doRequest(object $request): Response
338
344
$ this ->beforeRequest ();
339
345
340
346
$ app = $ this ->getApplication ();
341
- if (!$ app instanceof Application) {
347
+ if (! $ app instanceof Application) {
342
348
throw new ConfigurationException ("Application is not a web application " );
343
349
}
344
350
@@ -372,7 +378,7 @@ public function doRequest(object $request): Response
372
378
// to expect error response codes in tests.
373
379
$ app ->errorHandler ->discardExistingOutput = false ;
374
380
$ app ->errorHandler ->handleException ($ e );
375
- } elseif (!$ e instanceof ExitException) {
381
+ } elseif (! $ e instanceof ExitException) {
376
382
// for exceptions not related to Http, we pass them to Codeception
377
383
throw $ e ;
378
384
}
@@ -386,7 +392,7 @@ public function doRequest(object $request): Response
386
392
}
387
393
388
394
$ content = ob_get_clean ();
389
- if (empty ($ content ) && !empty ($ yiiResponse ->content ) && !isset ($ yiiResponse ->stream )) {
395
+ if (empty ($ content ) && ! empty ($ yiiResponse ->content ) && ! isset ($ yiiResponse ->stream )) {
390
396
throw new \RuntimeException ('No content was sent from Yii application ' );
391
397
} elseif ($ content === false ) {
392
398
throw new \RuntimeException ('Failed to get output buffer ' );
@@ -397,6 +403,7 @@ public function doRequest(object $request): Response
397
403
398
404
/**
399
405
* Encodes the cookies and adds them to the headers.
406
+ *
400
407
* @throws \yii\base\InvalidConfigException
401
408
*/
402
409
protected function encodeCookies (
@@ -409,7 +416,9 @@ protected function encodeCookies(
409
416
}
410
417
411
418
foreach ($ response ->getCookies () as $ cookie ) {
412
- /** @var \yii\web\Cookie $cookie */
419
+ /**
420
+ * @var \yii\web\Cookie $cookie
421
+ */
413
422
$ value = $ cookie ->value ;
414
423
// Expire = 1 means we're removing the cookie
415
424
if ($ cookie ->expire !== 1 && isset ($ validationKey )) {
@@ -434,7 +443,8 @@ protected function encodeCookies(
434
443
435
444
/**
436
445
* Replace mailer with in memory mailer
437
- * @param array<string, mixed> $config Original configuration
446
+ *
447
+ * @param array<string, mixed> $config Original configuration
438
448
* @return array<string, mixed> New configuration
439
449
*/
440
450
protected function mockMailer (array $ config ): array
@@ -460,7 +470,7 @@ protected function mockMailer(array $config): array
460
470
];
461
471
462
472
if (isset ($ config ['components ' ])) {
463
- if (!is_array ($ config ['components ' ])) {
473
+ if (! is_array ($ config ['components ' ])) {
464
474
throw new ModuleConfigException (
465
475
$ this ,
466
476
"Yii2 config does not contain components key is not of type array " ,
@@ -491,13 +501,13 @@ public function restart(): void
491
501
* Return an assoc array with the client context: cookieJar, history.
492
502
*
493
503
* @internal
494
- * @return array{ cookieJar: CookieJar, history: History }
504
+ * @return array{ cookieJar: CookieJar, history: History }
495
505
*/
496
506
public function getContext (): array
497
507
{
498
508
return [
499
509
'cookieJar ' => $ this ->cookieJar ,
500
- 'history ' => $ this ->history ,
510
+ 'history ' => $ this ->history ,
501
511
];
502
512
}
503
513
@@ -514,6 +524,7 @@ public function setContext(array $context): void
514
524
515
525
/**
516
526
* This functions closes the session of the application, if the application exists and has a session.
527
+ *
517
528
* @internal
518
529
*/
519
530
public function closeSession (): void
@@ -532,11 +543,10 @@ protected function resetResponse(Application $app): void
532
543
{
533
544
$ method = $ this ->responseCleanMethod ;
534
545
// First check the current response object.
535
- if (
536
- ($ app ->response ->hasEventHandlers (YiiResponse::EVENT_BEFORE_SEND )
537
- || $ app ->response ->hasEventHandlers (YiiResponse::EVENT_AFTER_SEND )
538
- || $ app ->response ->hasEventHandlers (YiiResponse::EVENT_AFTER_PREPARE )
539
- || count ($ app ->response ->getBehaviors ()) > 0 )
546
+ if (($ app ->response ->hasEventHandlers (YiiResponse::EVENT_BEFORE_SEND )
547
+ || $ app ->response ->hasEventHandlers (YiiResponse::EVENT_AFTER_SEND )
548
+ || $ app ->response ->hasEventHandlers (YiiResponse::EVENT_AFTER_PREPARE )
549
+ || count ($ app ->response ->getBehaviors ()) > 0 )
540
550
&& $ method === self ::CLEAN_RECREATE
541
551
) {
542
552
Debug::debug (
@@ -608,7 +618,7 @@ protected function beforeRequest(): void
608
618
609
619
$ application = $ this ->getApplication ();
610
620
611
- if (!$ application instanceof Application) {
621
+ if (! $ application instanceof Application) {
612
622
throw new ConfigurationException ('Application must be an instance of web application when doing requests ' );
613
623
}
614
624
$ this ->resetResponse ($ application );
0 commit comments