gpt4 book ai didi

yii2 - Codeception功能测试中的假IP

转载 作者:行者123 更新时间:2023-12-04 13:45:50 25 4
gpt4 key购买 nike

我正在使用 Yii2 和 Codeception 来运行测试。

当我运行 功能测试,$SERVER['REMOTE_ADDR']没有定义。我如何模拟要在这些测试中使用的 ip。

我试过 $SERVER['REMOTE_ADDR'] = '127.0.0.1' ,但它不起作用。

最佳答案

昨天我遇到了这样的问题,我找到了解决方案!所以,我的解决方案与 Yii2 框架有关,但我认为您可以根据自己的环境修改它。

  • 扩展你的代码接收模块(Yii2 对我来说):

  • <?php

    namespace codeception;

    use Codeception\Module\Yii2;

    class CustomModule extends Yii2
    {
    protected function recreateClient()
    {
    parent::recreateClient();

    if ($ip = ($this->config['ip'] ?? null)) {
    $this->client->setServerParameter('REMOTE_ADDR', $ip);
    }
    }
    }

  • 转到 codeception.yaml 配置并设置您的 ip :

  • actor: MyTester
    modules:
    enabled:
    - \Helper\E2eCommon
    - Asserts
    - Mockery
    # here
    - \codeception\CustomModule:
    configFile: 'codeception/_config/config.php'
    - REST:
    # here
    depends: \codeception\CustomModule
    part: Json
    config:
    # and here!
    \codeception\CustomModule:
    ip: 192.168.1.1 # ip mock here!

    extensions:
    enabled:
    - \ResetYiiContainerExtension

    就这样!请享用 :)

    关于yii2 - Codeception功能测试中的假IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48773938/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com