15
15
use Codeception \Lib \Interfaces \ActiveRecord ;
16
16
use Codeception \Lib \Interfaces \PartedModule ;
17
17
use Codeception \TestInterface ;
18
+ use Exception ;
18
19
use PHPUnit \Framework \Assert ;
19
20
use ReflectionClass ;
20
21
use RuntimeException ;
@@ -276,7 +277,9 @@ public function _initialize(): void
276
277
277
278
$ this ->defineConstants ();
278
279
$ 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 ()];
280
283
}
281
284
282
285
/**
@@ -294,26 +297,27 @@ protected function onReconfigure(): void
294
297
}
295
298
296
299
/**
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
+ * }
300
307
*/
301
- private function initServerGlobal (): void
308
+ private function getServerParams (): array
302
309
{
303
310
$ entryUrl = $ this ->config ['entryUrl ' ];
304
311
$ parsedUrl = parse_url ($ entryUrl );
305
312
$ entryFile = $ this ->config ['entryScript ' ] ?: basename ($ entryUrl );
306
313
$ entryScript = $ this ->config ['entryScript ' ] ?: ($ parsedUrl ['path ' ] ?? '' );
307
- $ _SERVER = array_merge (
308
- $ _SERVER ,
309
- [
314
+ return [
310
315
'SCRIPT_FILENAME ' => $ entryFile ,
311
316
'SCRIPT_NAME ' => $ entryScript ,
312
317
'SERVER_NAME ' => $ parsedUrl ['host ' ] ?? '' ,
313
318
'SERVER_PORT ' => $ parsedUrl ['port ' ] ?? '80 ' ,
314
319
'HTTPS ' => isset ($ parsedUrl ['scheme ' ]) && $ parsedUrl ['scheme ' ] === 'https ' ,
315
- ]
316
- );
320
+ ];
317
321
}
318
322
319
323
/**
@@ -335,23 +339,24 @@ protected function validateConfig(): void
335
339
"The application config file does not exist: " . $ pathToConfig ,
336
340
);
337
341
}
338
- $ validMethods = implode (", " , Yii2Connector::CLEAN_METHODS );
342
+ $ validCleanMethods = implode (", " , Yii2Connector::CLEAN_METHODS );
339
343
if (! in_array ($ this ->config ['responseCleanMethod ' ], Yii2Connector::CLEAN_METHODS , true )) {
340
344
throw new ModuleConfigException (
341
345
self ::class,
342
- "The response clean method must be one of: " . $ validMethods ,
346
+ "The response clean method must be one of: " . $ validCleanMethods ,
343
347
);
344
348
}
349
+ $ validMailMethods = implode (", " , Yii2Connector::MAIL_METHODS );
345
350
if (! in_array ($ this ->config ['mailMethod ' ], Yii2Connector::MAIL_METHODS , true )) {
346
351
throw new ModuleConfigException (
347
352
self ::class,
348
- "The mail method must be one of: " . $ validMethods
353
+ "The mail method must be one of: " . $ validMailMethods
349
354
);
350
355
}
351
356
if (! in_array ($ this ->config ['requestCleanMethod ' ], Yii2Connector::CLEAN_METHODS , true )) {
352
357
throw new ModuleConfigException (
353
358
self ::class,
354
- "The request clean method must be one of: " . $ validMethods ,
359
+ "The request clean method must be one of: " . $ validCleanMethods ,
355
360
);
356
361
}
357
362
}
@@ -377,19 +382,7 @@ private function configureClient(array $settings): void
377
382
*/
378
383
protected function recreateClient (): void
379
384
{
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 ());
393
386
$ this ->validateConfig ();
394
387
$ this ->configureClient ($ this ->config );
395
388
}
@@ -463,7 +456,7 @@ public function _after(TestInterface $test): void
463
456
}
464
457
465
458
/**
466
- * @param \ Exception $fail
459
+ * @param Exception $fail
467
460
*/
468
461
public function _failed (TestInterface $ test , $ fail ): void
469
462
{
0 commit comments