@@ -15,6 +15,7 @@
1515use Codeception\Lib\Interfaces\ActiveRecord;
1616use Codeception\Lib\Interfaces\PartedModule;
1717use Codeception\TestInterface;
18+use Exception;
1819use PHPUnit\Framework\Assert;
1920use ReflectionClass;
2021use RuntimeException;
@@ -276,7 +277,9 @@ public function _initialize(): void
276277277278$this->defineConstants();
278279$this->server = $_SERVER;
279-$this->initServerGlobal();
280+// Adds the required server params. Note this is done separately from the request cycle since someone might call
281+// `Url::to` before doing a request, which would instantiate the request component with incorrect server params.
282+$_SERVER = [...$_SERVER, $this->getServerParams()];
280283 }
281284282285/**
@@ -294,26 +297,27 @@ protected function onReconfigure(): void
294297 }
295298296299/**
297- * Adds the required server params.
298- * Note this is done separately from the request cycle since someone might call
299- * `Url::to` before doing a request, which would instantiate the request component with incorrect server params.
300+ * @return array{
301+ * SCRIPT_FILENAME: string,
302+ * SCRIPT_NAME: string,
303+ * SERVER_NAME: string,
304+ * SERVER_PORT: string|int,
305+ * HTTPS: bool
306+ * }
300307 */
301-private function initServerGlobal(): void
308+private function getServerParams(): array
302309 {
303310$entryUrl = $this->config['entryUrl'];
304311$parsedUrl = parse_url($entryUrl);
305312$entryFile = $this->config['entryScript'] ?: basename($entryUrl);
306313$entryScript = $this->config['entryScript'] ?: ($parsedUrl['path'] ?? '');
307-$_SERVER = array_merge(
308-$_SERVER,
309- [
314+return [
310315'SCRIPT_FILENAME' => $entryFile,
311316'SCRIPT_NAME' => $entryScript,
312317'SERVER_NAME' => $parsedUrl['host'] ?? '',
313318'SERVER_PORT' => $parsedUrl['port'] ?? '80',
314319'HTTPS' => isset($parsedUrl['scheme']) && $parsedUrl['scheme'] === 'https',
315- ]
316- );
320+ ];
317321 }
318322319323/**
@@ -335,23 +339,24 @@ protected function validateConfig(): void
335339"The application config file does not exist: " . $pathToConfig,
336340 );
337341 }
338-$validMethods = implode(", ", Yii2Connector::CLEAN_METHODS);
342+$validCleanMethods = implode(", ", Yii2Connector::CLEAN_METHODS);
339343if (! in_array($this->config['responseCleanMethod'], Yii2Connector::CLEAN_METHODS, true)) {
340344throw new ModuleConfigException(
341345self::class,
342-"The response clean method must be one of: " . $validMethods,
346+"The response clean method must be one of: " . $validCleanMethods,
343347 );
344348 }
349+$validMailMethods = implode(", ", Yii2Connector::MAIL_METHODS);
345350if (! in_array($this->config['mailMethod'], Yii2Connector::MAIL_METHODS, true)) {
346351throw new ModuleConfigException(
347352self::class,
348-"The mail method must be one of: " . $validMethods
353+"The mail method must be one of: " . $validMailMethods
349354 );
350355 }
351356if (! in_array($this->config['requestCleanMethod'], Yii2Connector::CLEAN_METHODS, true)) {
352357throw new ModuleConfigException(
353358self::class,
354-"The request clean method must be one of: " . $validMethods,
359+"The request clean method must be one of: " . $validCleanMethods,
355360 );
356361 }
357362 }
@@ -377,19 +382,7 @@ private function configureClient(array $settings): void
377382 */
378383protected function recreateClient(): void
379384 {
380-$entryUrl = $this->config['entryUrl'];
381-$parsedUrl = parse_url($entryUrl);
382-$entryFile = $this->config['entryScript'] ?: basename($entryUrl);
383-$entryScript = $this->config['entryScript'] ?: ($parsedUrl['path'] ?? '');
384-$this->client = new Yii2Connector(
385- [
386-'SCRIPT_FILENAME' => $entryFile,
387-'SCRIPT_NAME' => $entryScript,
388-'SERVER_NAME' => $parsedUrl['host'] ?? '',
389-'SERVER_PORT' => $parsedUrl['port'] ?? '80',
390-'HTTPS' => isset($parsedUrl['scheme']) && $parsedUrl['scheme'] === 'https',
391- ]
392- );
385+$this->client = new Yii2Connector($this->getServerParams());
393386$this->validateConfig();
394387$this->configureClient($this->config);
395388 }
@@ -463,7 +456,7 @@ public function _after(TestInterface $test): void
463456 }
464457465458/**
466- * @param \Exception $fail
459+ * @param Exception $fail
467460 */
468461public function _failed(TestInterface $test, $fail): void
469462 {