Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SachinAgarwal's avatar

Laravel 5 testing environment

Like in Laravel 4, where we can make .env.testing.php, where can i setup testing environment variables. and how can switch laravel 5 to testing mode and back to local mode?

0 likes
11 replies
radbonev's avatar

L5 doesn't use the .env.testing.php notation anymore.

In your root directory you should notice a file .env.example or .env If it happens to be the file to .env.example, you should manually rename it to .env

Anyhow, in this file you can put all of your environmental variables.

When you open the file you will see something like

APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomString

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null

Now, in L5 you can create a file .env.testing and put your testing configurations in. Make sure that yout APP_ENV equals to testing; therefore, in your .env.testing file you should have:

APP_ENV=testing
2 likes
SachinAgarwal's avatar

@radbonev Ok i got that, but when running codeception tests, it is still taking the .env file and not .env.testing. how can i fix that?

3 likes
radbonev's avatar
Level 7

@sachinagarwal, in your configurations (e.g. functional.suite.yml), you can set a environment_file option.

For instance -

class_name: FunctionalTester
modules:
    enabled: [Laravel5, FunctionalHelper]
    config:
        Laravel5:
            environment_file: .env.testing

tell me if that worked :)

1 like
SachinAgarwal's avatar

@radbonev Yes that worked. Thanks a lot. And one last thing. How can i migrate this testing database? B'coz when i run

php artisan migrate --env=testing

It is not working. IT shows

Nothing to MIgrate
1 like
radbonev's avatar

@sachinagarwal , do you use the same database (for testing and local), would you paste both .env files?

If you happen to use the same database for "local" and "testing" it will result in "Nothing to Migrate"

SachinAgarwal's avatar

@radbonev No i do not use same database for both "local" and "testing".

here is my .env file

APP_ENV=local
APP_DEBUG=true
APP_KEY=V4EYYl0MjI4Bin9GkFPUE3cpAARPCF27

DB_HOST=localhost
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=laravel

CACHE_DRIVER=file
SESSION_DRIVER=file

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null

and my .env.testing file

APP_ENV=testing
APP_DEBUG=true
APP_KEY=V4EYYl0MjI4Bin9GkFPUE3cpAARPCF27

DB_HOST=localhost
DB_DATABASE=laravel_testing
DB_USERNAME=laravel
DB_PASSWORD=laravel

CACHE_DRIVER=file
SESSION_DRIVER=file

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
SachinAgarwal's avatar

@radbonev I think the "--env" option is not working.

It is taking .env file irrespective of "APP_ENV" set to "local" or "testing" even if I use "--env=testing"

i.e., I tried in .env

APP_ENV=local
...
DB_DATABASE=laravel_testing
...
php artisan migrate --env=testing

And my testing database was migrated.

1 like
radbonev's avatar

I will play with that later today. If you are sure that this is some sort of bug you can open a issue on github.

1 like
stefanX201's avatar

i have the same issue. of facts like this i think laravel 4.2 is the better choice for a solid enviroment.

1 like
shopsaloon's avatar

According to http://laravel.com/docs/5.1/testing we should put those variables inside the unit testing configuration file.

Since I'm using phpunit, my phpunit.xml file looks as follows. I know the question was assuming CodeCeption, but I figured others might find this useful.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="true"
         syntaxCheck="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>
            <directory suffix=".php">app/</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_CONNECTION" value="testing"/>
        <env name="FILESYSTEM" value="local"/>
    </php>
</phpunit>
2 likes

Please or to participate in this conversation.