0

I'm trying to execute codeception acceptance test according to https://github.com/yiisoft/yii2-app-basic/blob/master/README.md#testing

I cannot figure out why yii serve starts with entryScript index.php while I expect index-test.php. That leads to YII_DEBUG = false and, as a consequence, unable to save e-mail to a file.

Here is my 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

Here is my 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

I've added c3.php to my index-test.php. It' the only contrast from original file.

Dekar
  • 43
  • 4

2 Answers2

0

You did not specify index-test.php for the Webdriver module. Try this:

 - WebDriver:
     url: 'http://localhost:8080/index-test.php'
     window_size: 1920x1080
sunomad
  • 1,693
  • 18
  • 23
  • In this case tests are fail with blank page in _output. – Dekar Jun 08 '18 at 11:47
  • Here is output for 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.` – Dekar Jun 08 '18 at 13:21
  • You will need to figure out why it is redirecting, and fix that first. Personally, I always set up vhosts so I can run the site on for example http://mywebapp.loc and http://mywebapp.test , where the second one will run from the index-test.php – sunomad Jun 11 '18 at 14:52
  • Thank you for your reply. If I understand clearly, yii serve runs build-in Php server on localhost:8080,so there is no correlation between it and vhost or nginx config. I caught up that localhost:8080/index.php did the same 301 redirect. Last two lines aremore interecting - this redirected o nothing: Location: // [following] – Dekar Jun 13 '18 at 10:08
  • And what if you don't use yii serve with Codeception, and just setup the vhost? – sunomad Jun 15 '18 at 08:36
  • In that case we are unable to grab e-mails – Dekar Jul 23 '18 at 17:26
0

I've had similar issue. Maybe not ideal solution, but it works ok for me on local machine:

In acceptance.yml:

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

In 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();
}

Links:

yii2/framework/console/controllers/ServeController.php

PHP: Built-in web server - Manual

PHP built in server and .htaccess mod rewrites