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)

3
  • Are you sure 'something' user have access for database? Commented Sep 3, 2016 at 9:47
  • Yes i am sure user have access Commented Sep 3, 2016 at 10:34
  • 1
    using password: NO is a clue that something is wrong with your database parameters Commented Sep 3, 2016 at 14:26

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.