How do I configure the db component to run tests in Yii2 with codeception

27 Views Asked by At

I have two db components in my app and my web.php is configured as shown below. I use the same db components in my console.php file too.

$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm' => '@vendor/npm-asset',
        '@photos' => '@app/uploads/avatars',
    ],
    'components' => [
         //...

        'db' => $db['smis'],
        'db2' => $db['smisportal']

        //...
    ],
    'params' => $params,
    'modules' => [
        //...

        'gridview' => [
            'class' => Module::class
        ]
    ],
];

return $config;

My db.php file is as shown:

return [
    'smis' => [
        'class' => 'yii\db\Connection',
        'dsn' => //...
        'username' => //...
        'password' => //...
        'charset' => //...
    ],
    'smisportal' => [
        'class' => 'yii\db\Connection',
        'dsn' => //...
        'username' => //...
        'password' => //...
        'charset' => //...
    ]
];

I want to run an example test below using codeception

class RegistrationCourseTest extends \Codeception\Test\Unit
{
    public function testEmailIsSentOnRegistration()
    {
        $this->assertSame('test', 'test', 'Strings don\'t match');
    }
}

My tests/_bootstrap.php looks like this:

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

My tests/unit.suite.yml looks like this:

# Codeception Test Suite Configuration

# suite for unit (internal) tests.
# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.

actor: UnitTester
modules:
    enabled:
      - Asserts
      - Yii2:
            part: [orm, email, fixtures]

When I run the test I get this exception [yii\base\InvalidConfigException] The configuration for the "db" component must contain a "class" element.

0

There are 0 best solutions below