GitHub

@@ -8,6 +8,7 @@

88

use Codeception\Lib\Connector\Yii2\Logger;

99

use Codeception\Lib\Connector\Yii2\TestMailer;

1010

use Codeception\Util\Debug;

11+

use InvalidArgumentException;

1112

use Symfony\Component\BrowserKit\AbstractBrowser as Client;

1213

use Symfony\Component\BrowserKit\Cookie;

1314

use Symfony\Component\BrowserKit\CookieJar;

@@ -114,7 +115,7 @@ public function getApplication(): \yii\base\Application

114115

public function resetApplication(bool $closeSession = true): void

115116

{

116117

codecept_debug('Destroying application');

117-

if (true === $closeSession) {

118+

if ($closeSession) {

118119

$this->closeSession();

119120

}

120121

Yii::$app = null;

@@ -141,13 +142,13 @@ public function findAndLoginUser(int|string|IdentityInterface $user): void

141142

throw new ConfigurationException('The user component is not configured');

142143

}

143144144-

if ($user instanceof \yii\web\IdentityInterface) {

145+

if ($user instanceof IdentityInterface) {

145146

$identity = $user;

146147

} else {

147148

// class name implementing IdentityInterface

148149

$identityClass = $userComponent->identityClass;

149-

$identity = call_user_func([$identityClass, 'findIdentity'], $user);

150-

if (!isset($identity)) {

150+

$identity = $identityClass::findIdentity($user);

151+

if ($identity === null) {

151152

throw new \RuntimeException('User not found');

152153

}

153154

}

@@ -181,7 +182,7 @@ public function getInternalDomains(): array

181182

if ($urlManager->enablePrettyUrl) {

182183

foreach ($urlManager->rules as $rule) {

183184

/** @var \yii\web\UrlRule $rule */

184-

if (isset($rule->host)) {

185+

if ($rule->host !== null) {

185186

$domains[] = $this->getDomainRegex($rule->host);

186187

}

187188

}

@@ -228,12 +229,12 @@ private function getDomainRegex(string $template): string

228229

$template = $matches[1];

229230

}

230231

$parameters = [];

231-

if (strpos($template, '<') !== false) {

232+

if (str_contains($template, '<')) {

232233

$template = preg_replace_callback(

233234

'/<(?:\w+):?([^>]+)?>/u',

234-

function ($matches) use (&$parameters) {

235+

function ($matches) use (&$parameters): string {

235236

$key = '__' . count($parameters) . '__';

236-

$parameters[$key] = isset($matches[1]) ? $matches[1] : '\w+';

237+

$parameters[$key] = $matches[1] ?? '\w+';

237238

return $key;

238239

},

239240

$template

@@ -258,24 +259,18 @@ public function startApp(?\yii\log\Logger $logger = null): void

258259

codecept_debug('Starting application');

259260

$config = require($this->configFile);

260261

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;

266263

}

267264268-

if (isset($config['container']))

269-

{

265+

if (isset($config['container'])) {

270266

Yii::configure(Yii::$container, $config['container']);

271267

unset($config['container']);

272268

}

273269274270

$config = $this->mockMailer($config);

275-

/** @var \yii\base\Application $app */

276271

Yii::$app = Yii::createObject($config);

277272278-

if ($logger !== null) {

273+

if ($logger instanceof \yii\log\Logger) {

279274

Yii::setLogger($logger);

280275

} else {

281276

Yii::setLogger(new Logger());

@@ -326,9 +321,6 @@ public function doRequest(object $request): \Symfony\Component\BrowserKit\Respon

326321

$target->enabled = false;

327322

}

328323329-330-331-332324

$yiiRequest = $app->getRequest();

333325

if ($request->getContent() !== null) {

334326

$yiiRequest->setRawBody($request->getContent());

@@ -441,7 +433,7 @@ protected function mockMailer(array $config): array

441433442434

$mailerConfig = [

443435

'class' => TestMailer::class,

444-

'callback' => function (MessageInterface $message) {

436+

'callback' => function (MessageInterface $message): void {

445437

$this->emails[] = $message;

446438

}

447439

];

@@ -474,7 +466,7 @@ public function getContext(): array

474466

{

475467

return [

476468

'cookieJar' => $this->cookieJar,

477-

'history' => $this->history,

469+

'history' => $this->history,

478470

];

479471

}

480472

@@ -509,11 +501,12 @@ protected function resetResponse(Application $app): void

509501

{

510502

$method = $this->responseCleanMethod;

511503

// 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

517510

) {

518511

Debug::debug(<<<TEXT

519512

[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

525518

$method = self::CLEAN_CLEAR;

526519

}

527520528-

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+

};

539527

}

540528541529

protected function resetRequest(Application $app): void

@@ -555,12 +543,9 @@ protected function resetRequest(Application $app): void

555543

$method = self::CLEAN_CLEAR;

556544

}

557545558-

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 {

564549

$request->getHeaders()->removeAll();

565550

$request->setBaseUrl(null);

566551

$request->setHostInfo(null);

@@ -572,11 +557,10 @@ protected function resetRequest(Application $app): void

572557

$request->setSecurePort(0);

573558

$request->setAcceptableContentTypes(null);

574559

$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+

};

580564

}

581565582566

/**

Read the original on github.com ↗