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?

miken32
  • 42,008
  • 16
  • 111
  • 154
bsgrd
  • 633
  • 1
  • 9
  • 26
  • 1
    What version of Laravel are you using? – Rwd Jun 05 '17 at 17:03
  • 1
    @RossWilson i'm using version 5.2 – bsgrd Jun 05 '17 at 17:11
  • 1
    Can you share your full phpunit.xml file? – Sandeesh Jun 05 '17 at 17:56
  • 1
    @Sandeesh i added the phpunit.xml file – bsgrd Jun 05 '17 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 `` 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. – toraman Sep 03 '19 at 09:56

3 Answers3

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

Tyteck
  • 135
  • 2
  • 12
  • 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 – Jaroslav Klimčík Feb 28 '19 at 08:17
  • 2
    `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 – zeros-and-ones Jun 14 '19 at 21:45
  • 1
    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. – Tyteck Jul 12 '20 at 20:07
  • 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. – naw103 Sep 06 '20 at 20:00
0

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

Faruk Nasir
  • 192
  • 1
  • 6
-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.

iateadonut
  • 1,951
  • 21
  • 32