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?

5
  • 1
    What version of Laravel are you using? Commented Jun 5, 2017 at 17:03
  • 1
    @RossWilson i'm using version 5.2 Commented Jun 5, 2017 at 17:11
  • 1
    Can you share your full phpunit.xml file? Commented Jun 5, 2017 at 17:56
  • 1
    @Sandeesh i added the phpunit.xml file Commented Jun 5, 2017 at 18:31
  • 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. Commented Sep 3, 2019 at 9:56

3 Answers 3

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 :)

Sign up to request clarification or add additional context in comments.

4 Comments

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 works
php artisan config:clear will remove the cached config.php file and force the app to use what is defined in the configs and .env
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.
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.
0

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

Comments

-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.

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.