Laravel PHPUnit loading .env.testing file
Asked Answered
T

3

10

I have some problems with getting Laravel to load the proper .env file for my testcases. I'm using PHPUnit with the following var set in phpunit.xml:

<?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="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
            <exclude>
                <file>./app/Http/routes.php</file>
            </exclude>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>
</phpunit>

In my .env.testing file i have:

APP_ENV=testing
DB_CONNECTION=sqlite

And i have his connection set up under config/database.php:

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

It just isn't loading the .env.testing file. If i do this in my TestCase.php:

dd(env('APP_ENV'));

I still get "development" from my .env file

I have also tried using:

$app->loadEnvironmentFrom('.env.testing');

Like suggested in the thread here

Does anyone have an idea to what could be wrong?

Thaxton answered 5/6, 2017 at 16:46 Comment(5)
What version of Laravel are you using?Pepi
@RossWilson i'm using version 5.2Thaxton
Can you share your full phpunit.xml file?Cumming
@Cumming i added the phpunit.xml fileThaxton
I was having the same problem and was commenting here a workaround but then I realized that I'm having this in my bootstrap script rather than the actual tests. In my case the problem is that <env name="APP_ENV" value="testing"/> has no effect in the bootstrap script. I had to add putenv('APP_ENV=testing'); manually before creating the application. If this is not the case for you, you may be able to use this as a workaround.Drank
S
7

My problem was quite similar. Env file .env.testing was not read at all. I tried to put some var_dump everywhere in my test file, everything was ok but even a var_dump(env("APP_NAME")) was null.

I figured to type a "php artisan config:clear" and everything was back to normal. Not sure what I've done but it worked for me :)

Standee answered 7/2, 2019 at 20:35 Comment(4)
I tried php artisan config:cache but it doesn't helped. When i tried php artisan config:clear it worked. I don't have any idea what is difference (clear should be included inside cache command) but it somehow worksDoublequick
php artisan config:clear will remove the cached config.php file and force the app to use what is defined in the configs and .envTickler
The problem with artisan config:clear or artisan optimize:clear is that if you have a permission problem to delete files/folders it will fail silently.Standee
Thanks @Tyteck, been banging my head against a wall trying to get phpunit to read the correct env files. tried artisan config:clear as many posts suggested and it did nothing, after seeing your posts i ran with sudo, sudo php artisan config:clear and it now it works correctly.Cohosh
V
0

To load the .env.testing file, you have to execute the artisan command with --env=testing

Vesicant answered 27/10, 2021 at 14:4 Comment(0)
M
-2

Are you running Feature or Unit tests? I think unit tests are only supposed to work on mocks, while Feature tests may persist to the database.

Moult answered 13/5, 2021 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.