【问题标题】:Yii2 build-in server starts from index.phpYii2 内置服务器从 index.php 开始
【发布时间】:2018-11-16 23:24:03
【问题描述】:

我正在尝试根据https://github.com/yiisoft/yii2-app-basic/blob/master/README.md#testing执行codeception验收测试

我不明白为什么 yii serve 以 entryScript index.php 开头,而我期望 index-test.php。这会导致 YII_DEBUG = false 并因此无法将电子邮件保存到文件中。

这是我的 codeception.yml

actor: Tester
paths:
    tests: tests
    log: tests/_output
    data: tests/_data
    helpers: tests/_support
coverage:
    enabled: true
    remote: false
    c3_url: 'http://localhost:8080/index-test.php'
    include:
      - commands/*
      - components/*
      - controllers/*
      - models/*
      - modules/*
settings:
    bootstrap: _bootstrap.php
    memory_limit: 1024M
    colors: true
modules:
    config:
        Yii2:
            configFile: 'config/test.php'
            entryScript: index-test.php
            cleanup: false

这是我的acceptance.yml:

actor: AcceptanceTester
extensions:
    enabled:
        - Codeception\Extension\RunProcess:
            - ./tests/bin/yii serve
            - wait 2
modules:
    enabled:
    - WebDriver:
        url: 'http://localhost:8080/'
        window_size: 1920x1080
        browser: chrome
        capabilities:
            chromeOptions:
                args: ["--no-sandbox", "--headless", "--disable-gpu"]
                binary: "/usr/bin/google-chrome-stable"
            unexpectedAlertBehaviour: 'accept'

    - Yii2:
        part: [orm, email]
        entryScript: index-test.php

我已将 c3.php 添加到我的 index-test.php。这是与原始文件的唯一对比。

【问题讨论】:

    标签: php testing yii2 codeception


    【解决方案1】:

    您没有为 Webdriver 模块指定 index-test.php。试试这个:

     - WebDriver:
         url: 'http://localhost:8080/index-test.php'
         window_size: 1920x1080
    

    【讨论】:

    • 在这种情况下测试失败,_output 中出现空白页。
    • 这里是 wget 的输出:wget http://localhost:8080/index-test.php --2018-06-08 16:17:27-- http://localhost:8080/index-test.php Resolving localhost (localhost)... ::1, 127.0.0.1 Connecting to localhost (localhost)|::1|:8080... connected. HTTP request sent, awaiting response... 301 Moved Permanently Location: // [following] http://: Invalid host name.
    • 您需要弄清楚它重定向的原因,并首先修复它。就个人而言,我总是设置虚拟主机,这样我就可以在 mywebapp.locmywebapp.test 上运行该站点,第二个将从 index-test.php 运行
    • 感谢您的回复。如果我理解清楚,yii serve 在 localhost:8080 上运行内置的 Php 服务器,因此它与 vhost 或 nginx 配置之间没有关联。我发现 localhost:8080/index.php 做了同样的 301 重定向。最后两行更有趣 - 这没有重定向:位置:// [following]
    • 如果你不使用 yii serve 和 Codeception,而只是设置虚拟主机呢?
    【解决方案2】:

    我也遇到过类似的问题。也许不是理想的解决方案,但它适用于我在本地机器上:

    在acceptance.yml中:

    extensions:
        enabled:
            - Codeception\Extension\RunProcess:
                - php ./tests/bin/yii serve -r=web/index-test.php
                - wait 2
    

    在 web/index-test.php 中:

    <?php
    
    // NOTE: Make sure this file is not accessible when deployed to production
    if (!in_array(@$_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1'])) {
       die('You are not allowed to access this file.');
    }
    
    chdir(__DIR__);
    
    // ignore query params
    $file = preg_replace('/\\?.*$/', '', $_SERVER["REQUEST_URI"]);
    $filePath = realpath(ltrim($file, '/'));
    
    if ($filePath && is_file($filePath)) {
        // 1. check that file is not outside of this directory for security
        // 2. check for circular reference to router
        // 3. don't serve dotfiles
        if (strpos($filePath, __DIR__ . DIRECTORY_SEPARATOR) === 0 &&
            $filePath != __DIR__ . DIRECTORY_SEPARATOR . 'index-test.php' &&
            substr(basename($filePath), 0, 1) != '.'
        ) {
            if (strtolower(substr($filePath, -4)) == '.php') {
                // php file; serve through interpreter
                include $filePath;
            } else {
                // asset file; serve from filesystem
                return false;
            }
        } else {
            // disallowed file
            header("HTTP/1.1 404 Not Found");
            echo "404 Not Found";
        }
    } else {
        defined('YII_DEBUG') or define('YII_DEBUG', true);
        defined('YII_ENV') or define('YII_ENV', 'test');
        
        require(__DIR__ . '/../vendor/autoload.php');
        require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
    
        $config = require(__DIR__ . '/../config/test.php');
    
        (new yii\web\Application($config))->run();
    }
    

    链接:

    yii2/framework/console/controllers/ServeController.php

    PHP: Built-in web server - Manual

    PHP built in server and .htaccess mod rewrites

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 2011-07-11
      相关资源
      最近更新 更多