0

I need to run tests with a separate database

I use POSTGRESQL as database driver.

My config/database.php

'pgsql' => [
        'driver' => 'pgsql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '5432'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
        'schema' => 'public',
        'sslmode' => 'prefer',
    ],

Special for running tests I defined DB_DATABASE in phpunit.xml

<php>
    <env name="APP_ENV" value="testing"/>
    <env name="BCRYPT_ROUNDS" value="4"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="SESSION_DRIVER" value="array"/>
    <env name="QUEUE_DRIVER" value="sync"/>
    <env name="MAIL_DRIVER" value="array"/>
    <env name="TELESCOPE_ENABLED" value="false"/>
    ...
    <env name="DB_DATABASE" value="database_test"/>
</php>

But when I run my PHPUnit tests, I get the error

PDOException: SQLSTATE[08006] [7] FATAL: database "database_test" does not exist

1
  • I would recommend following @thisiskelvin's approach below. However, if still want to use your approach, you just need to create the database_test database in postgres before running the tests. Commented Mar 15, 2019 at 11:40

1 Answer 1

2

The new connection must be set within database.php.

I would recommend setting up a sqlite database connection specifically for testing:

...

'connections' => [
    'database_test' => [
        'driver'   => 'sqlite',
        'database' => ':memory:',
        'prefix'   => '',
    ],
...

This will create a sqlite database connection which runs in memory.

You must then set the DB_CONNECTION rather than the DB_DATABASE.

<env name="DB_CONNECTION" value="database_test"/>
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.