jeudi 27 juillet 2017

Can't enable codeception tests for Yii2

I have problems configuring Codeception acceptance tests for my Yii2 website.

Tests are running okay, but I'd like to change the configuration so testing contact form won't send any emails. It should be pretty simple task, but I can't force it to use alternative config.

Following this tutorial: http://ift.tt/2dCMwYZ

/codeception.yml:

actor: Tester
paths:
    tests: tests
    log: tests/_output
    data: tests/_data
    helpers: tests/_support
settings:
    bootstrap: _bootstrap.php
    memory_limit: 1024M
    colors: true
modules:
    config:
        Yii2:
            configFile: 'config/test.php'

/tests/acceptance.suite.yml

class_name: AcceptanceTester
modules:
    enabled:
        - WebDriver:
            url: http://localhost/mypage
            browser: firefox
        - Yii2:
            part: orm
            entryScript: index.php
            cleanup: false
            configFile: test.php

And again I have no idea which directory this file points to. I tried with /config/test.php, /tests/config/test.php, nothing.

/config/test.php

<?php
$params = require(__DIR__ . '/params.php');
$dbParams = require(__DIR__ . '/test_db.php');

/**
 * Application configuration shared by all test types
 */
return [
    'id' => 'basic-tests',
    'basePath' => dirname(__DIR__),    
    'language' => 'en-US',
    'components' => [
        'db' => $dbParams,
        'mailer' => [
            'useFileTransport' => true, // <--- I'm interested in this setting 
        ],
        'urlManager' => [
            'showScriptName' => true,
        ],
        'user' => [
            'identityClass' => 'app\models\User',
        ],        
        'request' => [
            'cookieValidationKey' => 'test',
            'enableCsrfValidation' => false,
            // but if you absolutely need it set cookie domain to localhost
            /*
            'csrfCookie' => [
                'domain' => 'localhost',
            ],
            */
        ],        
    ],
    'params' => $params,
];

I even thought that this path might be relative to /tests directory, so I created file there as well:

/tests/config/test.php

<?php
// config/test.php
$config =  yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/main.php'),
    require(__DIR__ . '/main-local.php'),
    [
        'id' => 'app-tests',
        'components' => [
            'db' => [
                'dsn' => 'mysql:host=localhost;dbname=yii_app_test',
            ],
            'mailer' => [
                'useFileTransport' => true
            ]
        ]
    ]
);
return $config;

/tests/_bootstrap.php

<?php
define('YII_ENV', 'test');
defined('YII_DEBUG') or define('YII_DEBUG', true);

require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
require __DIR__ .'/../vendor/autoload.php';

part of /config/web.php to check if environment values are passed, but they aren't

if (YII_ENV_TEST || YII_ENV === 'test'){
    die('Testing settings loaded');
}

Launching my tests with ./vendor/bin/codecept run tests/acceptance/ContactCest.php

I'm not publishing tests because they are irrelevant, as they just use clicks on elements to send contact form. At the moment I've wasted 4 hours trying to override that config file. Probably it's some stupid rookie mistake, but I'm about to give up now.

Help me, StackOverflow, you are my only hope! ;)

Aucun commentaire:

Enregistrer un commentaire