0
votes

I noticed a strage behaviour in codeception: if I rely only on codeception.yml with:

actor: Tester
paths:
    tests: tests
    log: tests/_output
    data: tests/_data
    helpers: tests/_support
settings:
    bootstrap: _bootstrap.php
    colors: true
    memory_limit: 1024M
modules:
    config:
        Db:
            dsn: 'mysql:host=localhost;dbname=test'
            user: 'test'
            password: 'test'
            dump: tests/_data/dump.sql

run:

php artisan migrate --package=cartalyst/sentry --env="testing" php artisan migrate --seed --env="testing"

I've got this error

[PDOException]
SQLSTATE[42000] [1044] Access denied for user ''@'localhost' to database 'f orge'

if I put a file database.php in app/config/testing with

return array(

    'default' => 'mysql',

        'connections' => array(

        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'lama-test',
            'username'  => 'lama-test',
            'password'  => 'lama-test',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        )

    ),

all it's ok.

I'm new at codeception so I'm wondering what's the matter and if I make something wrong.

1
you are setting the environment to "testing" when you run the migration - so you need the corresponding database configuration for testing. You seem to have answered your own question? - Laurence
I've read the book laravel-testing-decoded and it states to populate the testing db you should run php artisan migrate --seed --env="testing" but I wait for a call to codeception.yml not to database.php - Whisher

1 Answers

1
votes

So there are two aspects to database access:

  1. From your application
  2. From codeception directly

From your application:

When running tests, your application needs to access your database as part of your code. i.e. when you search for the current user, or add a new blog post etc. This database access will occur through Laravel in "testing" mode - so it is good practice to define a "testing" database (which you have done).

From codeception directly:

When running tests, it is important that databases are the same before/after each test. That way a test does not rely on the output of a previous test etc. Codeception will use the database access defined in the yaml file to directly access the database, and 'refresh' it with your tests/_data/dump.sql file in between each test.

Codeception is not using the 'direct' database access during the actual test - just before/after.