K
K
knowledge2018-02-18 22:55:02
PHP
knowledge, 2018-02-18 22:55:02

How to set up a database when testing with codeception?

I want to make a test for a method that checks if the user has this login when registering.
Method Code

public function ifExistUsername(string $username)
  {
    $user = User::where('username', $username)->first();
    if ($user)
      return 'Пользователь с таким логином уже есть.';

    return;
  }

In codeception, the test looks like this:
public function testIfExistUsername()
    {
        $validator = new FormValidator();

        $username = 'user0';
        $this->assertContains($validator->ifExistUsername($username), 'Пользователь с таким логином уже есть.');
    }

In the codeception settings, I enabled the Db module and set the settings:
- Db:
            dsn: 'mysql:host=localhost;dbname=testdb'
            user: 'root'
            password: ''
            dump: 'tests/_data/dump.sql'
            populate: true
            cleanup: true

I created a test db, put a dump, but when the test is run, codeception checks the login not in the test database, but in the main one, how can I configure it to work with the test database? I read the documentation page in the db module on the codeception site, it is not explained there
PS I do not use laravel, but I use eloquent

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hOtRush, 2018-02-19
@knowledge

The Db module settings for codeception will not be used by the system you are testing. They are needed in order to upload the dump to an empty database, clean the database after each test and delete everything after the tests are completed. Your system should have its own mechanisms for selecting database settings for the testing process.
The most true way is probably to write a module for codeception, as is done for all major frameworks, incl. Laravel https://github.com/Codeception/Codeception/blob/2.... https://github.com/Codeception/Codeception/blob/2....
Well, if you use dot-env configuration - be careful. Codeception can load settings from all .env files lying next to codeception.yml in a chaotic manner, and this is very sluggishly written in the docks, so even .env.example can fundamentally break your tests)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question