0

When creating automated testing in unit testing. I get this error.

My code is in unit test

 function testSavingUser()
{
    $user = new User();
    $user->auth_key='value';
    $user->password_hash='value';
    $user->password_reset_token='value';
    $user->email='value';
    $user->status=1;
    $user->first_name='value';
    $user->last_name='value';
    $user->image='value';
    $user->role_id=1;
    $user->created_date='2016-08-31 16:57:10';
    $user->updated_date='2016-08-31 16:57:10';

    $user->save();
    $this->assertEquals('Miles Davis', $user->getFullName());
    $this->tester->seeInDatabase('users', ['name' => 'Miles', 'surname' => 'Davis']);
}

This my config file

    $config =  yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/web.php'),
    //require(__DIR__ . '/main-local.php'),
    [
        'id' => 'app-tests',
        'components' => [
            'db' => [
                'class' => 'yii\db\Connection',
                'dsn' => 'mysql:host=' . getenv('DB_HOST'). ';dbname=' . getenv('DB_NAME'),
                'username' => getenv('DB_USER'),
                'password' => getenv('DB_PASSWORD'),
                'charset' => 'utf8',
            ],
        ]        
    ]
);
return $config;

In functional.suite.yml

class_name: FunctionalTester
    modules:
      enabled:
         - Yii2:
             configFile: 'config/test.php'

When trying to execute my test with codecept run it shows error like this

[yii\db\Exception] SQLSTATE[28000] [1045] Access denied for user 'something'@'localhost' (using password: NO)

Naktibalda
  • 13,705
  • 5
  • 35
  • 51
Yasar Arafath
  • 605
  • 1
  • 9
  • 30

1 Answers1

1

Possible reasons of error:

  1. Provided credentials are incorrect or mixed with each other. Check them accurately for typos, etc.
  2. Environment variables are not accessed or extracted correctly with getenv calls. Check if the keys are right and make sure that correct values returned (you can use something like var_dump).
  3. User has no privileges to access this database. You can see how to set it for example in this related question.
Community
  • 1
  • 1
arogachev
  • 33,150
  • 7
  • 114
  • 117