Environment configuration in Codeception with dotenv

, 3 comments,

Codeception is configurated using the codeception.yml file. But, you don't want to store any credentials in this file. You can use a dotenv configuration file to store these data (for example database credentials) and use them in our code and codeception.yml file.

Background

Head over to the phpdotenv repository to learn more about dotenv configuration and why you should use it.

Set up .env

First, create a .env.example file in the root of your repository. Fill it with all the names of variables that you need, but do not fill in the actual data:

DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=

Then, copy .env.example to .env and set all the variables to their correct values:

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myproject
DB_USERNAME=homestead
DB_PASSWORD=secret

Add the example file to Git. Add .env to your .gitignore list, never add it to your repository.

Configure Codeception

This is the codeception.yml that I've used for a recent project:

actor: Tester
paths:
    tests: tests
    log: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
settings:
    bootstrap: _bootstrap.php
    colors: true
    memory_limit: 1024M
extensions:
    enabled:
        - Codeception\Extension\RunFailed
modules:
    config:
        Db:
            dsn: "mysql:host=%DB_HOST%:%DB_PORT%;dbname=%DB_DATABASE%"
            user: "%DB_USERNAME%"
            password: "%DB_PASSWORD%"
params:
    - .env

As you can see the last line instructs Codeception to load the .env file and read all variables that it contains.

You can use these variables in your configuration like you see in the database configuration parts. %DB_HOST% with be replaced with the value of DB_HOST from .env.

Using variables in your code

All variables will be assigned to the global $_ENV variable. So, in your PHP code, you can read from $_ENV['DB_HOST'] to retrieve the same value as before.



If you have any questions or comments about this post, feel free to leave a comment!

Related articles

Comments

3
When I try this, I get the following error:

```
[Codeception\Exception\ConfigurationException]
Failed loading params from .env
`vlucas/phpdotenv` library is required to parse .env files.
Please install it via composer: composer require vlucas/phpdotenv
```

I have it installed and it is in my compsoser.json file. Any tips?
Hi Steve,

Sorry, I'm not sure what is causing that exception. You could take a look at this project where I used this set: https://github.com/barryvanveen/allunited-import-news.

Hope that helps!

Barry
Thanks for posting, this is exactly what I needed.

However, perhaps a more cleaner way of reading the $_ENV values is with getenv(), for example getenv('DB_HOST').

Add your comment