Search code examples
testingyii2codeceptionyii2-basic-app

Creating tests for modules yii2


Hi i am new with yii2 testing, i have setup testing environment and tests in app/tests and its working fine. Now i want to setup tests for one of my modules which is modules/v1 and contains commands,components,controllers and models folders. I have created initial setup for tests in modules/v1 using codecept bootstrap and setup it as following

codeception.yml

namespace: v1
paths:
    tests: tests
    output: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
actor_suffix: Tester
settings:
    bootstrap: /../_bootstrap.php
    colors: true
    memory_limit: 1024M
extensions:
    enabled:
        - Codeception\Extension\RunFailed
modules:
    enabled:
        - Yii2:
            configFile: '/../../config/test.php'

tests/unit.suite.yml

actor: UnitTester
modules:
    enabled:
        - Asserts
        - \v1\Helper\Unit

tests/functional.suite.yml

actor: FunctionalTester
modules:
    enabled:
        - Asserts
        - \v1\Helper\Functional

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';

Now what i want to test is a command action in moduels/v1/commands/ ReportController.php

<?php

namespace app\modules\v1\commands;

class ReportController extends Controller
{
   public function actionOverview()
   {
      echo 1;
   }
}

In my tests\functional\commands\ReportControllerCest.php i want to run action overview from commands\ReportController.php which i am doing like this

<?php
namespace modules\v1\tests\functional\commands;

use v1\FunctionalTester;
use Yii;

class ReportControllerCest
{
    public function _before(FunctionalTester $I)
    {
    }

    public function _after(FunctionalTester $I)
    {
    }

    // tests
    public function tryToTest(FunctionalTester $I)
    {
        Yii::$app->createControllerByID('report')->run('overview');
    }
}

which is not working and gives me following error

1) ReportControllerCest: Try to test
 Test  tests\functional\commands\ReportControllerCest.php:tryToTest

  [yii\base\InvalidRouteException] Unable to resolve the request: report/overview

#1  C:\xampp\htdocs\preroll\vendor\yiisoft\yii2\base\Controller.php:189
#2  C:\xampp\htdocs\preroll\modules\v1\tests\functional\commands\ReportControllerCest.php:20
#3  modules\v1\tests\functional\commands\ReportControllerCest->tryToTest

means its not checking into that commands/ReportController.php

Questions. 1-is this how modules should be tested with their separate tests folder? if yes then what m doing wrong? if no then whats the right way to do it? 2-how can i test commands (console commands)?


Solution

  • I will leave the work around through which i achieved my goal for anyone else having same problem.
    I used codeception Environments in Advanced Usage and set up different config file for both console and web app. then just use --env when u run tests codecept run --env console