Skip to content

Commit 09da620

Browse files
committed
chore: clean up code for better readability
1 parent 5ff8868 commit 09da620

File tree

2 files changed

+17
-40
lines changed

2 files changed

+17
-40
lines changed

src/Codeception/Lib/Connector/Yii2.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\BrowserKit\CookieJar;
1616
use Symfony\Component\BrowserKit\History;
1717
use Symfony\Component\BrowserKit\Request as BrowserkitRequest;
18+
use yii\web\Request as YiiRequest;
1819
use Symfony\Component\BrowserKit\Response;
1920
use Yii;
2021
use yii\base\Component;
@@ -123,11 +124,11 @@ protected function getApplication(): \yii\base\Application
123124
return Yii::$app ?? throw new \RuntimeException('Failed to create Yii2 application');
124125
}
125126

126-
private function getWebRequest(): Request
127+
private function getWebRequest(): YiiRequest
127128
{
128129
$request = $this->getApplication()->request;
129-
if (!$request instanceof Request) {
130-
throw new \RuntimeException('Request component is not of type ' . Request::class);
130+
if (!$request instanceof YiiRequest) {
131+
throw new \RuntimeException('Request component is not of type ' . YiiRequest::class);
131132
}
132133
return $request;
133134
}
@@ -181,15 +182,11 @@ public function findAndLoginUser(int|string|IdentityInterface $user): void
181182
*/
182183
public function hashCookieData(string $name, string $value): string
183184
{
184-
$app = $this->getApplication();
185-
$request = $app->getRequest();
186-
if (!$request instanceof Request) {
187-
throw new \RuntimeException("Can't do cookie operations on non-web requests");
188-
}
185+
$request = $this->getWebRequest();
189186
if (!$request->enableCookieValidation) {
190187
return $value;
191188
}
192-
return $app->security->hashData(serialize([$name, $value]), $request->cookieValidationKey);
189+
return $this->getApplication()->security->hashData(serialize([$name, $value]), $request->cookieValidationKey);
193190
}
194191

195192
/**
@@ -367,9 +364,9 @@ public function doRequest(object $request): Response
367364
* Sending the response is problematic because it tries to send headers.
368365
*/
369366
$app->trigger($app::EVENT_BEFORE_REQUEST);
370-
$response = $app->handleRequest($yiiRequest);
367+
$yiiResponse = $app->handleRequest($yiiRequest);
371368
$app->trigger($app::EVENT_AFTER_REQUEST);
372-
$response->send();
369+
$yiiResponse->send();
373370
} catch (\Exception $e) {
374371
if ($e instanceof UserException) {
375372
// Don't discard output and pass exception handling to Yii to be able
@@ -383,20 +380,20 @@ public function doRequest(object $request): Response
383380
$response = $app->response;
384381
}
385382

386-
$this->encodeCookies($response, $yiiRequest, $app->security);
383+
$this->encodeCookies($yiiResponse, $yiiRequest, $app->security);
387384

388-
if ($response->isRedirection) {
389-
Debug::debug("[Redirect with headers]" . print_r($response->getHeaders()->toArray(), true));
385+
if ($yiiResponse->isRedirection) {
386+
Debug::debug("[Redirect with headers]" . print_r($yiiResponse->getHeaders()->toArray(), true));
390387
}
391388

392389
$content = ob_get_clean();
393-
if (empty($content) && !empty($response->content) && !isset($response->stream)) {
390+
if (empty($content) && !empty($yiiResponse->content) && !isset($yiiResponse->stream)) {
394391
throw new \Exception('No content was sent from Yii application');
395392
} elseif ($content === false) {
396393
throw new \Exception('Failed to get output buffer');
397394
}
398395

399-
return new Response($content, $response->statusCode, $response->getHeaders()->toArray());
396+
return new Response($content, $yiiResponse->statusCode, $yiiResponse->getHeaders()->toArray());
400397
}
401398

402399
/**
@@ -405,7 +402,7 @@ public function doRequest(object $request): Response
405402
*/
406403
protected function encodeCookies(
407404
YiiResponse $response,
408-
Request $request,
405+
YiiRequest $request,
409406
Security $security
410407
): void {
411408
if ($request->enableCookieValidation) {
@@ -520,7 +517,7 @@ public function setContext(array $context): void
520517
*/
521518
public function closeSession(): void
522519
{
523-
$app = \Yii::$app;
520+
$app = $this->getApplication();
524521
if ($app instanceof \yii\web\Application && $app->has('session', true)) {
525522
$app->session->close();
526523
}

src/Codeception/Module/Yii2.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\BrowserKit\History;
2323
use Yii;
2424
use yii\base\Security;
25+
use yii\web\Application as WebApplication;
2526
use yii\db\ActiveQueryInterface;
2627
use yii\db\ActiveRecordInterface;
2728
use yii\helpers\Url;
@@ -749,27 +750,6 @@ public function amOnRoute(string $route, array $params = []): void
749750
$this->amOnPage(Url::to($params));
750751
}
751752

752-
/**
753-
* Gets a component from the Yii container. Throws an exception if the
754-
* component is not available
755-
*
756-
* ```php
757-
* <?php
758-
* $mailer = $I->grabComponent('mailer');
759-
* ```
760-
*
761-
* @throws \Codeception\Exception\ModuleException
762-
* @deprecated in your tests you can use \Yii::$app directly.
763-
*/
764-
public function grabComponent(string $component): null|object
765-
{
766-
try {
767-
return $this->getClient()->getComponent($component);
768-
} catch (ConfigurationException $e) {
769-
throw new ModuleException($this, $e->getMessage());
770-
}
771-
}
772-
773753
/**
774754
* Checks that an email is sent.
775755
*
@@ -913,7 +893,7 @@ public function _initializeSession(): void
913893
*/
914894
public function _backupSession(): array
915895
{
916-
if (Yii::$app instanceof Application && Yii::$app->has('session', true) && Yii::$app->session->useCustomStorage) {
896+
if (Yii::$app instanceof WebApplication && Yii::$app->has('session', true) && Yii::$app->session->useCustomStorage) {
917897
throw new ModuleException($this, "Yii2 MultiSession only supports the default session backend.");
918898
}
919899
return [

0 commit comments

Comments
 (0)