1515use Symfony \Component \BrowserKit \CookieJar ;
1616use Symfony \Component \BrowserKit \History ;
1717use Symfony \Component \BrowserKit \Request as BrowserkitRequest ;
18- use yii \web \Request as YiiRequest ;
1918use Symfony \Component \BrowserKit \Response ;
2019use Yii ;
2120use yii \base \Component ;
2423use yii \base \Security ;
2524use yii \base \UserException ;
2625use yii \mail \BaseMessage ;
27- use yii \mail \MessageInterface ;
2826use yii \web \Application ;
29- use yii \web \ErrorHandler ;
3027use yii \web \IdentityInterface ;
3128use yii \web \Request ;
29+ use yii \web \Request as YiiRequest ;
3230use yii \web \Response as YiiResponse ;
3331use yii \web \User ;
3432
3533/**
3634 * @extends Client<BrowserkitRequest, Response>
3735 */
38- class Yii2 extends Client
36+ final class Yii2 extends Client
3937{
4038 use Shared \PhpSuperGlobalsConverter;
4139
@@ -45,18 +43,22 @@ class Yii2 extends Client
4543 self ::CLEAN_FORCE_RECREATE ,
4644 self ::CLEAN_MANUAL ,
4745 ];
46+
4847 /**
4948 * Clean the response object by recreating it.
5049 * This might lose behaviors / event handlers / other changes that are done in the application bootstrap phase.
5150 */
5251 public const CLEAN_RECREATE = 'recreate ' ;
52+
5353 /**
5454 * Same as recreate but will not warn when behaviors / event handlers are lost.
5555 */
5656 public const CLEAN_FORCE_RECREATE = 'force_recreate ' ;
57+
5758 /**
5859 * Clean the response object by resetting specific properties via its' `clear()` method.
5960 * This will keep behaviors / event handlers, but could inadvertently leave some changes intact.
61+ *
6062 * @see \yii\web\Response::clear()
6163 */
6264 public const CLEAN_CLEAR = 'clear ' ;
@@ -67,7 +69,6 @@ class Yii2 extends Client
6769 */
6870 public const CLEAN_MANUAL = 'manual ' ;
6971
70-
7172 /**
7273 * @var string application config file
7374 */
@@ -91,6 +92,7 @@ class Yii2 extends Client
9192 /**
9293 * This option is there primarily for backwards compatibility.
9394 * It means you cannot make any modification to application state inside your app, since they will get discarded.
95+ *
9496 * @var bool whether to recreate the whole application before each request
9597 */
9698 public $ recreateApplication = false ;
@@ -106,7 +108,6 @@ class Yii2 extends Client
106108 */
107109 public string |null $ applicationClass = null ;
108110
109-
110111 /**
111112 * @var list<BaseMessage>
112113 */
@@ -117,7 +118,7 @@ class Yii2 extends Client
117118 */
118119 protected function getApplication (): \yii \base \Application
119120 {
120- if (!isset (Yii::$ app )) {
121+ if (! isset (Yii::$ app )) {
121122 $ this ->startApp ();
122123 }
123124 return Yii::$ app ?? throw new \RuntimeException ('Failed to create Yii2 application ' );
@@ -126,7 +127,7 @@ protected function getApplication(): \yii\base\Application
126127 private function getWebRequest (): YiiRequest
127128 {
128129 $ request = $ this ->getApplication ()->request ;
129- if (!$ request instanceof YiiRequest) {
130+ if (! $ request instanceof YiiRequest) {
130131 throw new \RuntimeException ('Request component is not of type ' . YiiRequest::class);
131132 }
132133 return $ request ;
@@ -148,15 +149,16 @@ public function resetApplication(bool $closeSession = true): void
148149
149150 /**
150151 * Finds and logs in a user
152+ *
151153 * @internal
152- * @throws ConfigurationException
153- * @throws \RuntimeException
154+ * @throws ConfigurationException
155+ * @throws \RuntimeException
154156 */
155157 public function findAndLoginUser (int |string |IdentityInterface $ user ): void
156158 {
157159 $ app = $ this ->getApplication ();
158160 $ userComponent = $ app ->get ('user ' );
159- if (!$ userComponent instanceof User) {
161+ if (! $ userComponent instanceof User) {
160162 throw new ConfigurationException ('The user component is not configured ' );
161163 }
162164
@@ -175,22 +177,22 @@ public function findAndLoginUser(int|string|IdentityInterface $user): void
175177
176178 /**
177179 * @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
181183 */
182184 public function hashCookieData (string $ name , string $ value ): string
183185 {
184186 $ request = $ this ->getWebRequest ();
185- if (!$ request ->enableCookieValidation ) {
187+ if (! $ request ->enableCookieValidation ) {
186188 return $ value ;
187189 }
188190 return $ this ->getApplication ()->security ->hashData (serialize ([$ name , $ value ]), $ request ->cookieValidationKey );
189191 }
190192
191193 /**
192194 * @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
194196 */
195197 public function getInternalDomains (): array
196198 {
@@ -199,7 +201,9 @@ public function getInternalDomains(): array
199201 $ domains = [$ this ->getDomainRegex ($ urlManager ->hostInfo )];
200202 if ($ urlManager ->enablePrettyUrl ) {
201203 foreach ($ urlManager ->rules as $ rule ) {
202- /** @var \yii\web\UrlRule $rule */
204+ /**
205+ * @var \yii\web\UrlRule $rule
206+ */
203207 if ($ rule ->host !== null ) {
204208 $ domains [] = $ this ->getDomainRegex ($ rule ->host );
205209 }
@@ -210,7 +214,7 @@ public function getInternalDomains(): array
210214
211215 /**
212216 * @internal
213- * @return list<BaseMessage> List of sent emails
217+ * @return list<BaseMessage> List of sent emails
214218 */
215219 public function getEmails (): array
216220 {
@@ -219,6 +223,7 @@ public function getEmails(): array
219223
220224 /**
221225 * Deletes all stored emails.
226+ *
222227 * @internal
223228 */
224229 public function clearEmails (): void
@@ -233,7 +238,7 @@ public function getComponent(string $name): object|null
233238 {
234239 $ app = $ this ->getApplication ();
235240 $ result = $ app ->get ($ name , false );
236- if (!isset ($ result )) {
241+ if (! isset ($ result )) {
237242 throw new ConfigurationException ("Component $ name is not available in current application " );
238243 }
239244 return $ result ;
@@ -269,6 +274,7 @@ function ($matches) use (&$parameters): string {
269274
270275 /**
271276 * Gets the name of the CSRF param.
277+ *
272278 * @internal
273279 */
274280 public function getCsrfParamName (): string
@@ -279,8 +285,8 @@ public function getCsrfParamName(): string
279285 public function startApp (?\yii \log \Logger $ logger = null ): void
280286 {
281287 codecept_debug ('Starting application ' );
282- $ config = require ( $ this ->configFile ) ;
283- if (!isset ($ config ['class ' ])) {
288+ $ config = include $ this ->configFile ;
289+ if (! isset ($ config ['class ' ])) {
284290 $ config ['class ' ] = $ this ->applicationClass ?? \yii \web \Application::class;
285291 }
286292
@@ -291,7 +297,7 @@ public function startApp(?\yii\log\Logger $logger = null): void
291297
292298 $ config = $ this ->mockMailer ($ config );
293299 $ app = Yii::createObject ($ config );
294- if (!$ app instanceof \yii \base \Application) {
300+ if (! $ app instanceof \yii \base \Application) {
295301 throw new ModuleConfigException ($ this , "Failed to initialize Yii2 app " );
296302 }
297303 \Yii::$ app = $ app ;
@@ -338,7 +344,7 @@ public function doRequest(object $request): Response
338344 $ this ->beforeRequest ();
339345
340346 $ app = $ this ->getApplication ();
341- if (!$ app instanceof Application) {
347+ if (! $ app instanceof Application) {
342348 throw new ConfigurationException ("Application is not a web application " );
343349 }
344350
@@ -372,7 +378,7 @@ public function doRequest(object $request): Response
372378 // to expect error response codes in tests.
373379 $ app ->errorHandler ->discardExistingOutput = false ;
374380 $ app ->errorHandler ->handleException ($ e );
375- } elseif (!$ e instanceof ExitException) {
381+ } elseif (! $ e instanceof ExitException) {
376382 // for exceptions not related to Http, we pass them to Codeception
377383 throw $ e ;
378384 }
@@ -386,7 +392,7 @@ public function doRequest(object $request): Response
386392 }
387393
388394 $ content = ob_get_clean ();
389- if (empty ($ content ) && !empty ($ yiiResponse ->content ) && !isset ($ yiiResponse ->stream )) {
395+ if (empty ($ content ) && ! empty ($ yiiResponse ->content ) && ! isset ($ yiiResponse ->stream )) {
390396 throw new \RuntimeException ('No content was sent from Yii application ' );
391397 } elseif ($ content === false ) {
392398 throw new \RuntimeException ('Failed to get output buffer ' );
@@ -397,6 +403,7 @@ public function doRequest(object $request): Response
397403
398404 /**
399405 * Encodes the cookies and adds them to the headers.
406+ *
400407 * @throws \yii\base\InvalidConfigException
401408 */
402409 protected function encodeCookies (
@@ -409,7 +416,9 @@ protected function encodeCookies(
409416 }
410417
411418 foreach ($ response ->getCookies () as $ cookie ) {
412- /** @var \yii\web\Cookie $cookie */
419+ /**
420+ * @var \yii\web\Cookie $cookie
421+ */
413422 $ value = $ cookie ->value ;
414423 // Expire = 1 means we're removing the cookie
415424 if ($ cookie ->expire !== 1 && isset ($ validationKey )) {
@@ -434,7 +443,8 @@ protected function encodeCookies(
434443
435444 /**
436445 * Replace mailer with in memory mailer
437- * @param array<string, mixed> $config Original configuration
446+ *
447+ * @param array<string, mixed> $config Original configuration
438448 * @return array<string, mixed> New configuration
439449 */
440450 protected function mockMailer (array $ config ): array
@@ -460,7 +470,7 @@ protected function mockMailer(array $config): array
460470 ];
461471
462472 if (isset ($ config ['components ' ])) {
463- if (!is_array ($ config ['components ' ])) {
473+ if (! is_array ($ config ['components ' ])) {
464474 throw new ModuleConfigException (
465475 $ this ,
466476 "Yii2 config does not contain components key is not of type array " ,
@@ -491,13 +501,13 @@ public function restart(): void
491501 * Return an assoc array with the client context: cookieJar, history.
492502 *
493503 * @internal
494- * @return array{ cookieJar: CookieJar, history: History }
504+ * @return array{ cookieJar: CookieJar, history: History }
495505 */
496506 public function getContext (): array
497507 {
498508 return [
499509 'cookieJar ' => $ this ->cookieJar ,
500- 'history ' => $ this ->history ,
510+ 'history ' => $ this ->history ,
501511 ];
502512 }
503513
@@ -514,6 +524,7 @@ public function setContext(array $context): void
514524
515525 /**
516526 * This functions closes the session of the application, if the application exists and has a session.
527+ *
517528 * @internal
518529 */
519530 public function closeSession (): void
@@ -532,11 +543,10 @@ protected function resetResponse(Application $app): void
532543 {
533544 $ method = $ this ->responseCleanMethod ;
534545 // 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 )
540550 && $ method === self ::CLEAN_RECREATE
541551 ) {
542552 Debug::debug (
@@ -608,7 +618,7 @@ protected function beforeRequest(): void
608618
609619 $ application = $ this ->getApplication ();
610620
611- if (!$ application instanceof Application) {
621+ if (! $ application instanceof Application) {
612622 throw new ConfigurationException ('Application must be an instance of web application when doing requests ' );
613623 }
614624 $ this ->resetResponse ($ application );
0 commit comments