yiisoft / yii-base-web

Home Page:https://www.yiiframework.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Script to run tests

leandrogehlen opened this issue · comments

I'm using new app template, but to run tests i have created yiitest and yiitest.bat into bin directory.

#!/usr/bin/env php
<?php
/**
 * Yii console bootstrap file.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

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

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

$config = yii\helpers\ArrayHelper::merge(
    require(dirname(__DIR__) . '/config/console.php'), [
        'components' => [
            'db' => require(__DIR__ . '/../config/db.test.php')
        ]
    ]
);

$application = new yii\console\Application($config);
$exitCode = $application->run();
exit($exitCode);

But i didn't like this solution then i have the follow suggestion to solve it:

  • Create a new file with test configurations into config/test.php
return yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/console.php'), [
        'id' => 'app-test',
        'aliases' => [
            '@tests' => dirname(__DIR__) . '/tests'
        ],
        'components' => [
            'cache' => [
                'class' => 'yii\caching\FileCache',
            ],
            'session' => [
                'class' => 'yii\web\CacheSession'
            ],
            'db' => require(__DIR__ . '/db.test.php')
        ]
    ]
);
  • Create new core command yii\console\controllers\TestController
  • Use only bin/yii with params (bin/yii test --config=test)

On reflection this can be solved with environment var in config/console.php:

<?php
$params = require __DIR__ . '/params.php';
$db = require __DIR__ .  (YII_ENV === 'test')  ?  '/db.test.php' : 'db.php';
$config = [
    'id' => 'my-project',
    'name' => 'MyProject',
    ....