Bezdepbonusu7Программирование: статьи и ответы

Вопрос

Встроенный сервер Yii2 начинается с index.php

Автор вопроса: DekarОтветов: 2

Я пытаюсь выполнить приемочный тест кода в соответствии с https://github.com/yiisoft/yii2-app-basic/blob/master/README.md#testing

Я не могу понять, почему 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

Вот мой accept.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. Это единственное отличие от оригинального файла.

phptestingcodeceptionyii2

Ответы

Ответ 1 · sunomad

Вы не указали index-test.php для модуля Webdriver. Попробуй это:

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

Ответ 2 · Yaroslav Kabaliuk

У меня была аналогичная проблема. Возможно, это не идеальное решение, но у меня на локальной машине оно работает нормально:

В accept.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: встроенный веб-сервер - вручную

Встроенный сервер PHP и мод .htaccess переписывает

Перевод вопроса и ответов сообщества Stack Overflow, лицензия CC BY-SA. Оригинал на Stack Overflow.

Похожие материалы